p54common.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  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. /* XX correct? */
  688. rx_status.qual = (100 * hdr->rssi) / 127;
  689. if (hdr->rate & 0x10)
  690. rx_status.flag |= RX_FLAG_SHORTPRE;
  691. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  692. rx_status.rate_idx = (rate < 4) ? 0 : rate - 4;
  693. else
  694. rx_status.rate_idx = rate;
  695. rx_status.freq = freq;
  696. rx_status.band = dev->conf.channel->band;
  697. rx_status.antenna = hdr->antenna;
  698. tsf32 = le32_to_cpu(hdr->tsf32);
  699. if (tsf32 < priv->tsf_low32)
  700. priv->tsf_high32++;
  701. rx_status.mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  702. priv->tsf_low32 = tsf32;
  703. rx_status.flag |= RX_FLAG_TSFT;
  704. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  705. header_len += hdr->align[0];
  706. skb_pull(skb, header_len);
  707. skb_trim(skb, le16_to_cpu(hdr->len));
  708. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  709. queue_delayed_work(dev->workqueue, &priv->work,
  710. msecs_to_jiffies(P54_STATISTICS_UPDATE));
  711. return -1;
  712. }
  713. static void inline p54_wake_free_queues(struct ieee80211_hw *dev)
  714. {
  715. struct p54_common *priv = dev->priv;
  716. int i;
  717. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  718. return ;
  719. for (i = 0; i < dev->queues; i++)
  720. if (priv->tx_stats[i + P54_QUEUE_DATA].len <
  721. priv->tx_stats[i + P54_QUEUE_DATA].limit)
  722. ieee80211_wake_queue(dev, i);
  723. }
  724. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  725. {
  726. struct p54_common *priv = dev->priv;
  727. struct ieee80211_tx_info *info;
  728. struct p54_tx_info *range;
  729. unsigned long flags;
  730. u32 freed = 0, last_addr = priv->rx_start;
  731. if (unlikely(!skb || !dev || !skb_queue_len(&priv->tx_queue)))
  732. return;
  733. /*
  734. * don't try to free an already unlinked skb
  735. */
  736. if (unlikely((!skb->next) || (!skb->prev)))
  737. return;
  738. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  739. info = IEEE80211_SKB_CB(skb);
  740. range = (void *)info->rate_driver_data;
  741. if (skb->prev != (struct sk_buff *)&priv->tx_queue) {
  742. struct ieee80211_tx_info *ni;
  743. struct p54_tx_info *mr;
  744. ni = IEEE80211_SKB_CB(skb->prev);
  745. mr = (struct p54_tx_info *)ni->rate_driver_data;
  746. last_addr = mr->end_addr;
  747. }
  748. if (skb->next != (struct sk_buff *)&priv->tx_queue) {
  749. struct ieee80211_tx_info *ni;
  750. struct p54_tx_info *mr;
  751. ni = IEEE80211_SKB_CB(skb->next);
  752. mr = (struct p54_tx_info *)ni->rate_driver_data;
  753. freed = mr->start_addr - last_addr;
  754. } else
  755. freed = priv->rx_end - last_addr;
  756. __skb_unlink(skb, &priv->tx_queue);
  757. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  758. dev_kfree_skb_any(skb);
  759. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  760. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  761. p54_wake_free_queues(dev);
  762. }
  763. EXPORT_SYMBOL_GPL(p54_free_skb);
  764. static struct sk_buff *p54_find_tx_entry(struct ieee80211_hw *dev,
  765. __le32 req_id)
  766. {
  767. struct p54_common *priv = dev->priv;
  768. struct sk_buff *entry;
  769. unsigned long flags;
  770. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  771. entry = priv->tx_queue.next;
  772. while (entry != (struct sk_buff *)&priv->tx_queue) {
  773. struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
  774. if (hdr->req_id == req_id) {
  775. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  776. return entry;
  777. }
  778. entry = entry->next;
  779. }
  780. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  781. return NULL;
  782. }
  783. static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb)
  784. {
  785. struct p54_common *priv = dev->priv;
  786. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  787. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  788. struct sk_buff *entry;
  789. u32 addr = le32_to_cpu(hdr->req_id) - priv->headroom;
  790. struct p54_tx_info *range = NULL;
  791. u32 freed = 0;
  792. u32 last_addr = priv->rx_start;
  793. unsigned long flags;
  794. int count, idx;
  795. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  796. entry = (struct sk_buff *) priv->tx_queue.next;
  797. while (entry != (struct sk_buff *)&priv->tx_queue) {
  798. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(entry);
  799. struct p54_hdr *entry_hdr;
  800. struct p54_tx_data *entry_data;
  801. unsigned int pad = 0, frame_len;
  802. range = (void *)info->rate_driver_data;
  803. if (range->start_addr != addr) {
  804. last_addr = range->end_addr;
  805. entry = entry->next;
  806. continue;
  807. }
  808. if (entry->next != (struct sk_buff *)&priv->tx_queue) {
  809. struct ieee80211_tx_info *ni;
  810. struct p54_tx_info *mr;
  811. ni = IEEE80211_SKB_CB(entry->next);
  812. mr = (struct p54_tx_info *)ni->rate_driver_data;
  813. freed = mr->start_addr - last_addr;
  814. } else
  815. freed = priv->rx_end - last_addr;
  816. last_addr = range->end_addr;
  817. __skb_unlink(entry, &priv->tx_queue);
  818. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  819. frame_len = entry->len;
  820. entry_hdr = (struct p54_hdr *) entry->data;
  821. entry_data = (struct p54_tx_data *) entry_hdr->data;
  822. priv->tx_stats[entry_data->hw_queue].len--;
  823. priv->stats.dot11ACKFailureCount += payload->tries - 1;
  824. /*
  825. * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
  826. * generated by the driver. Therefore tx_status is bogus
  827. * and we don't want to confuse the mac80211 stack.
  828. */
  829. if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
  830. if (entry_data->hw_queue == P54_QUEUE_BEACON)
  831. priv->cached_beacon = NULL;
  832. kfree_skb(entry);
  833. goto out;
  834. }
  835. /*
  836. * Clear manually, ieee80211_tx_info_clear_status would
  837. * clear the counts too and we need them.
  838. */
  839. memset(&info->status.ampdu_ack_len, 0,
  840. sizeof(struct ieee80211_tx_info) -
  841. offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
  842. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  843. status.ampdu_ack_len) != 23);
  844. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  845. pad = entry_data->align[0];
  846. /* walk through the rates array and adjust the counts */
  847. count = payload->tries;
  848. for (idx = 0; idx < 4; idx++) {
  849. if (count >= info->status.rates[idx].count) {
  850. count -= info->status.rates[idx].count;
  851. } else if (count > 0) {
  852. info->status.rates[idx].count = count;
  853. count = 0;
  854. } else {
  855. info->status.rates[idx].idx = -1;
  856. info->status.rates[idx].count = 0;
  857. }
  858. }
  859. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  860. (!payload->status))
  861. info->flags |= IEEE80211_TX_STAT_ACK;
  862. if (payload->status & P54_TX_PSM_CANCELLED)
  863. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  864. info->status.ack_signal = p54_rssi_to_dbm(dev,
  865. (int)payload->ack_rssi);
  866. /* Undo all changes to the frame. */
  867. switch (entry_data->key_type) {
  868. case P54_CRYPTO_TKIPMICHAEL: {
  869. u8 *iv = (u8 *)(entry_data->align + pad +
  870. entry_data->crypt_offset);
  871. /* Restore the original TKIP IV. */
  872. iv[2] = iv[0];
  873. iv[0] = iv[1];
  874. iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
  875. frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
  876. break;
  877. }
  878. case P54_CRYPTO_AESCCMP:
  879. frame_len -= 8; /* remove CCMP_MIC */
  880. break;
  881. case P54_CRYPTO_WEP:
  882. frame_len -= 4; /* remove WEP_ICV */
  883. break;
  884. }
  885. skb_trim(entry, frame_len);
  886. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  887. ieee80211_tx_status_irqsafe(dev, entry);
  888. goto out;
  889. }
  890. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  891. out:
  892. if (freed >= priv->headroom + sizeof(struct p54_hdr) + 48 +
  893. IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  894. p54_wake_free_queues(dev);
  895. }
  896. static void p54_rx_eeprom_readback(struct ieee80211_hw *dev,
  897. struct sk_buff *skb)
  898. {
  899. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  900. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  901. struct p54_common *priv = dev->priv;
  902. if (!priv->eeprom)
  903. return ;
  904. if (priv->fw_var >= 0x509) {
  905. memcpy(priv->eeprom, eeprom->v2.data,
  906. le16_to_cpu(eeprom->v2.len));
  907. } else {
  908. memcpy(priv->eeprom, eeprom->v1.data,
  909. le16_to_cpu(eeprom->v1.len));
  910. }
  911. complete(&priv->eeprom_comp);
  912. }
  913. static void p54_rx_stats(struct ieee80211_hw *dev, struct sk_buff *skb)
  914. {
  915. struct p54_common *priv = dev->priv;
  916. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  917. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  918. u32 tsf32;
  919. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  920. return ;
  921. tsf32 = le32_to_cpu(stats->tsf32);
  922. if (tsf32 < priv->tsf_low32)
  923. priv->tsf_high32++;
  924. priv->tsf_low32 = tsf32;
  925. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  926. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  927. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  928. priv->noise = p54_rssi_to_dbm(dev, le32_to_cpu(stats->noise));
  929. p54_free_skb(dev, p54_find_tx_entry(dev, hdr->req_id));
  930. }
  931. static void p54_rx_trap(struct ieee80211_hw *dev, struct sk_buff *skb)
  932. {
  933. struct p54_common *priv = dev->priv;
  934. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  935. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  936. u16 event = le16_to_cpu(trap->event);
  937. u16 freq = le16_to_cpu(trap->frequency);
  938. switch (event) {
  939. case P54_TRAP_BEACON_TX:
  940. break;
  941. case P54_TRAP_RADAR:
  942. printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
  943. wiphy_name(dev->wiphy), freq);
  944. break;
  945. case P54_TRAP_NO_BEACON:
  946. if (priv->vif)
  947. ieee80211_beacon_loss(priv->vif);
  948. break;
  949. case P54_TRAP_SCAN:
  950. break;
  951. case P54_TRAP_TBTT:
  952. break;
  953. case P54_TRAP_TIMER:
  954. break;
  955. default:
  956. printk(KERN_INFO "%s: received event:%x freq:%d\n",
  957. wiphy_name(dev->wiphy), event, freq);
  958. break;
  959. }
  960. }
  961. static int p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb)
  962. {
  963. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  964. switch (le16_to_cpu(hdr->type)) {
  965. case P54_CONTROL_TYPE_TXDONE:
  966. p54_rx_frame_sent(dev, skb);
  967. break;
  968. case P54_CONTROL_TYPE_TRAP:
  969. p54_rx_trap(dev, skb);
  970. break;
  971. case P54_CONTROL_TYPE_BBP:
  972. break;
  973. case P54_CONTROL_TYPE_STAT_READBACK:
  974. p54_rx_stats(dev, skb);
  975. break;
  976. case P54_CONTROL_TYPE_EEPROM_READBACK:
  977. p54_rx_eeprom_readback(dev, skb);
  978. break;
  979. default:
  980. printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
  981. wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));
  982. break;
  983. }
  984. return 0;
  985. }
  986. /* returns zero if skb can be reused */
  987. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  988. {
  989. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  990. if (type & P54_HDR_FLAG_CONTROL)
  991. return p54_rx_control(dev, skb);
  992. else
  993. return p54_rx_data(dev, skb);
  994. }
  995. EXPORT_SYMBOL_GPL(p54_rx);
  996. /*
  997. * So, the firmware is somewhat stupid and doesn't know what places in its
  998. * memory incoming data should go to. By poking around in the firmware, we
  999. * can find some unused memory to upload our packets to. However, data that we
  1000. * want the card to TX needs to stay intact until the card has told us that
  1001. * it is done with it. This function finds empty places we can upload to and
  1002. * marks allocated areas as reserved if necessary. p54_rx_frame_sent or
  1003. * p54_free_skb frees allocated areas.
  1004. */
  1005. static int p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,
  1006. struct p54_hdr *data, u32 len)
  1007. {
  1008. struct p54_common *priv = dev->priv;
  1009. struct sk_buff *entry;
  1010. struct sk_buff *target_skb = NULL;
  1011. struct ieee80211_tx_info *info;
  1012. struct p54_tx_info *range;
  1013. u32 last_addr = priv->rx_start;
  1014. u32 largest_hole = 0;
  1015. u32 target_addr = priv->rx_start;
  1016. unsigned long flags;
  1017. unsigned int left;
  1018. len = (len + priv->headroom + priv->tailroom + 3) & ~0x3;
  1019. if (!skb)
  1020. return -EINVAL;
  1021. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  1022. left = skb_queue_len(&priv->tx_queue);
  1023. if (unlikely(left >= 28)) {
  1024. /*
  1025. * The tx_queue is nearly full!
  1026. * We have throttle normal data traffic, because we must
  1027. * have a few spare slots for control frames left.
  1028. */
  1029. ieee80211_stop_queues(dev);
  1030. queue_delayed_work(dev->workqueue, &priv->work,
  1031. msecs_to_jiffies(P54_TX_TIMEOUT));
  1032. if (unlikely(left == 32)) {
  1033. /*
  1034. * The tx_queue is now really full.
  1035. *
  1036. * TODO: check if the device has crashed and reset it.
  1037. */
  1038. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1039. return -ENOSPC;
  1040. }
  1041. }
  1042. entry = priv->tx_queue.next;
  1043. while (left--) {
  1044. u32 hole_size;
  1045. info = IEEE80211_SKB_CB(entry);
  1046. range = (void *)info->rate_driver_data;
  1047. hole_size = range->start_addr - last_addr;
  1048. if (!target_skb && hole_size >= len) {
  1049. target_skb = entry->prev;
  1050. hole_size -= len;
  1051. target_addr = last_addr;
  1052. }
  1053. largest_hole = max(largest_hole, hole_size);
  1054. last_addr = range->end_addr;
  1055. entry = entry->next;
  1056. }
  1057. if (!target_skb && priv->rx_end - last_addr >= len) {
  1058. target_skb = priv->tx_queue.prev;
  1059. largest_hole = max(largest_hole, priv->rx_end - last_addr - len);
  1060. if (!skb_queue_empty(&priv->tx_queue)) {
  1061. info = IEEE80211_SKB_CB(target_skb);
  1062. range = (void *)info->rate_driver_data;
  1063. target_addr = range->end_addr;
  1064. }
  1065. } else
  1066. largest_hole = max(largest_hole, priv->rx_end - last_addr);
  1067. if (!target_skb) {
  1068. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1069. ieee80211_stop_queues(dev);
  1070. return -ENOSPC;
  1071. }
  1072. info = IEEE80211_SKB_CB(skb);
  1073. range = (void *)info->rate_driver_data;
  1074. range->start_addr = target_addr;
  1075. range->end_addr = target_addr + len;
  1076. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  1077. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  1078. if (largest_hole < priv->headroom + sizeof(struct p54_hdr) +
  1079. 48 + IEEE80211_MAX_RTS_THRESHOLD + priv->tailroom)
  1080. ieee80211_stop_queues(dev);
  1081. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  1082. return 0;
  1083. }
  1084. static struct sk_buff *p54_alloc_skb(struct ieee80211_hw *dev, u16 hdr_flags,
  1085. u16 payload_len, u16 type, gfp_t memflags)
  1086. {
  1087. struct p54_common *priv = dev->priv;
  1088. struct p54_hdr *hdr;
  1089. struct sk_buff *skb;
  1090. size_t frame_len = sizeof(*hdr) + payload_len;
  1091. if (frame_len > P54_MAX_CTRL_FRAME_LEN)
  1092. return NULL;
  1093. skb = __dev_alloc_skb(priv->tx_hdr_len + frame_len, memflags);
  1094. if (!skb)
  1095. return NULL;
  1096. skb_reserve(skb, priv->tx_hdr_len);
  1097. hdr = (struct p54_hdr *) skb_put(skb, sizeof(*hdr));
  1098. hdr->flags = cpu_to_le16(hdr_flags);
  1099. hdr->len = cpu_to_le16(payload_len);
  1100. hdr->type = cpu_to_le16(type);
  1101. hdr->tries = hdr->rts_tries = 0;
  1102. if (p54_assign_address(dev, skb, hdr, frame_len)) {
  1103. kfree_skb(skb);
  1104. return NULL;
  1105. }
  1106. return skb;
  1107. }
  1108. int p54_read_eeprom(struct ieee80211_hw *dev)
  1109. {
  1110. struct p54_common *priv = dev->priv;
  1111. struct p54_eeprom_lm86 *eeprom_hdr;
  1112. struct sk_buff *skb;
  1113. size_t eeprom_size = 0x2020, offset = 0, blocksize, maxblocksize;
  1114. int ret = -ENOMEM;
  1115. void *eeprom = NULL;
  1116. maxblocksize = EEPROM_READBACK_LEN;
  1117. if (priv->fw_var >= 0x509)
  1118. maxblocksize -= 0xc;
  1119. else
  1120. maxblocksize -= 0x4;
  1121. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL, sizeof(*eeprom_hdr) +
  1122. maxblocksize, P54_CONTROL_TYPE_EEPROM_READBACK,
  1123. GFP_KERNEL);
  1124. if (!skb)
  1125. goto free;
  1126. priv->eeprom = kzalloc(EEPROM_READBACK_LEN, GFP_KERNEL);
  1127. if (!priv->eeprom)
  1128. goto free;
  1129. eeprom = kzalloc(eeprom_size, GFP_KERNEL);
  1130. if (!eeprom)
  1131. goto free;
  1132. eeprom_hdr = (struct p54_eeprom_lm86 *) skb_put(skb,
  1133. sizeof(*eeprom_hdr) + maxblocksize);
  1134. while (eeprom_size) {
  1135. blocksize = min(eeprom_size, maxblocksize);
  1136. if (priv->fw_var < 0x509) {
  1137. eeprom_hdr->v1.offset = cpu_to_le16(offset);
  1138. eeprom_hdr->v1.len = cpu_to_le16(blocksize);
  1139. } else {
  1140. eeprom_hdr->v2.offset = cpu_to_le32(offset);
  1141. eeprom_hdr->v2.len = cpu_to_le16(blocksize);
  1142. eeprom_hdr->v2.magic2 = 0xf;
  1143. memcpy(eeprom_hdr->v2.magic, (const char *)"LOCK", 4);
  1144. }
  1145. priv->tx(dev, skb);
  1146. if (!wait_for_completion_interruptible_timeout(&priv->eeprom_comp, HZ)) {
  1147. printk(KERN_ERR "%s: device does not respond!\n",
  1148. wiphy_name(dev->wiphy));
  1149. ret = -EBUSY;
  1150. goto free;
  1151. }
  1152. memcpy(eeprom + offset, priv->eeprom, blocksize);
  1153. offset += blocksize;
  1154. eeprom_size -= blocksize;
  1155. }
  1156. ret = p54_parse_eeprom(dev, eeprom, offset);
  1157. free:
  1158. kfree(priv->eeprom);
  1159. priv->eeprom = NULL;
  1160. p54_free_skb(dev, skb);
  1161. kfree(eeprom);
  1162. return ret;
  1163. }
  1164. EXPORT_SYMBOL_GPL(p54_read_eeprom);
  1165. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  1166. bool set)
  1167. {
  1168. struct p54_common *priv = dev->priv;
  1169. struct sk_buff *skb;
  1170. struct p54_tim *tim;
  1171. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*tim),
  1172. P54_CONTROL_TYPE_TIM, GFP_ATOMIC);
  1173. if (!skb)
  1174. return -ENOMEM;
  1175. tim = (struct p54_tim *) skb_put(skb, sizeof(*tim));
  1176. tim->count = 1;
  1177. tim->entry[0] = cpu_to_le16(set ? (sta->aid | 0x8000) : sta->aid);
  1178. priv->tx(dev, skb);
  1179. return 0;
  1180. }
  1181. static int p54_sta_unlock(struct ieee80211_hw *dev, u8 *addr)
  1182. {
  1183. struct p54_common *priv = dev->priv;
  1184. struct sk_buff *skb;
  1185. struct p54_sta_unlock *sta;
  1186. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*sta),
  1187. P54_CONTROL_TYPE_PSM_STA_UNLOCK, GFP_ATOMIC);
  1188. if (!skb)
  1189. return -ENOMEM;
  1190. sta = (struct p54_sta_unlock *)skb_put(skb, sizeof(*sta));
  1191. memcpy(sta->addr, addr, ETH_ALEN);
  1192. priv->tx(dev, skb);
  1193. return 0;
  1194. }
  1195. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  1196. enum sta_notify_cmd notify_cmd,
  1197. struct ieee80211_sta *sta)
  1198. {
  1199. switch (notify_cmd) {
  1200. case STA_NOTIFY_ADD:
  1201. case STA_NOTIFY_REMOVE:
  1202. /*
  1203. * Notify the firmware that we don't want or we don't
  1204. * need to buffer frames for this station anymore.
  1205. */
  1206. p54_sta_unlock(dev, sta->addr);
  1207. break;
  1208. case STA_NOTIFY_AWAKE:
  1209. /* update the firmware's filter table */
  1210. p54_sta_unlock(dev, sta->addr);
  1211. break;
  1212. default:
  1213. break;
  1214. }
  1215. }
  1216. static int p54_tx_cancel(struct ieee80211_hw *dev, struct sk_buff *entry)
  1217. {
  1218. struct p54_common *priv = dev->priv;
  1219. struct sk_buff *skb;
  1220. struct p54_hdr *hdr;
  1221. struct p54_txcancel *cancel;
  1222. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*cancel),
  1223. P54_CONTROL_TYPE_TXCANCEL, GFP_ATOMIC);
  1224. if (!skb)
  1225. return -ENOMEM;
  1226. hdr = (void *)entry->data;
  1227. cancel = (struct p54_txcancel *)skb_put(skb, sizeof(*cancel));
  1228. cancel->req_id = hdr->req_id;
  1229. priv->tx(dev, skb);
  1230. return 0;
  1231. }
  1232. static int p54_tx_fill(struct ieee80211_hw *dev, struct sk_buff *skb,
  1233. struct ieee80211_tx_info *info, u8 *queue, size_t *extra_len,
  1234. u16 *flags, u16 *aid)
  1235. {
  1236. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1237. struct p54_common *priv = dev->priv;
  1238. int ret = 1;
  1239. switch (priv->mode) {
  1240. case NL80211_IFTYPE_MONITOR:
  1241. /*
  1242. * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
  1243. * every frame in promiscuous/monitor mode.
  1244. * see STSW45x0C LMAC API - page 12.
  1245. */
  1246. *aid = 0;
  1247. *flags = P54_HDR_FLAG_DATA_OUT_PROMISC;
  1248. *queue += P54_QUEUE_DATA;
  1249. break;
  1250. case NL80211_IFTYPE_STATION:
  1251. *aid = 1;
  1252. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1253. *queue = P54_QUEUE_MGMT;
  1254. ret = 0;
  1255. } else
  1256. *queue += P54_QUEUE_DATA;
  1257. break;
  1258. case NL80211_IFTYPE_AP:
  1259. case NL80211_IFTYPE_ADHOC:
  1260. case NL80211_IFTYPE_MESH_POINT:
  1261. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1262. *aid = 0;
  1263. *queue = P54_QUEUE_CAB;
  1264. return 0;
  1265. }
  1266. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  1267. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1268. *aid = 0;
  1269. *queue = P54_QUEUE_MGMT;
  1270. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  1271. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1272. return 0;
  1273. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  1274. *aid = 0;
  1275. if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  1276. /*
  1277. * Injecting beacons on top of a AP is
  1278. * not a good idea... nevertheless,
  1279. * it should be doable.
  1280. */
  1281. *queue += P54_QUEUE_DATA;
  1282. return 1;
  1283. }
  1284. *flags = P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  1285. *queue = P54_QUEUE_BEACON;
  1286. *extra_len = IEEE80211_MAX_TIM_LEN;
  1287. return 0;
  1288. } else {
  1289. *queue = P54_QUEUE_MGMT;
  1290. ret = 0;
  1291. }
  1292. } else
  1293. *queue += P54_QUEUE_DATA;
  1294. if (info->control.sta)
  1295. *aid = info->control.sta->aid;
  1296. else
  1297. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  1298. break;
  1299. }
  1300. return ret;
  1301. }
  1302. static u8 p54_convert_algo(enum ieee80211_key_alg alg)
  1303. {
  1304. switch (alg) {
  1305. case ALG_WEP:
  1306. return P54_CRYPTO_WEP;
  1307. case ALG_TKIP:
  1308. return P54_CRYPTO_TKIPMICHAEL;
  1309. case ALG_CCMP:
  1310. return P54_CRYPTO_AESCCMP;
  1311. default:
  1312. return 0;
  1313. }
  1314. }
  1315. static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  1316. {
  1317. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1318. struct ieee80211_tx_queue_stats *current_queue;
  1319. struct p54_common *priv = dev->priv;
  1320. struct p54_hdr *hdr;
  1321. struct p54_tx_data *txhdr;
  1322. size_t padding, len, tim_len = 0;
  1323. int i, j, ridx, ret;
  1324. u16 hdr_flags = 0, aid = 0;
  1325. u8 rate, queue, crypt_offset = 0;
  1326. u8 cts_rate = 0x20;
  1327. u8 rc_flags;
  1328. u8 calculated_tries[4];
  1329. u8 nrates = 0, nremaining = 8;
  1330. queue = skb_get_queue_mapping(skb);
  1331. ret = p54_tx_fill(dev, skb, info, &queue, &tim_len, &hdr_flags, &aid);
  1332. current_queue = &priv->tx_stats[queue];
  1333. if (unlikely((current_queue->len > current_queue->limit) && ret))
  1334. return NETDEV_TX_BUSY;
  1335. current_queue->len++;
  1336. current_queue->count++;
  1337. if ((current_queue->len == current_queue->limit) && ret)
  1338. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  1339. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  1340. len = skb->len;
  1341. if (info->control.hw_key) {
  1342. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  1343. if (info->control.hw_key->alg == ALG_TKIP) {
  1344. u8 *iv = (u8 *)(skb->data + crypt_offset);
  1345. /*
  1346. * The firmware excepts that the IV has to have
  1347. * this special format
  1348. */
  1349. iv[1] = iv[0];
  1350. iv[0] = iv[2];
  1351. iv[2] = 0;
  1352. }
  1353. }
  1354. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  1355. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  1356. if (padding)
  1357. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  1358. hdr->type = cpu_to_le16(aid);
  1359. hdr->rts_tries = info->control.rates[0].count;
  1360. /*
  1361. * we register the rates in perfect order, and
  1362. * RTS/CTS won't happen on 5 GHz
  1363. */
  1364. cts_rate = info->control.rts_cts_rate_idx;
  1365. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  1366. /* see how many rates got used */
  1367. for (i = 0; i < 4; i++) {
  1368. if (info->control.rates[i].idx < 0)
  1369. break;
  1370. nrates++;
  1371. }
  1372. /* limit tries to 8/nrates per rate */
  1373. for (i = 0; i < nrates; i++) {
  1374. /*
  1375. * The magic expression here is equivalent to 8/nrates for
  1376. * all values that matter, but avoids division and jumps.
  1377. * Note that nrates can only take the values 1 through 4.
  1378. */
  1379. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  1380. info->control.rates[i].count);
  1381. nremaining -= calculated_tries[i];
  1382. }
  1383. /* if there are tries left, distribute from back to front */
  1384. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  1385. int tmp = info->control.rates[i].count - calculated_tries[i];
  1386. if (tmp <= 0)
  1387. continue;
  1388. /* RC requested more tries at this rate */
  1389. tmp = min_t(int, tmp, nremaining);
  1390. calculated_tries[i] += tmp;
  1391. nremaining -= tmp;
  1392. }
  1393. ridx = 0;
  1394. for (i = 0; i < nrates && ridx < 8; i++) {
  1395. /* we register the rates in perfect order */
  1396. rate = info->control.rates[i].idx;
  1397. if (info->band == IEEE80211_BAND_5GHZ)
  1398. rate += 4;
  1399. /* store the count we actually calculated for TX status */
  1400. info->control.rates[i].count = calculated_tries[i];
  1401. rc_flags = info->control.rates[i].flags;
  1402. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  1403. rate |= 0x10;
  1404. cts_rate |= 0x10;
  1405. }
  1406. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1407. rate |= 0x40;
  1408. else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1409. rate |= 0x20;
  1410. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  1411. txhdr->rateset[ridx] = rate;
  1412. ridx++;
  1413. }
  1414. }
  1415. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  1416. hdr_flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  1417. /* TODO: enable bursting */
  1418. hdr->flags = cpu_to_le16(hdr_flags);
  1419. hdr->tries = ridx;
  1420. txhdr->rts_rate_idx = 0;
  1421. if (info->control.hw_key) {
  1422. txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
  1423. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  1424. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  1425. if (info->control.hw_key->alg == ALG_TKIP) {
  1426. if (unlikely(skb_tailroom(skb) < 12))
  1427. goto err;
  1428. /* reserve space for the MIC key */
  1429. len += 8;
  1430. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  1431. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  1432. }
  1433. /* reserve some space for ICV */
  1434. len += info->control.hw_key->icv_len;
  1435. memset(skb_put(skb, info->control.hw_key->icv_len), 0,
  1436. info->control.hw_key->icv_len);
  1437. } else {
  1438. txhdr->key_type = 0;
  1439. txhdr->key_len = 0;
  1440. }
  1441. txhdr->crypt_offset = crypt_offset;
  1442. txhdr->hw_queue = queue;
  1443. txhdr->backlog = current_queue->len;
  1444. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  1445. txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
  1446. 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
  1447. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1448. txhdr->longbow.cts_rate = cts_rate;
  1449. txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
  1450. } else {
  1451. txhdr->normal.output_power = priv->output_power;
  1452. txhdr->normal.cts_rate = cts_rate;
  1453. }
  1454. if (padding)
  1455. txhdr->align[0] = padding;
  1456. hdr->len = cpu_to_le16(len);
  1457. /* modifies skb->cb and with it info, so must be last! */
  1458. if (unlikely(p54_assign_address(dev, skb, hdr, skb->len + tim_len)))
  1459. goto err;
  1460. priv->tx(dev, skb);
  1461. queue_delayed_work(dev->workqueue, &priv->work,
  1462. msecs_to_jiffies(P54_TX_FRAME_LIFETIME));
  1463. return NETDEV_TX_OK;
  1464. err:
  1465. skb_pull(skb, sizeof(*hdr) + sizeof(*txhdr) + padding);
  1466. current_queue->len--;
  1467. current_queue->count--;
  1468. return NETDEV_TX_BUSY;
  1469. }
  1470. static int p54_setup_mac(struct ieee80211_hw *dev)
  1471. {
  1472. struct p54_common *priv = dev->priv;
  1473. struct sk_buff *skb;
  1474. struct p54_setup_mac *setup;
  1475. u16 mode;
  1476. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*setup),
  1477. P54_CONTROL_TYPE_SETUP, GFP_ATOMIC);
  1478. if (!skb)
  1479. return -ENOMEM;
  1480. setup = (struct p54_setup_mac *) skb_put(skb, sizeof(*setup));
  1481. if (dev->conf.radio_enabled) {
  1482. switch (priv->mode) {
  1483. case NL80211_IFTYPE_STATION:
  1484. mode = P54_FILTER_TYPE_STATION;
  1485. break;
  1486. case NL80211_IFTYPE_AP:
  1487. mode = P54_FILTER_TYPE_AP;
  1488. break;
  1489. case NL80211_IFTYPE_ADHOC:
  1490. case NL80211_IFTYPE_MESH_POINT:
  1491. mode = P54_FILTER_TYPE_IBSS;
  1492. break;
  1493. case NL80211_IFTYPE_MONITOR:
  1494. mode = P54_FILTER_TYPE_PROMISCUOUS;
  1495. break;
  1496. default:
  1497. mode = P54_FILTER_TYPE_HIBERNATE;
  1498. break;
  1499. }
  1500. /*
  1501. * "TRANSPARENT and PROMISCUOUS are mutually exclusive"
  1502. * STSW45X0C LMAC API - page 12
  1503. */
  1504. if (((priv->filter_flags & FIF_PROMISC_IN_BSS) ||
  1505. (priv->filter_flags & FIF_OTHER_BSS)) &&
  1506. (mode != P54_FILTER_TYPE_PROMISCUOUS))
  1507. mode |= P54_FILTER_TYPE_TRANSPARENT;
  1508. } else
  1509. mode = P54_FILTER_TYPE_HIBERNATE;
  1510. setup->mac_mode = cpu_to_le16(mode);
  1511. memcpy(setup->mac_addr, priv->mac_addr, ETH_ALEN);
  1512. memcpy(setup->bssid, priv->bssid, ETH_ALEN);
  1513. setup->rx_antenna = 2 & priv->rx_diversity_mask; /* automatic */
  1514. setup->rx_align = 0;
  1515. if (priv->fw_var < 0x500) {
  1516. setup->v1.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1517. memset(setup->v1.rts_rates, 0, 8);
  1518. setup->v1.rx_addr = cpu_to_le32(priv->rx_end);
  1519. setup->v1.max_rx = cpu_to_le16(priv->rx_mtu);
  1520. setup->v1.rxhw = cpu_to_le16(priv->rxhw);
  1521. setup->v1.wakeup_timer = cpu_to_le16(priv->wakeup_timer);
  1522. setup->v1.unalloc0 = cpu_to_le16(0);
  1523. } else {
  1524. setup->v2.rx_addr = cpu_to_le32(priv->rx_end);
  1525. setup->v2.max_rx = cpu_to_le16(priv->rx_mtu);
  1526. setup->v2.rxhw = cpu_to_le16(priv->rxhw);
  1527. setup->v2.timer = cpu_to_le16(priv->wakeup_timer);
  1528. setup->v2.truncate = cpu_to_le16(48896);
  1529. setup->v2.basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1530. setup->v2.sbss_offset = 0;
  1531. setup->v2.mcast_window = 0;
  1532. setup->v2.rx_rssi_threshold = 0;
  1533. setup->v2.rx_ed_threshold = 0;
  1534. setup->v2.ref_clock = cpu_to_le32(644245094);
  1535. setup->v2.lpf_bandwidth = cpu_to_le16(65535);
  1536. setup->v2.osc_start_delay = cpu_to_le16(65535);
  1537. }
  1538. priv->tx(dev, skb);
  1539. return 0;
  1540. }
  1541. static int p54_scan(struct ieee80211_hw *dev, u16 mode, u16 dwell)
  1542. {
  1543. struct p54_common *priv = dev->priv;
  1544. struct sk_buff *skb;
  1545. struct p54_hdr *hdr;
  1546. struct p54_scan_head *head;
  1547. struct p54_iq_autocal_entry *iq_autocal;
  1548. union p54_scan_body_union *body;
  1549. struct p54_scan_tail_rate *rate;
  1550. struct pda_rssi_cal_entry *rssi;
  1551. unsigned int i;
  1552. void *entry;
  1553. int band = dev->conf.channel->band;
  1554. __le16 freq = cpu_to_le16(dev->conf.channel->center_freq);
  1555. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*head) +
  1556. 2 + sizeof(*iq_autocal) + sizeof(*body) +
  1557. sizeof(*rate) + 2 * sizeof(*rssi),
  1558. P54_CONTROL_TYPE_SCAN, GFP_ATOMIC);
  1559. if (!skb)
  1560. return -ENOMEM;
  1561. head = (struct p54_scan_head *) skb_put(skb, sizeof(*head));
  1562. memset(head->scan_params, 0, sizeof(head->scan_params));
  1563. head->mode = cpu_to_le16(mode);
  1564. head->dwell = cpu_to_le16(dwell);
  1565. head->freq = freq;
  1566. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1567. __le16 *pa_power_points = (__le16 *) skb_put(skb, 2);
  1568. *pa_power_points = cpu_to_le16(0x0c);
  1569. }
  1570. iq_autocal = (void *) skb_put(skb, sizeof(*iq_autocal));
  1571. for (i = 0; i < priv->iq_autocal_len; i++) {
  1572. if (priv->iq_autocal[i].freq != freq)
  1573. continue;
  1574. memcpy(iq_autocal, &priv->iq_autocal[i].params,
  1575. sizeof(struct p54_iq_autocal_entry));
  1576. break;
  1577. }
  1578. if (i == priv->iq_autocal_len)
  1579. goto err;
  1580. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW)
  1581. body = (void *) skb_put(skb, sizeof(body->longbow));
  1582. else
  1583. body = (void *) skb_put(skb, sizeof(body->normal));
  1584. for (i = 0; i < priv->output_limit->entries; i++) {
  1585. __le16 *entry_freq = (void *) (priv->output_limit->data +
  1586. priv->output_limit->entry_size * i);
  1587. if (*entry_freq != freq)
  1588. continue;
  1589. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1590. memcpy(&body->longbow.power_limits,
  1591. (void *) entry_freq + sizeof(__le16),
  1592. priv->output_limit->entry_size);
  1593. } else {
  1594. struct pda_channel_output_limit *limits =
  1595. (void *) entry_freq;
  1596. body->normal.val_barker = 0x38;
  1597. body->normal.val_bpsk = body->normal.dup_bpsk =
  1598. limits->val_bpsk;
  1599. body->normal.val_qpsk = body->normal.dup_qpsk =
  1600. limits->val_qpsk;
  1601. body->normal.val_16qam = body->normal.dup_16qam =
  1602. limits->val_16qam;
  1603. body->normal.val_64qam = body->normal.dup_64qam =
  1604. limits->val_64qam;
  1605. }
  1606. break;
  1607. }
  1608. if (i == priv->output_limit->entries)
  1609. goto err;
  1610. entry = (void *)(priv->curve_data->data + priv->curve_data->offset);
  1611. for (i = 0; i < priv->curve_data->entries; i++) {
  1612. if (*((__le16 *)entry) != freq) {
  1613. entry += priv->curve_data->entry_size;
  1614. continue;
  1615. }
  1616. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1617. memcpy(&body->longbow.curve_data,
  1618. (void *) entry + sizeof(__le16),
  1619. priv->curve_data->entry_size);
  1620. } else {
  1621. struct p54_scan_body *chan = &body->normal;
  1622. struct pda_pa_curve_data *curve_data =
  1623. (void *) priv->curve_data->data;
  1624. entry += sizeof(__le16);
  1625. chan->pa_points_per_curve = 8;
  1626. memset(chan->curve_data, 0, sizeof(*chan->curve_data));
  1627. memcpy(chan->curve_data, entry,
  1628. sizeof(struct p54_pa_curve_data_sample) *
  1629. min((u8)8, curve_data->points_per_channel));
  1630. }
  1631. break;
  1632. }
  1633. if (i == priv->curve_data->entries)
  1634. goto err;
  1635. if ((priv->fw_var >= 0x500) && (priv->fw_var < 0x509)) {
  1636. rate = (void *) skb_put(skb, sizeof(*rate));
  1637. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1638. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1639. rate->rts_rates[i] = i;
  1640. }
  1641. rssi = (struct pda_rssi_cal_entry *) skb_put(skb, sizeof(*rssi));
  1642. rssi->mul = cpu_to_le16(priv->rssical_db[band].mul);
  1643. rssi->add = cpu_to_le16(priv->rssical_db[band].add);
  1644. if (priv->rxhw == PDR_SYNTH_FRONTEND_LONGBOW) {
  1645. /* Longbow frontend needs ever more */
  1646. rssi = (void *) skb_put(skb, sizeof(*rssi));
  1647. rssi->mul = cpu_to_le16(priv->rssical_db[band].longbow_unkn);
  1648. rssi->add = cpu_to_le16(priv->rssical_db[band].longbow_unk2);
  1649. }
  1650. if (priv->fw_var >= 0x509) {
  1651. rate = (void *) skb_put(skb, sizeof(*rate));
  1652. rate->basic_rate_mask = cpu_to_le32(priv->basic_rate_mask);
  1653. for (i = 0; i < sizeof(rate->rts_rates); i++)
  1654. rate->rts_rates[i] = i;
  1655. }
  1656. hdr = (struct p54_hdr *) skb->data;
  1657. hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
  1658. priv->tx(dev, skb);
  1659. return 0;
  1660. err:
  1661. printk(KERN_ERR "%s: frequency change failed\n", wiphy_name(dev->wiphy));
  1662. p54_free_skb(dev, skb);
  1663. return -EINVAL;
  1664. }
  1665. static int p54_set_leds(struct ieee80211_hw *dev)
  1666. {
  1667. struct p54_common *priv = dev->priv;
  1668. struct sk_buff *skb;
  1669. struct p54_led *led;
  1670. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*led),
  1671. P54_CONTROL_TYPE_LED, GFP_ATOMIC);
  1672. if (!skb)
  1673. return -ENOMEM;
  1674. led = (struct p54_led *) skb_put(skb, sizeof(*led));
  1675. led->flags = cpu_to_le16(0x0003);
  1676. led->mask[0] = led->mask[1] = cpu_to_le16(priv->softled_state);
  1677. led->delay[0] = cpu_to_le16(1);
  1678. led->delay[1] = cpu_to_le16(0);
  1679. priv->tx(dev, skb);
  1680. return 0;
  1681. }
  1682. #define P54_SET_QUEUE(queue, ai_fs, cw_min, cw_max, _txop) \
  1683. do { \
  1684. queue.aifs = cpu_to_le16(ai_fs); \
  1685. queue.cwmin = cpu_to_le16(cw_min); \
  1686. queue.cwmax = cpu_to_le16(cw_max); \
  1687. queue.txop = cpu_to_le16(_txop); \
  1688. } while(0)
  1689. static int p54_set_edcf(struct ieee80211_hw *dev)
  1690. {
  1691. struct p54_common *priv = dev->priv;
  1692. struct sk_buff *skb;
  1693. struct p54_edcf *edcf;
  1694. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*edcf),
  1695. P54_CONTROL_TYPE_DCFINIT, GFP_ATOMIC);
  1696. if (!skb)
  1697. return -ENOMEM;
  1698. edcf = (struct p54_edcf *)skb_put(skb, sizeof(*edcf));
  1699. if (priv->use_short_slot) {
  1700. edcf->slottime = 9;
  1701. edcf->sifs = 0x10;
  1702. edcf->eofpad = 0x00;
  1703. } else {
  1704. edcf->slottime = 20;
  1705. edcf->sifs = 0x0a;
  1706. edcf->eofpad = 0x06;
  1707. }
  1708. /* (see prism54/isl_oid.h for further details) */
  1709. edcf->frameburst = cpu_to_le16(0);
  1710. edcf->round_trip_delay = cpu_to_le16(0);
  1711. edcf->flags = 0;
  1712. memset(edcf->mapping, 0, sizeof(edcf->mapping));
  1713. memcpy(edcf->queue, priv->qos_params, sizeof(edcf->queue));
  1714. priv->tx(dev, skb);
  1715. return 0;
  1716. }
  1717. static int p54_set_ps(struct ieee80211_hw *dev)
  1718. {
  1719. struct p54_common *priv = dev->priv;
  1720. struct sk_buff *skb;
  1721. struct p54_psm *psm;
  1722. u16 mode;
  1723. int i;
  1724. if (dev->conf.flags & IEEE80211_CONF_PS)
  1725. mode = P54_PSM | P54_PSM_BEACON_TIMEOUT | P54_PSM_DTIM |
  1726. P54_PSM_CHECKSUM | P54_PSM_MCBC;
  1727. else
  1728. mode = P54_PSM_CAM;
  1729. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*psm),
  1730. P54_CONTROL_TYPE_PSM, GFP_ATOMIC);
  1731. if (!skb)
  1732. return -ENOMEM;
  1733. psm = (struct p54_psm *)skb_put(skb, sizeof(*psm));
  1734. psm->mode = cpu_to_le16(mode);
  1735. psm->aid = cpu_to_le16(priv->aid);
  1736. for (i = 0; i < ARRAY_SIZE(psm->intervals); i++) {
  1737. psm->intervals[i].interval =
  1738. cpu_to_le16(dev->conf.listen_interval);
  1739. psm->intervals[i].periods = cpu_to_le16(1);
  1740. }
  1741. psm->beacon_rssi_skip_max = 200;
  1742. psm->rssi_delta_threshold = 0;
  1743. psm->nr = 10;
  1744. psm->exclude[0] = 0;
  1745. priv->tx(dev, skb);
  1746. return 0;
  1747. }
  1748. static int p54_beacon_tim(struct sk_buff *skb)
  1749. {
  1750. /*
  1751. * the good excuse for this mess is ... the firmware.
  1752. * The dummy TIM MUST be at the end of the beacon frame,
  1753. * because it'll be overwritten!
  1754. */
  1755. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1756. u8 *pos, *end;
  1757. if (skb->len <= sizeof(mgmt))
  1758. return -EINVAL;
  1759. pos = (u8 *)mgmt->u.beacon.variable;
  1760. end = skb->data + skb->len;
  1761. while (pos < end) {
  1762. if (pos + 2 + pos[1] > end)
  1763. return -EINVAL;
  1764. if (pos[0] == WLAN_EID_TIM) {
  1765. u8 dtim_len = pos[1];
  1766. u8 dtim_period = pos[3];
  1767. u8 *next = pos + 2 + dtim_len;
  1768. if (dtim_len < 3)
  1769. return -EINVAL;
  1770. memmove(pos, next, end - next);
  1771. if (dtim_len > 3)
  1772. skb_trim(skb, skb->len - (dtim_len - 3));
  1773. pos = end - (dtim_len + 2);
  1774. /* add the dummy at the end */
  1775. pos[0] = WLAN_EID_TIM;
  1776. pos[1] = 3;
  1777. pos[2] = 0;
  1778. pos[3] = dtim_period;
  1779. pos[4] = 0;
  1780. return 0;
  1781. }
  1782. pos += 2 + pos[1];
  1783. }
  1784. return 0;
  1785. }
  1786. static int p54_beacon_update(struct ieee80211_hw *dev,
  1787. struct ieee80211_vif *vif)
  1788. {
  1789. struct p54_common *priv = dev->priv;
  1790. struct sk_buff *beacon;
  1791. int ret;
  1792. if (priv->cached_beacon) {
  1793. p54_tx_cancel(dev, priv->cached_beacon);
  1794. /* wait for the last beacon the be freed */
  1795. msleep(10);
  1796. }
  1797. beacon = ieee80211_beacon_get(dev, vif);
  1798. if (!beacon)
  1799. return -ENOMEM;
  1800. ret = p54_beacon_tim(beacon);
  1801. if (ret)
  1802. return ret;
  1803. ret = p54_tx(dev, beacon);
  1804. if (ret)
  1805. return ret;
  1806. priv->cached_beacon = beacon;
  1807. priv->tsf_high32 = 0;
  1808. priv->tsf_low32 = 0;
  1809. return 0;
  1810. }
  1811. static int p54_start(struct ieee80211_hw *dev)
  1812. {
  1813. struct p54_common *priv = dev->priv;
  1814. int err;
  1815. mutex_lock(&priv->conf_mutex);
  1816. err = priv->open(dev);
  1817. if (err)
  1818. goto out;
  1819. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  1820. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  1821. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  1822. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  1823. err = p54_set_edcf(dev);
  1824. if (err)
  1825. goto out;
  1826. memset(priv->bssid, ~0, ETH_ALEN);
  1827. priv->mode = NL80211_IFTYPE_MONITOR;
  1828. err = p54_setup_mac(dev);
  1829. if (err) {
  1830. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1831. goto out;
  1832. }
  1833. queue_delayed_work(dev->workqueue, &priv->work, 0);
  1834. priv->softled_state = 0;
  1835. err = p54_set_leds(dev);
  1836. out:
  1837. mutex_unlock(&priv->conf_mutex);
  1838. return err;
  1839. }
  1840. static void p54_stop(struct ieee80211_hw *dev)
  1841. {
  1842. struct p54_common *priv = dev->priv;
  1843. struct sk_buff *skb;
  1844. mutex_lock(&priv->conf_mutex);
  1845. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  1846. priv->softled_state = 0;
  1847. p54_set_leds(dev);
  1848. #ifdef CONFIG_P54_LEDS
  1849. cancel_delayed_work_sync(&priv->led_work);
  1850. #endif /* CONFIG_P54_LEDS */
  1851. cancel_delayed_work_sync(&priv->work);
  1852. if (priv->cached_beacon)
  1853. p54_tx_cancel(dev, priv->cached_beacon);
  1854. priv->stop(dev);
  1855. while ((skb = skb_dequeue(&priv->tx_queue)))
  1856. kfree_skb(skb);
  1857. priv->cached_beacon = NULL;
  1858. priv->tsf_high32 = priv->tsf_low32 = 0;
  1859. mutex_unlock(&priv->conf_mutex);
  1860. }
  1861. static int p54_add_interface(struct ieee80211_hw *dev,
  1862. struct ieee80211_if_init_conf *conf)
  1863. {
  1864. struct p54_common *priv = dev->priv;
  1865. mutex_lock(&priv->conf_mutex);
  1866. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  1867. mutex_unlock(&priv->conf_mutex);
  1868. return -EOPNOTSUPP;
  1869. }
  1870. priv->vif = conf->vif;
  1871. switch (conf->type) {
  1872. case NL80211_IFTYPE_STATION:
  1873. case NL80211_IFTYPE_ADHOC:
  1874. case NL80211_IFTYPE_AP:
  1875. case NL80211_IFTYPE_MESH_POINT:
  1876. priv->mode = conf->type;
  1877. break;
  1878. default:
  1879. mutex_unlock(&priv->conf_mutex);
  1880. return -EOPNOTSUPP;
  1881. }
  1882. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  1883. p54_setup_mac(dev);
  1884. mutex_unlock(&priv->conf_mutex);
  1885. return 0;
  1886. }
  1887. static void p54_remove_interface(struct ieee80211_hw *dev,
  1888. struct ieee80211_if_init_conf *conf)
  1889. {
  1890. struct p54_common *priv = dev->priv;
  1891. mutex_lock(&priv->conf_mutex);
  1892. priv->vif = NULL;
  1893. if (priv->cached_beacon)
  1894. p54_tx_cancel(dev, priv->cached_beacon);
  1895. priv->mode = NL80211_IFTYPE_MONITOR;
  1896. memset(priv->mac_addr, 0, ETH_ALEN);
  1897. memset(priv->bssid, 0, ETH_ALEN);
  1898. p54_setup_mac(dev);
  1899. mutex_unlock(&priv->conf_mutex);
  1900. }
  1901. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  1902. {
  1903. int ret = 0;
  1904. struct p54_common *priv = dev->priv;
  1905. struct ieee80211_conf *conf = &dev->conf;
  1906. mutex_lock(&priv->conf_mutex);
  1907. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1908. priv->output_power = conf->power_level << 2;
  1909. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  1910. ret = p54_setup_mac(dev);
  1911. if (ret)
  1912. goto out;
  1913. }
  1914. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1915. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1916. if (ret)
  1917. goto out;
  1918. }
  1919. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1920. ret = p54_set_ps(dev);
  1921. if (ret)
  1922. goto out;
  1923. }
  1924. out:
  1925. mutex_unlock(&priv->conf_mutex);
  1926. return ret;
  1927. }
  1928. static int p54_config_interface(struct ieee80211_hw *dev,
  1929. struct ieee80211_vif *vif,
  1930. struct ieee80211_if_conf *conf)
  1931. {
  1932. struct p54_common *priv = dev->priv;
  1933. int ret = 0;
  1934. mutex_lock(&priv->conf_mutex);
  1935. if (conf->changed & IEEE80211_IFCC_BSSID) {
  1936. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1937. ret = p54_setup_mac(dev);
  1938. if (ret)
  1939. goto out;
  1940. }
  1941. if (conf->changed & IEEE80211_IFCC_BEACON) {
  1942. ret = p54_scan(dev, P54_SCAN_EXIT, 0);
  1943. if (ret)
  1944. goto out;
  1945. ret = p54_setup_mac(dev);
  1946. if (ret)
  1947. goto out;
  1948. ret = p54_beacon_update(dev, vif);
  1949. if (ret)
  1950. goto out;
  1951. ret = p54_set_edcf(dev);
  1952. if (ret)
  1953. goto out;
  1954. }
  1955. out:
  1956. mutex_unlock(&priv->conf_mutex);
  1957. return ret;
  1958. }
  1959. static void p54_configure_filter(struct ieee80211_hw *dev,
  1960. unsigned int changed_flags,
  1961. unsigned int *total_flags,
  1962. int mc_count, struct dev_mc_list *mclist)
  1963. {
  1964. struct p54_common *priv = dev->priv;
  1965. *total_flags &= FIF_PROMISC_IN_BSS |
  1966. FIF_OTHER_BSS;
  1967. priv->filter_flags = *total_flags;
  1968. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
  1969. p54_setup_mac(dev);
  1970. }
  1971. static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1972. const struct ieee80211_tx_queue_params *params)
  1973. {
  1974. struct p54_common *priv = dev->priv;
  1975. int ret;
  1976. mutex_lock(&priv->conf_mutex);
  1977. if ((params) && !(queue > 4)) {
  1978. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  1979. params->cw_min, params->cw_max, params->txop);
  1980. ret = p54_set_edcf(dev);
  1981. } else
  1982. ret = -EINVAL;
  1983. mutex_unlock(&priv->conf_mutex);
  1984. return ret;
  1985. }
  1986. static int p54_init_xbow_synth(struct ieee80211_hw *dev)
  1987. {
  1988. struct p54_common *priv = dev->priv;
  1989. struct sk_buff *skb;
  1990. struct p54_xbow_synth *xbow;
  1991. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*xbow),
  1992. P54_CONTROL_TYPE_XBOW_SYNTH_CFG, GFP_KERNEL);
  1993. if (!skb)
  1994. return -ENOMEM;
  1995. xbow = (struct p54_xbow_synth *)skb_put(skb, sizeof(*xbow));
  1996. xbow->magic1 = cpu_to_le16(0x1);
  1997. xbow->magic2 = cpu_to_le16(0x2);
  1998. xbow->freq = cpu_to_le16(5390);
  1999. memset(xbow->padding, 0, sizeof(xbow->padding));
  2000. priv->tx(dev, skb);
  2001. return 0;
  2002. }
  2003. static void p54_work(struct work_struct *work)
  2004. {
  2005. struct p54_common *priv = container_of(work, struct p54_common,
  2006. work.work);
  2007. struct ieee80211_hw *dev = priv->hw;
  2008. struct sk_buff *skb;
  2009. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  2010. return ;
  2011. /*
  2012. * TODO: walk through tx_queue and do the following tasks
  2013. * 1. initiate bursts.
  2014. * 2. cancel stuck frames / reset the device if necessary.
  2015. */
  2016. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL,
  2017. sizeof(struct p54_statistics),
  2018. P54_CONTROL_TYPE_STAT_READBACK, GFP_KERNEL);
  2019. if (!skb)
  2020. return ;
  2021. priv->tx(dev, skb);
  2022. }
  2023. static int p54_get_stats(struct ieee80211_hw *dev,
  2024. struct ieee80211_low_level_stats *stats)
  2025. {
  2026. struct p54_common *priv = dev->priv;
  2027. memcpy(stats, &priv->stats, sizeof(*stats));
  2028. return 0;
  2029. }
  2030. static int p54_get_tx_stats(struct ieee80211_hw *dev,
  2031. struct ieee80211_tx_queue_stats *stats)
  2032. {
  2033. struct p54_common *priv = dev->priv;
  2034. memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
  2035. sizeof(stats[0]) * dev->queues);
  2036. return 0;
  2037. }
  2038. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  2039. struct ieee80211_vif *vif,
  2040. struct ieee80211_bss_conf *info,
  2041. u32 changed)
  2042. {
  2043. struct p54_common *priv = dev->priv;
  2044. if (changed & BSS_CHANGED_ERP_SLOT) {
  2045. priv->use_short_slot = info->use_short_slot;
  2046. p54_set_edcf(dev);
  2047. }
  2048. if (changed & BSS_CHANGED_BASIC_RATES) {
  2049. if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
  2050. priv->basic_rate_mask = (info->basic_rates << 4);
  2051. else
  2052. priv->basic_rate_mask = info->basic_rates;
  2053. p54_setup_mac(dev);
  2054. if (priv->fw_var >= 0x500)
  2055. p54_scan(dev, P54_SCAN_EXIT, 0);
  2056. }
  2057. if (changed & BSS_CHANGED_ASSOC) {
  2058. if (info->assoc) {
  2059. priv->aid = info->aid;
  2060. priv->wakeup_timer = info->beacon_int *
  2061. info->dtim_period * 5;
  2062. p54_setup_mac(dev);
  2063. }
  2064. }
  2065. }
  2066. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  2067. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2068. struct ieee80211_key_conf *key)
  2069. {
  2070. struct p54_common *priv = dev->priv;
  2071. struct sk_buff *skb;
  2072. struct p54_keycache *rxkey;
  2073. int slot, ret = 0;
  2074. u8 algo = 0;
  2075. if (modparam_nohwcrypt)
  2076. return -EOPNOTSUPP;
  2077. mutex_lock(&priv->conf_mutex);
  2078. if (cmd == SET_KEY) {
  2079. switch (key->alg) {
  2080. case ALG_TKIP:
  2081. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  2082. BR_DESC_PRIV_CAP_TKIP))) {
  2083. ret = -EOPNOTSUPP;
  2084. goto out_unlock;
  2085. }
  2086. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2087. algo = P54_CRYPTO_TKIPMICHAEL;
  2088. break;
  2089. case ALG_WEP:
  2090. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
  2091. ret = -EOPNOTSUPP;
  2092. goto out_unlock;
  2093. }
  2094. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2095. algo = P54_CRYPTO_WEP;
  2096. break;
  2097. case ALG_CCMP:
  2098. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
  2099. ret = -EOPNOTSUPP;
  2100. goto out_unlock;
  2101. }
  2102. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2103. algo = P54_CRYPTO_AESCCMP;
  2104. break;
  2105. default:
  2106. ret = -EOPNOTSUPP;
  2107. goto out_unlock;
  2108. }
  2109. slot = bitmap_find_free_region(priv->used_rxkeys,
  2110. priv->rx_keycache_size, 0);
  2111. if (slot < 0) {
  2112. /*
  2113. * The device supports the choosen algorithm, but the
  2114. * firmware does not provide enough key slots to store
  2115. * all of them.
  2116. * But encryption offload for outgoing frames is always
  2117. * possible, so we just pretend that the upload was
  2118. * successful and do the decryption in software.
  2119. */
  2120. /* mark the key as invalid. */
  2121. key->hw_key_idx = 0xff;
  2122. goto out_unlock;
  2123. }
  2124. } else {
  2125. slot = key->hw_key_idx;
  2126. if (slot == 0xff) {
  2127. /* This key was not uploaded into the rx key cache. */
  2128. goto out_unlock;
  2129. }
  2130. bitmap_release_region(priv->used_rxkeys, slot, 0);
  2131. algo = 0;
  2132. }
  2133. skb = p54_alloc_skb(dev, P54_HDR_FLAG_CONTROL_OPSET, sizeof(*rxkey),
  2134. P54_CONTROL_TYPE_RX_KEYCACHE, GFP_KERNEL);
  2135. if (!skb) {
  2136. bitmap_release_region(priv->used_rxkeys, slot, 0);
  2137. ret = -ENOSPC;
  2138. goto out_unlock;
  2139. }
  2140. rxkey = (struct p54_keycache *)skb_put(skb, sizeof(*rxkey));
  2141. rxkey->entry = slot;
  2142. rxkey->key_id = key->keyidx;
  2143. rxkey->key_type = algo;
  2144. if (sta)
  2145. memcpy(rxkey->mac, sta->addr, ETH_ALEN);
  2146. else
  2147. memset(rxkey->mac, ~0, ETH_ALEN);
  2148. if (key->alg != ALG_TKIP) {
  2149. rxkey->key_len = min((u8)16, key->keylen);
  2150. memcpy(rxkey->key, key->key, rxkey->key_len);
  2151. } else {
  2152. rxkey->key_len = 24;
  2153. memcpy(rxkey->key, key->key, 16);
  2154. memcpy(&(rxkey->key[16]), &(key->key
  2155. [NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]), 8);
  2156. }
  2157. priv->tx(dev, skb);
  2158. key->hw_key_idx = slot;
  2159. out_unlock:
  2160. mutex_unlock(&priv->conf_mutex);
  2161. return ret;
  2162. }
  2163. #ifdef CONFIG_P54_LEDS
  2164. static void p54_update_leds(struct work_struct *work)
  2165. {
  2166. struct p54_common *priv = container_of(work, struct p54_common,
  2167. led_work.work);
  2168. int err, i, tmp, blink_delay = 400;
  2169. bool rerun = false;
  2170. /* Don't toggle the LED, when the device is down. */
  2171. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2172. return ;
  2173. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2174. if (priv->leds[i].toggled) {
  2175. priv->softled_state |= BIT(i);
  2176. tmp = 70 + 200 / (priv->leds[i].toggled);
  2177. if (tmp < blink_delay)
  2178. blink_delay = tmp;
  2179. if (priv->leds[i].led_dev.brightness == LED_OFF)
  2180. rerun = true;
  2181. priv->leds[i].toggled =
  2182. !!priv->leds[i].led_dev.brightness;
  2183. } else
  2184. priv->softled_state &= ~BIT(i);
  2185. err = p54_set_leds(priv->hw);
  2186. if (err && net_ratelimit())
  2187. printk(KERN_ERR "%s: failed to update LEDs.\n",
  2188. wiphy_name(priv->hw->wiphy));
  2189. if (rerun)
  2190. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2191. msecs_to_jiffies(blink_delay));
  2192. }
  2193. static void p54_led_brightness_set(struct led_classdev *led_dev,
  2194. enum led_brightness brightness)
  2195. {
  2196. struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
  2197. led_dev);
  2198. struct ieee80211_hw *dev = led->hw_dev;
  2199. struct p54_common *priv = dev->priv;
  2200. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  2201. return ;
  2202. if (brightness) {
  2203. led->toggled++;
  2204. queue_delayed_work(priv->hw->workqueue, &priv->led_work,
  2205. HZ/10);
  2206. }
  2207. }
  2208. static int p54_register_led(struct ieee80211_hw *dev,
  2209. unsigned int led_index,
  2210. char *name, char *trigger)
  2211. {
  2212. struct p54_common *priv = dev->priv;
  2213. struct p54_led_dev *led = &priv->leds[led_index];
  2214. int err;
  2215. if (led->registered)
  2216. return -EEXIST;
  2217. snprintf(led->name, sizeof(led->name), "p54-%s::%s",
  2218. wiphy_name(dev->wiphy), name);
  2219. led->hw_dev = dev;
  2220. led->index = led_index;
  2221. led->led_dev.name = led->name;
  2222. led->led_dev.default_trigger = trigger;
  2223. led->led_dev.brightness_set = p54_led_brightness_set;
  2224. err = led_classdev_register(wiphy_dev(dev->wiphy), &led->led_dev);
  2225. if (err)
  2226. printk(KERN_ERR "%s: Failed to register %s LED.\n",
  2227. wiphy_name(dev->wiphy), name);
  2228. else
  2229. led->registered = 1;
  2230. return err;
  2231. }
  2232. static int p54_init_leds(struct ieee80211_hw *dev)
  2233. {
  2234. struct p54_common *priv = dev->priv;
  2235. int err;
  2236. /*
  2237. * TODO:
  2238. * Figure out if the EEPROM contains some hints about the number
  2239. * of available/programmable LEDs of the device.
  2240. */
  2241. INIT_DELAYED_WORK(&priv->led_work, p54_update_leds);
  2242. err = p54_register_led(dev, 0, "assoc",
  2243. ieee80211_get_assoc_led_name(dev));
  2244. if (err)
  2245. return err;
  2246. err = p54_register_led(dev, 1, "tx",
  2247. ieee80211_get_tx_led_name(dev));
  2248. if (err)
  2249. return err;
  2250. err = p54_register_led(dev, 2, "rx",
  2251. ieee80211_get_rx_led_name(dev));
  2252. if (err)
  2253. return err;
  2254. err = p54_register_led(dev, 3, "radio",
  2255. ieee80211_get_radio_led_name(dev));
  2256. if (err)
  2257. return err;
  2258. err = p54_set_leds(dev);
  2259. return err;
  2260. }
  2261. static void p54_unregister_leds(struct ieee80211_hw *dev)
  2262. {
  2263. struct p54_common *priv = dev->priv;
  2264. int i;
  2265. for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
  2266. if (priv->leds[i].registered)
  2267. led_classdev_unregister(&priv->leds[i].led_dev);
  2268. }
  2269. #endif /* CONFIG_P54_LEDS */
  2270. static const struct ieee80211_ops p54_ops = {
  2271. .tx = p54_tx,
  2272. .start = p54_start,
  2273. .stop = p54_stop,
  2274. .add_interface = p54_add_interface,
  2275. .remove_interface = p54_remove_interface,
  2276. .set_tim = p54_set_tim,
  2277. .sta_notify = p54_sta_notify,
  2278. .set_key = p54_set_key,
  2279. .config = p54_config,
  2280. .config_interface = p54_config_interface,
  2281. .bss_info_changed = p54_bss_info_changed,
  2282. .configure_filter = p54_configure_filter,
  2283. .conf_tx = p54_conf_tx,
  2284. .get_stats = p54_get_stats,
  2285. .get_tx_stats = p54_get_tx_stats
  2286. };
  2287. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  2288. {
  2289. struct ieee80211_hw *dev;
  2290. struct p54_common *priv;
  2291. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  2292. if (!dev)
  2293. return NULL;
  2294. priv = dev->priv;
  2295. priv->hw = dev;
  2296. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  2297. priv->basic_rate_mask = 0x15f;
  2298. skb_queue_head_init(&priv->tx_queue);
  2299. dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2300. IEEE80211_HW_SIGNAL_DBM |
  2301. IEEE80211_HW_NOISE_DBM |
  2302. IEEE80211_HW_BEACON_FILTER;
  2303. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2304. BIT(NL80211_IFTYPE_ADHOC) |
  2305. BIT(NL80211_IFTYPE_AP) |
  2306. BIT(NL80211_IFTYPE_MESH_POINT);
  2307. dev->channel_change_time = 1000; /* TODO: find actual value */
  2308. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  2309. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  2310. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  2311. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  2312. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  2313. dev->queues = 1;
  2314. priv->noise = -94;
  2315. /*
  2316. * We support at most 8 tries no matter which rate they're at,
  2317. * we cannot support max_rates * max_rate_tries as we set it
  2318. * here, but setting it correctly to 4/2 or so would limit us
  2319. * artificially if the RC algorithm wants just two rates, so
  2320. * let's say 4/7, we'll redistribute it at TX time, see the
  2321. * comments there.
  2322. */
  2323. dev->max_rates = 4;
  2324. dev->max_rate_tries = 7;
  2325. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  2326. sizeof(struct p54_tx_data);
  2327. mutex_init(&priv->conf_mutex);
  2328. init_completion(&priv->eeprom_comp);
  2329. INIT_DELAYED_WORK(&priv->work, p54_work);
  2330. return dev;
  2331. }
  2332. EXPORT_SYMBOL_GPL(p54_init_common);
  2333. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  2334. {
  2335. int err;
  2336. err = ieee80211_register_hw(dev);
  2337. if (err) {
  2338. dev_err(pdev, "Cannot register device (%d).\n", err);
  2339. return err;
  2340. }
  2341. #ifdef CONFIG_P54_LEDS
  2342. err = p54_init_leds(dev);
  2343. if (err)
  2344. return err;
  2345. #endif /* CONFIG_P54_LEDS */
  2346. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  2347. return 0;
  2348. }
  2349. EXPORT_SYMBOL_GPL(p54_register_common);
  2350. void p54_free_common(struct ieee80211_hw *dev)
  2351. {
  2352. struct p54_common *priv = dev->priv;
  2353. kfree(priv->iq_autocal);
  2354. kfree(priv->output_limit);
  2355. kfree(priv->curve_data);
  2356. kfree(priv->used_rxkeys);
  2357. #ifdef CONFIG_P54_LEDS
  2358. p54_unregister_leds(dev);
  2359. #endif /* CONFIG_P54_LEDS */
  2360. }
  2361. EXPORT_SYMBOL_GPL(p54_free_common);
  2362. static int __init p54_init(void)
  2363. {
  2364. return 0;
  2365. }
  2366. static void __exit p54_exit(void)
  2367. {
  2368. }
  2369. module_init(p54_init);
  2370. module_exit(p54_exit);