main.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262
  1. /*
  2. Broadcom B43 wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
  4. Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
  5. Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  6. Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  7. Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Some parts of the code in this file are derived from the ipw2200
  9. driver Copyright(c) 2003 - 2004 Intel Corporation.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  21. Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/version.h>
  29. #include <linux/firmware.h>
  30. #include <linux/wireless.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/io.h>
  34. #include <linux/dma-mapping.h>
  35. #include <asm/unaligned.h>
  36. #include "b43.h"
  37. #include "main.h"
  38. #include "debugfs.h"
  39. #include "phy.h"
  40. #include "dma.h"
  41. #include "sysfs.h"
  42. #include "xmit.h"
  43. #include "lo.h"
  44. #include "pcmcia.h"
  45. MODULE_DESCRIPTION("Broadcom B43 wireless driver");
  46. MODULE_AUTHOR("Martin Langer");
  47. MODULE_AUTHOR("Stefano Brivio");
  48. MODULE_AUTHOR("Michael Buesch");
  49. MODULE_LICENSE("GPL");
  50. MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
  51. static int modparam_bad_frames_preempt;
  52. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  53. MODULE_PARM_DESC(bad_frames_preempt,
  54. "enable(1) / disable(0) Bad Frames Preemption");
  55. static char modparam_fwpostfix[16];
  56. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  57. MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
  58. static int modparam_hwpctl;
  59. module_param_named(hwpctl, modparam_hwpctl, int, 0444);
  60. MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
  61. static int modparam_nohwcrypt;
  62. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  63. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  64. static const struct ssb_device_id b43_ssb_tbl[] = {
  65. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
  66. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
  67. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
  68. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
  69. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
  70. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
  71. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
  72. SSB_DEVTABLE_END
  73. };
  74. MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
  75. /* Channel and ratetables are shared for all devices.
  76. * They can't be const, because ieee80211 puts some precalculated
  77. * data in there. This data is the same for all devices, so we don't
  78. * get concurrency issues */
  79. #define RATETAB_ENT(_rateid, _flags) \
  80. { \
  81. .rate = B43_RATE_TO_BASE100KBPS(_rateid), \
  82. .val = (_rateid), \
  83. .val2 = (_rateid), \
  84. .flags = (_flags), \
  85. }
  86. static struct ieee80211_rate __b43_ratetable[] = {
  87. RATETAB_ENT(B43_CCK_RATE_1MB, IEEE80211_RATE_CCK),
  88. RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_CCK_2),
  89. RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_CCK_2),
  90. RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_CCK_2),
  91. RATETAB_ENT(B43_OFDM_RATE_6MB, IEEE80211_RATE_OFDM),
  92. RATETAB_ENT(B43_OFDM_RATE_9MB, IEEE80211_RATE_OFDM),
  93. RATETAB_ENT(B43_OFDM_RATE_12MB, IEEE80211_RATE_OFDM),
  94. RATETAB_ENT(B43_OFDM_RATE_18MB, IEEE80211_RATE_OFDM),
  95. RATETAB_ENT(B43_OFDM_RATE_24MB, IEEE80211_RATE_OFDM),
  96. RATETAB_ENT(B43_OFDM_RATE_36MB, IEEE80211_RATE_OFDM),
  97. RATETAB_ENT(B43_OFDM_RATE_48MB, IEEE80211_RATE_OFDM),
  98. RATETAB_ENT(B43_OFDM_RATE_54MB, IEEE80211_RATE_OFDM),
  99. };
  100. #define b43_a_ratetable (__b43_ratetable + 4)
  101. #define b43_a_ratetable_size 8
  102. #define b43_b_ratetable (__b43_ratetable + 0)
  103. #define b43_b_ratetable_size 4
  104. #define b43_g_ratetable (__b43_ratetable + 0)
  105. #define b43_g_ratetable_size 12
  106. #define CHANTAB_ENT(_chanid, _freq) \
  107. { \
  108. .chan = (_chanid), \
  109. .freq = (_freq), \
  110. .val = (_chanid), \
  111. .flag = IEEE80211_CHAN_W_SCAN | \
  112. IEEE80211_CHAN_W_ACTIVE_SCAN | \
  113. IEEE80211_CHAN_W_IBSS, \
  114. .power_level = 0xFF, \
  115. .antenna_max = 0xFF, \
  116. }
  117. static struct ieee80211_channel b43_2ghz_chantable[] = {
  118. CHANTAB_ENT(1, 2412),
  119. CHANTAB_ENT(2, 2417),
  120. CHANTAB_ENT(3, 2422),
  121. CHANTAB_ENT(4, 2427),
  122. CHANTAB_ENT(5, 2432),
  123. CHANTAB_ENT(6, 2437),
  124. CHANTAB_ENT(7, 2442),
  125. CHANTAB_ENT(8, 2447),
  126. CHANTAB_ENT(9, 2452),
  127. CHANTAB_ENT(10, 2457),
  128. CHANTAB_ENT(11, 2462),
  129. CHANTAB_ENT(12, 2467),
  130. CHANTAB_ENT(13, 2472),
  131. CHANTAB_ENT(14, 2484),
  132. };
  133. #define b43_2ghz_chantable_size ARRAY_SIZE(b43_2ghz_chantable)
  134. #if 0
  135. static struct ieee80211_channel b43_5ghz_chantable[] = {
  136. CHANTAB_ENT(36, 5180),
  137. CHANTAB_ENT(40, 5200),
  138. CHANTAB_ENT(44, 5220),
  139. CHANTAB_ENT(48, 5240),
  140. CHANTAB_ENT(52, 5260),
  141. CHANTAB_ENT(56, 5280),
  142. CHANTAB_ENT(60, 5300),
  143. CHANTAB_ENT(64, 5320),
  144. CHANTAB_ENT(149, 5745),
  145. CHANTAB_ENT(153, 5765),
  146. CHANTAB_ENT(157, 5785),
  147. CHANTAB_ENT(161, 5805),
  148. CHANTAB_ENT(165, 5825),
  149. };
  150. #define b43_5ghz_chantable_size ARRAY_SIZE(b43_5ghz_chantable)
  151. #endif
  152. static void b43_wireless_core_exit(struct b43_wldev *dev);
  153. static int b43_wireless_core_init(struct b43_wldev *dev);
  154. static void b43_wireless_core_stop(struct b43_wldev *dev);
  155. static int b43_wireless_core_start(struct b43_wldev *dev);
  156. static int b43_ratelimit(struct b43_wl *wl)
  157. {
  158. if (!wl || !wl->current_dev)
  159. return 1;
  160. if (b43_status(wl->current_dev) < B43_STAT_STARTED)
  161. return 1;
  162. /* We are up and running.
  163. * Ratelimit the messages to avoid DoS over the net. */
  164. return net_ratelimit();
  165. }
  166. void b43info(struct b43_wl *wl, const char *fmt, ...)
  167. {
  168. va_list args;
  169. if (!b43_ratelimit(wl))
  170. return;
  171. va_start(args, fmt);
  172. printk(KERN_INFO "b43-%s: ",
  173. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  174. vprintk(fmt, args);
  175. va_end(args);
  176. }
  177. void b43err(struct b43_wl *wl, const char *fmt, ...)
  178. {
  179. va_list args;
  180. if (!b43_ratelimit(wl))
  181. return;
  182. va_start(args, fmt);
  183. printk(KERN_ERR "b43-%s ERROR: ",
  184. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  185. vprintk(fmt, args);
  186. va_end(args);
  187. }
  188. void b43warn(struct b43_wl *wl, const char *fmt, ...)
  189. {
  190. va_list args;
  191. if (!b43_ratelimit(wl))
  192. return;
  193. va_start(args, fmt);
  194. printk(KERN_WARNING "b43-%s warning: ",
  195. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  196. vprintk(fmt, args);
  197. va_end(args);
  198. }
  199. #if B43_DEBUG
  200. void b43dbg(struct b43_wl *wl, const char *fmt, ...)
  201. {
  202. va_list args;
  203. va_start(args, fmt);
  204. printk(KERN_DEBUG "b43-%s debug: ",
  205. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  206. vprintk(fmt, args);
  207. va_end(args);
  208. }
  209. #endif /* DEBUG */
  210. static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
  211. {
  212. u32 macctl;
  213. B43_WARN_ON(offset % 4 != 0);
  214. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  215. if (macctl & B43_MACCTL_BE)
  216. val = swab32(val);
  217. b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
  218. mmiowb();
  219. b43_write32(dev, B43_MMIO_RAM_DATA, val);
  220. }
  221. static inline void b43_shm_control_word(struct b43_wldev *dev,
  222. u16 routing, u16 offset)
  223. {
  224. u32 control;
  225. /* "offset" is the WORD offset. */
  226. control = routing;
  227. control <<= 16;
  228. control |= offset;
  229. b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
  230. }
  231. u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  232. {
  233. struct b43_wl *wl = dev->wl;
  234. unsigned long flags;
  235. u32 ret;
  236. spin_lock_irqsave(&wl->shm_lock, flags);
  237. if (routing == B43_SHM_SHARED) {
  238. B43_WARN_ON(offset & 0x0001);
  239. if (offset & 0x0003) {
  240. /* Unaligned access */
  241. b43_shm_control_word(dev, routing, offset >> 2);
  242. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  243. ret <<= 16;
  244. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  245. ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
  246. goto out;
  247. }
  248. offset >>= 2;
  249. }
  250. b43_shm_control_word(dev, routing, offset);
  251. ret = b43_read32(dev, B43_MMIO_SHM_DATA);
  252. out:
  253. spin_unlock_irqrestore(&wl->shm_lock, flags);
  254. return ret;
  255. }
  256. u16 b43_shm_read16(struct b43_wldev * dev, u16 routing, u16 offset)
  257. {
  258. struct b43_wl *wl = dev->wl;
  259. unsigned long flags;
  260. u16 ret;
  261. spin_lock_irqsave(&wl->shm_lock, flags);
  262. if (routing == B43_SHM_SHARED) {
  263. B43_WARN_ON(offset & 0x0001);
  264. if (offset & 0x0003) {
  265. /* Unaligned access */
  266. b43_shm_control_word(dev, routing, offset >> 2);
  267. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  268. goto out;
  269. }
  270. offset >>= 2;
  271. }
  272. b43_shm_control_word(dev, routing, offset);
  273. ret = b43_read16(dev, B43_MMIO_SHM_DATA);
  274. out:
  275. spin_unlock_irqrestore(&wl->shm_lock, flags);
  276. return ret;
  277. }
  278. void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  279. {
  280. struct b43_wl *wl = dev->wl;
  281. unsigned long flags;
  282. spin_lock_irqsave(&wl->shm_lock, flags);
  283. if (routing == B43_SHM_SHARED) {
  284. B43_WARN_ON(offset & 0x0001);
  285. if (offset & 0x0003) {
  286. /* Unaligned access */
  287. b43_shm_control_word(dev, routing, offset >> 2);
  288. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
  289. (value >> 16) & 0xffff);
  290. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  291. b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
  292. goto out;
  293. }
  294. offset >>= 2;
  295. }
  296. b43_shm_control_word(dev, routing, offset);
  297. b43_write32(dev, B43_MMIO_SHM_DATA, value);
  298. out:
  299. spin_unlock_irqrestore(&wl->shm_lock, flags);
  300. }
  301. void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  302. {
  303. struct b43_wl *wl = dev->wl;
  304. unsigned long flags;
  305. spin_lock_irqsave(&wl->shm_lock, flags);
  306. if (routing == B43_SHM_SHARED) {
  307. B43_WARN_ON(offset & 0x0001);
  308. if (offset & 0x0003) {
  309. /* Unaligned access */
  310. b43_shm_control_word(dev, routing, offset >> 2);
  311. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
  312. goto out;
  313. }
  314. offset >>= 2;
  315. }
  316. b43_shm_control_word(dev, routing, offset);
  317. b43_write16(dev, B43_MMIO_SHM_DATA, value);
  318. out:
  319. spin_unlock_irqrestore(&wl->shm_lock, flags);
  320. }
  321. /* Read HostFlags */
  322. u32 b43_hf_read(struct b43_wldev * dev)
  323. {
  324. u32 ret;
  325. ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
  326. ret <<= 16;
  327. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
  328. return ret;
  329. }
  330. /* Write HostFlags */
  331. void b43_hf_write(struct b43_wldev *dev, u32 value)
  332. {
  333. b43_shm_write16(dev, B43_SHM_SHARED,
  334. B43_SHM_SH_HOSTFLO, (value & 0x0000FFFF));
  335. b43_shm_write16(dev, B43_SHM_SHARED,
  336. B43_SHM_SH_HOSTFHI, ((value & 0xFFFF0000) >> 16));
  337. }
  338. void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
  339. {
  340. /* We need to be careful. As we read the TSF from multiple
  341. * registers, we should take care of register overflows.
  342. * In theory, the whole tsf read process should be atomic.
  343. * We try to be atomic here, by restaring the read process,
  344. * if any of the high registers changed (overflew).
  345. */
  346. if (dev->dev->id.revision >= 3) {
  347. u32 low, high, high2;
  348. do {
  349. high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  350. low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
  351. high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  352. } while (unlikely(high != high2));
  353. *tsf = high;
  354. *tsf <<= 32;
  355. *tsf |= low;
  356. } else {
  357. u64 tmp;
  358. u16 v0, v1, v2, v3;
  359. u16 test1, test2, test3;
  360. do {
  361. v3 = b43_read16(dev, B43_MMIO_TSF_3);
  362. v2 = b43_read16(dev, B43_MMIO_TSF_2);
  363. v1 = b43_read16(dev, B43_MMIO_TSF_1);
  364. v0 = b43_read16(dev, B43_MMIO_TSF_0);
  365. test3 = b43_read16(dev, B43_MMIO_TSF_3);
  366. test2 = b43_read16(dev, B43_MMIO_TSF_2);
  367. test1 = b43_read16(dev, B43_MMIO_TSF_1);
  368. } while (v3 != test3 || v2 != test2 || v1 != test1);
  369. *tsf = v3;
  370. *tsf <<= 48;
  371. tmp = v2;
  372. tmp <<= 32;
  373. *tsf |= tmp;
  374. tmp = v1;
  375. tmp <<= 16;
  376. *tsf |= tmp;
  377. *tsf |= v0;
  378. }
  379. }
  380. static void b43_time_lock(struct b43_wldev *dev)
  381. {
  382. u32 macctl;
  383. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  384. macctl |= B43_MACCTL_TBTTHOLD;
  385. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  386. /* Commit the write */
  387. b43_read32(dev, B43_MMIO_MACCTL);
  388. }
  389. static void b43_time_unlock(struct b43_wldev *dev)
  390. {
  391. u32 macctl;
  392. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  393. macctl &= ~B43_MACCTL_TBTTHOLD;
  394. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  395. /* Commit the write */
  396. b43_read32(dev, B43_MMIO_MACCTL);
  397. }
  398. static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
  399. {
  400. /* Be careful with the in-progress timer.
  401. * First zero out the low register, so we have a full
  402. * register-overflow duration to complete the operation.
  403. */
  404. if (dev->dev->id.revision >= 3) {
  405. u32 lo = (tsf & 0x00000000FFFFFFFFULL);
  406. u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
  407. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
  408. mmiowb();
  409. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
  410. mmiowb();
  411. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
  412. } else {
  413. u16 v0 = (tsf & 0x000000000000FFFFULL);
  414. u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
  415. u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
  416. u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
  417. b43_write16(dev, B43_MMIO_TSF_0, 0);
  418. mmiowb();
  419. b43_write16(dev, B43_MMIO_TSF_3, v3);
  420. mmiowb();
  421. b43_write16(dev, B43_MMIO_TSF_2, v2);
  422. mmiowb();
  423. b43_write16(dev, B43_MMIO_TSF_1, v1);
  424. mmiowb();
  425. b43_write16(dev, B43_MMIO_TSF_0, v0);
  426. }
  427. }
  428. void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
  429. {
  430. b43_time_lock(dev);
  431. b43_tsf_write_locked(dev, tsf);
  432. b43_time_unlock(dev);
  433. }
  434. static
  435. void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
  436. {
  437. static const u8 zero_addr[ETH_ALEN] = { 0 };
  438. u16 data;
  439. if (!mac)
  440. mac = zero_addr;
  441. offset |= 0x0020;
  442. b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
  443. data = mac[0];
  444. data |= mac[1] << 8;
  445. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  446. data = mac[2];
  447. data |= mac[3] << 8;
  448. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  449. data = mac[4];
  450. data |= mac[5] << 8;
  451. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  452. }
  453. static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
  454. {
  455. const u8 *mac;
  456. const u8 *bssid;
  457. u8 mac_bssid[ETH_ALEN * 2];
  458. int i;
  459. u32 tmp;
  460. bssid = dev->wl->bssid;
  461. mac = dev->wl->mac_addr;
  462. b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
  463. memcpy(mac_bssid, mac, ETH_ALEN);
  464. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  465. /* Write our MAC address and BSSID to template ram */
  466. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  467. tmp = (u32) (mac_bssid[i + 0]);
  468. tmp |= (u32) (mac_bssid[i + 1]) << 8;
  469. tmp |= (u32) (mac_bssid[i + 2]) << 16;
  470. tmp |= (u32) (mac_bssid[i + 3]) << 24;
  471. b43_ram_write(dev, 0x20 + i, tmp);
  472. }
  473. }
  474. static void b43_upload_card_macaddress(struct b43_wldev *dev)
  475. {
  476. b43_write_mac_bssid_templates(dev);
  477. b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
  478. }
  479. static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
  480. {
  481. /* slot_time is in usec. */
  482. if (dev->phy.type != B43_PHYTYPE_G)
  483. return;
  484. b43_write16(dev, 0x684, 510 + slot_time);
  485. b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
  486. }
  487. static void b43_short_slot_timing_enable(struct b43_wldev *dev)
  488. {
  489. b43_set_slot_time(dev, 9);
  490. dev->short_slot = 1;
  491. }
  492. static void b43_short_slot_timing_disable(struct b43_wldev *dev)
  493. {
  494. b43_set_slot_time(dev, 20);
  495. dev->short_slot = 0;
  496. }
  497. /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
  498. * Returns the _previously_ enabled IRQ mask.
  499. */
  500. static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
  501. {
  502. u32 old_mask;
  503. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  504. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
  505. return old_mask;
  506. }
  507. /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
  508. * Returns the _previously_ enabled IRQ mask.
  509. */
  510. static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
  511. {
  512. u32 old_mask;
  513. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  514. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
  515. return old_mask;
  516. }
  517. /* Synchronize IRQ top- and bottom-half.
  518. * IRQs must be masked before calling this.
  519. * This must not be called with the irq_lock held.
  520. */
  521. static void b43_synchronize_irq(struct b43_wldev *dev)
  522. {
  523. synchronize_irq(dev->dev->irq);
  524. tasklet_kill(&dev->isr_tasklet);
  525. }
  526. /* DummyTransmission function, as documented on
  527. * http://bcm-specs.sipsolutions.net/DummyTransmission
  528. */
  529. void b43_dummy_transmission(struct b43_wldev *dev)
  530. {
  531. struct b43_phy *phy = &dev->phy;
  532. unsigned int i, max_loop;
  533. u16 value;
  534. u32 buffer[5] = {
  535. 0x00000000,
  536. 0x00D40000,
  537. 0x00000000,
  538. 0x01000000,
  539. 0x00000000,
  540. };
  541. switch (phy->type) {
  542. case B43_PHYTYPE_A:
  543. max_loop = 0x1E;
  544. buffer[0] = 0x000201CC;
  545. break;
  546. case B43_PHYTYPE_B:
  547. case B43_PHYTYPE_G:
  548. max_loop = 0xFA;
  549. buffer[0] = 0x000B846E;
  550. break;
  551. default:
  552. B43_WARN_ON(1);
  553. return;
  554. }
  555. for (i = 0; i < 5; i++)
  556. b43_ram_write(dev, i * 4, buffer[i]);
  557. /* Commit writes */
  558. b43_read32(dev, B43_MMIO_MACCTL);
  559. b43_write16(dev, 0x0568, 0x0000);
  560. b43_write16(dev, 0x07C0, 0x0000);
  561. value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
  562. b43_write16(dev, 0x050C, value);
  563. b43_write16(dev, 0x0508, 0x0000);
  564. b43_write16(dev, 0x050A, 0x0000);
  565. b43_write16(dev, 0x054C, 0x0000);
  566. b43_write16(dev, 0x056A, 0x0014);
  567. b43_write16(dev, 0x0568, 0x0826);
  568. b43_write16(dev, 0x0500, 0x0000);
  569. b43_write16(dev, 0x0502, 0x0030);
  570. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  571. b43_radio_write16(dev, 0x0051, 0x0017);
  572. for (i = 0x00; i < max_loop; i++) {
  573. value = b43_read16(dev, 0x050E);
  574. if (value & 0x0080)
  575. break;
  576. udelay(10);
  577. }
  578. for (i = 0x00; i < 0x0A; i++) {
  579. value = b43_read16(dev, 0x050E);
  580. if (value & 0x0400)
  581. break;
  582. udelay(10);
  583. }
  584. for (i = 0x00; i < 0x0A; i++) {
  585. value = b43_read16(dev, 0x0690);
  586. if (!(value & 0x0100))
  587. break;
  588. udelay(10);
  589. }
  590. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  591. b43_radio_write16(dev, 0x0051, 0x0037);
  592. }
  593. static void key_write(struct b43_wldev *dev,
  594. u8 index, u8 algorithm, const u8 * key)
  595. {
  596. unsigned int i;
  597. u32 offset;
  598. u16 value;
  599. u16 kidx;
  600. /* Key index/algo block */
  601. kidx = b43_kidx_to_fw(dev, index);
  602. value = ((kidx << 4) | algorithm);
  603. b43_shm_write16(dev, B43_SHM_SHARED,
  604. B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
  605. /* Write the key to the Key Table Pointer offset */
  606. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  607. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  608. value = key[i];
  609. value |= (u16) (key[i + 1]) << 8;
  610. b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
  611. }
  612. }
  613. static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
  614. {
  615. u32 addrtmp[2] = { 0, 0, };
  616. u8 per_sta_keys_start = 8;
  617. if (b43_new_kidx_api(dev))
  618. per_sta_keys_start = 4;
  619. B43_WARN_ON(index < per_sta_keys_start);
  620. /* We have two default TX keys and possibly two default RX keys.
  621. * Physical mac 0 is mapped to physical key 4 or 8, depending
  622. * on the firmware version.
  623. * So we must adjust the index here.
  624. */
  625. index -= per_sta_keys_start;
  626. if (addr) {
  627. addrtmp[0] = addr[0];
  628. addrtmp[0] |= ((u32) (addr[1]) << 8);
  629. addrtmp[0] |= ((u32) (addr[2]) << 16);
  630. addrtmp[0] |= ((u32) (addr[3]) << 24);
  631. addrtmp[1] = addr[4];
  632. addrtmp[1] |= ((u32) (addr[5]) << 8);
  633. }
  634. if (dev->dev->id.revision >= 5) {
  635. /* Receive match transmitter address mechanism */
  636. b43_shm_write32(dev, B43_SHM_RCMTA,
  637. (index * 2) + 0, addrtmp[0]);
  638. b43_shm_write16(dev, B43_SHM_RCMTA,
  639. (index * 2) + 1, addrtmp[1]);
  640. } else {
  641. /* RXE (Receive Engine) and
  642. * PSM (Programmable State Machine) mechanism
  643. */
  644. if (index < 8) {
  645. /* TODO write to RCM 16, 19, 22 and 25 */
  646. } else {
  647. b43_shm_write32(dev, B43_SHM_SHARED,
  648. B43_SHM_SH_PSM + (index * 6) + 0,
  649. addrtmp[0]);
  650. b43_shm_write16(dev, B43_SHM_SHARED,
  651. B43_SHM_SH_PSM + (index * 6) + 4,
  652. addrtmp[1]);
  653. }
  654. }
  655. }
  656. static void do_key_write(struct b43_wldev *dev,
  657. u8 index, u8 algorithm,
  658. const u8 * key, size_t key_len, const u8 * mac_addr)
  659. {
  660. u8 buf[B43_SEC_KEYSIZE] = { 0, };
  661. u8 per_sta_keys_start = 8;
  662. if (b43_new_kidx_api(dev))
  663. per_sta_keys_start = 4;
  664. B43_WARN_ON(index >= dev->max_nr_keys);
  665. B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
  666. if (index >= per_sta_keys_start)
  667. keymac_write(dev, index, NULL); /* First zero out mac. */
  668. if (key)
  669. memcpy(buf, key, key_len);
  670. key_write(dev, index, algorithm, buf);
  671. if (index >= per_sta_keys_start)
  672. keymac_write(dev, index, mac_addr);
  673. dev->key[index].algorithm = algorithm;
  674. }
  675. static int b43_key_write(struct b43_wldev *dev,
  676. int index, u8 algorithm,
  677. const u8 * key, size_t key_len,
  678. const u8 * mac_addr,
  679. struct ieee80211_key_conf *keyconf)
  680. {
  681. int i;
  682. int sta_keys_start;
  683. if (key_len > B43_SEC_KEYSIZE)
  684. return -EINVAL;
  685. for (i = 0; i < dev->max_nr_keys; i++) {
  686. /* Check that we don't already have this key. */
  687. B43_WARN_ON(dev->key[i].keyconf == keyconf);
  688. }
  689. if (index < 0) {
  690. /* Either pairwise key or address is 00:00:00:00:00:00
  691. * for transmit-only keys. Search the index. */
  692. if (b43_new_kidx_api(dev))
  693. sta_keys_start = 4;
  694. else
  695. sta_keys_start = 8;
  696. for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
  697. if (!dev->key[i].keyconf) {
  698. /* found empty */
  699. index = i;
  700. break;
  701. }
  702. }
  703. if (index < 0) {
  704. b43err(dev->wl, "Out of hardware key memory\n");
  705. return -ENOSPC;
  706. }
  707. } else
  708. B43_WARN_ON(index > 3);
  709. do_key_write(dev, index, algorithm, key, key_len, mac_addr);
  710. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  711. /* Default RX key */
  712. B43_WARN_ON(mac_addr);
  713. do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
  714. }
  715. keyconf->hw_key_idx = index;
  716. dev->key[index].keyconf = keyconf;
  717. return 0;
  718. }
  719. static int b43_key_clear(struct b43_wldev *dev, int index)
  720. {
  721. if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
  722. return -EINVAL;
  723. do_key_write(dev, index, B43_SEC_ALGO_NONE,
  724. NULL, B43_SEC_KEYSIZE, NULL);
  725. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  726. do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
  727. NULL, B43_SEC_KEYSIZE, NULL);
  728. }
  729. dev->key[index].keyconf = NULL;
  730. return 0;
  731. }
  732. static void b43_clear_keys(struct b43_wldev *dev)
  733. {
  734. int i;
  735. for (i = 0; i < dev->max_nr_keys; i++)
  736. b43_key_clear(dev, i);
  737. }
  738. void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
  739. {
  740. u32 macctl;
  741. u16 ucstat;
  742. bool hwps;
  743. bool awake;
  744. int i;
  745. B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
  746. (ps_flags & B43_PS_DISABLED));
  747. B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
  748. if (ps_flags & B43_PS_ENABLED) {
  749. hwps = 1;
  750. } else if (ps_flags & B43_PS_DISABLED) {
  751. hwps = 0;
  752. } else {
  753. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  754. // and thus is not an AP and we are associated, set bit 25
  755. }
  756. if (ps_flags & B43_PS_AWAKE) {
  757. awake = 1;
  758. } else if (ps_flags & B43_PS_ASLEEP) {
  759. awake = 0;
  760. } else {
  761. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  762. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  763. // successful, set bit26
  764. }
  765. /* FIXME: For now we force awake-on and hwps-off */
  766. hwps = 0;
  767. awake = 1;
  768. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  769. if (hwps)
  770. macctl |= B43_MACCTL_HWPS;
  771. else
  772. macctl &= ~B43_MACCTL_HWPS;
  773. if (awake)
  774. macctl |= B43_MACCTL_AWAKE;
  775. else
  776. macctl &= ~B43_MACCTL_AWAKE;
  777. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  778. /* Commit write */
  779. b43_read32(dev, B43_MMIO_MACCTL);
  780. if (awake && dev->dev->id.revision >= 5) {
  781. /* Wait for the microcode to wake up. */
  782. for (i = 0; i < 100; i++) {
  783. ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
  784. B43_SHM_SH_UCODESTAT);
  785. if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
  786. break;
  787. udelay(10);
  788. }
  789. }
  790. }
  791. /* Turn the Analog ON/OFF */
  792. static void b43_switch_analog(struct b43_wldev *dev, int on)
  793. {
  794. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  795. }
  796. void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
  797. {
  798. u32 tmslow;
  799. u32 macctl;
  800. flags |= B43_TMSLOW_PHYCLKEN;
  801. flags |= B43_TMSLOW_PHYRESET;
  802. ssb_device_enable(dev->dev, flags);
  803. msleep(2); /* Wait for the PLL to turn on. */
  804. /* Now take the PHY out of Reset again */
  805. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  806. tmslow |= SSB_TMSLOW_FGC;
  807. tmslow &= ~B43_TMSLOW_PHYRESET;
  808. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  809. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  810. msleep(1);
  811. tmslow &= ~SSB_TMSLOW_FGC;
  812. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  813. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  814. msleep(1);
  815. /* Turn Analog ON */
  816. b43_switch_analog(dev, 1);
  817. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  818. macctl &= ~B43_MACCTL_GMODE;
  819. if (flags & B43_TMSLOW_GMODE)
  820. macctl |= B43_MACCTL_GMODE;
  821. macctl |= B43_MACCTL_IHR_ENABLED;
  822. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  823. }
  824. static void handle_irq_transmit_status(struct b43_wldev *dev)
  825. {
  826. u32 v0, v1;
  827. u16 tmp;
  828. struct b43_txstatus stat;
  829. while (1) {
  830. v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  831. if (!(v0 & 0x00000001))
  832. break;
  833. v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  834. stat.cookie = (v0 >> 16);
  835. stat.seq = (v1 & 0x0000FFFF);
  836. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  837. tmp = (v0 & 0x0000FFFF);
  838. stat.frame_count = ((tmp & 0xF000) >> 12);
  839. stat.rts_count = ((tmp & 0x0F00) >> 8);
  840. stat.supp_reason = ((tmp & 0x001C) >> 2);
  841. stat.pm_indicated = !!(tmp & 0x0080);
  842. stat.intermediate = !!(tmp & 0x0040);
  843. stat.for_ampdu = !!(tmp & 0x0020);
  844. stat.acked = !!(tmp & 0x0002);
  845. b43_handle_txstatus(dev, &stat);
  846. }
  847. }
  848. static void drain_txstatus_queue(struct b43_wldev *dev)
  849. {
  850. u32 dummy;
  851. if (dev->dev->id.revision < 5)
  852. return;
  853. /* Read all entries from the microcode TXstatus FIFO
  854. * and throw them away.
  855. */
  856. while (1) {
  857. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  858. if (!(dummy & 0x00000001))
  859. break;
  860. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  861. }
  862. }
  863. static u32 b43_jssi_read(struct b43_wldev *dev)
  864. {
  865. u32 val = 0;
  866. val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
  867. val <<= 16;
  868. val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
  869. return val;
  870. }
  871. static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
  872. {
  873. b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
  874. b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
  875. }
  876. static void b43_generate_noise_sample(struct b43_wldev *dev)
  877. {
  878. b43_jssi_write(dev, 0x7F7F7F7F);
  879. b43_write32(dev, B43_MMIO_MACCMD,
  880. b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
  881. B43_WARN_ON(dev->noisecalc.channel_at_start != dev->phy.channel);
  882. }
  883. static void b43_calculate_link_quality(struct b43_wldev *dev)
  884. {
  885. /* Top half of Link Quality calculation. */
  886. if (dev->noisecalc.calculation_running)
  887. return;
  888. dev->noisecalc.channel_at_start = dev->phy.channel;
  889. dev->noisecalc.calculation_running = 1;
  890. dev->noisecalc.nr_samples = 0;
  891. b43_generate_noise_sample(dev);
  892. }
  893. static void handle_irq_noise(struct b43_wldev *dev)
  894. {
  895. struct b43_phy *phy = &dev->phy;
  896. u16 tmp;
  897. u8 noise[4];
  898. u8 i, j;
  899. s32 average;
  900. /* Bottom half of Link Quality calculation. */
  901. B43_WARN_ON(!dev->noisecalc.calculation_running);
  902. if (dev->noisecalc.channel_at_start != phy->channel)
  903. goto drop_calculation;
  904. *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
  905. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  906. noise[2] == 0x7F || noise[3] == 0x7F)
  907. goto generate_new;
  908. /* Get the noise samples. */
  909. B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
  910. i = dev->noisecalc.nr_samples;
  911. noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  912. noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  913. noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  914. noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  915. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  916. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  917. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  918. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  919. dev->noisecalc.nr_samples++;
  920. if (dev->noisecalc.nr_samples == 8) {
  921. /* Calculate the Link Quality by the noise samples. */
  922. average = 0;
  923. for (i = 0; i < 8; i++) {
  924. for (j = 0; j < 4; j++)
  925. average += dev->noisecalc.samples[i][j];
  926. }
  927. average /= (8 * 4);
  928. average *= 125;
  929. average += 64;
  930. average /= 128;
  931. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
  932. tmp = (tmp / 128) & 0x1F;
  933. if (tmp >= 8)
  934. average += 2;
  935. else
  936. average -= 25;
  937. if (tmp == 8)
  938. average -= 72;
  939. else
  940. average -= 48;
  941. dev->stats.link_noise = average;
  942. drop_calculation:
  943. dev->noisecalc.calculation_running = 0;
  944. return;
  945. }
  946. generate_new:
  947. b43_generate_noise_sample(dev);
  948. }
  949. static void handle_irq_tbtt_indication(struct b43_wldev *dev)
  950. {
  951. if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
  952. ///TODO: PS TBTT
  953. } else {
  954. if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
  955. b43_power_saving_ctl_bits(dev, 0);
  956. }
  957. if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
  958. dev->dfq_valid = 1;
  959. }
  960. static void handle_irq_atim_end(struct b43_wldev *dev)
  961. {
  962. if (dev->dfq_valid) {
  963. b43_write32(dev, B43_MMIO_MACCMD,
  964. b43_read32(dev, B43_MMIO_MACCMD)
  965. | B43_MACCMD_DFQ_VALID);
  966. dev->dfq_valid = 0;
  967. }
  968. }
  969. static void handle_irq_pmq(struct b43_wldev *dev)
  970. {
  971. u32 tmp;
  972. //TODO: AP mode.
  973. while (1) {
  974. tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
  975. if (!(tmp & 0x00000008))
  976. break;
  977. }
  978. /* 16bit write is odd, but correct. */
  979. b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
  980. }
  981. static void b43_write_template_common(struct b43_wldev *dev,
  982. const u8 * data, u16 size,
  983. u16 ram_offset,
  984. u16 shm_size_offset, u8 rate)
  985. {
  986. u32 i, tmp;
  987. struct b43_plcp_hdr4 plcp;
  988. plcp.data = 0;
  989. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  990. b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  991. ram_offset += sizeof(u32);
  992. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  993. * So leave the first two bytes of the next write blank.
  994. */
  995. tmp = (u32) (data[0]) << 16;
  996. tmp |= (u32) (data[1]) << 24;
  997. b43_ram_write(dev, ram_offset, tmp);
  998. ram_offset += sizeof(u32);
  999. for (i = 2; i < size; i += sizeof(u32)) {
  1000. tmp = (u32) (data[i + 0]);
  1001. if (i + 1 < size)
  1002. tmp |= (u32) (data[i + 1]) << 8;
  1003. if (i + 2 < size)
  1004. tmp |= (u32) (data[i + 2]) << 16;
  1005. if (i + 3 < size)
  1006. tmp |= (u32) (data[i + 3]) << 24;
  1007. b43_ram_write(dev, ram_offset + i - 2, tmp);
  1008. }
  1009. b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
  1010. size + sizeof(struct b43_plcp_hdr6));
  1011. }
  1012. static void b43_write_beacon_template(struct b43_wldev *dev,
  1013. u16 ram_offset,
  1014. u16 shm_size_offset, u8 rate)
  1015. {
  1016. unsigned int i, len, variable_len;
  1017. const struct ieee80211_mgmt *bcn;
  1018. const u8 *ie;
  1019. bool tim_found = 0;
  1020. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  1021. len = min((size_t) dev->wl->current_beacon->len,
  1022. 0x200 - sizeof(struct b43_plcp_hdr6));
  1023. b43_write_template_common(dev, (const u8 *)bcn,
  1024. len, ram_offset, shm_size_offset, rate);
  1025. /* Find the position of the TIM and the DTIM_period value
  1026. * and write them to SHM. */
  1027. ie = bcn->u.beacon.variable;
  1028. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1029. for (i = 0; i < variable_len - 2; ) {
  1030. uint8_t ie_id, ie_len;
  1031. ie_id = ie[i];
  1032. ie_len = ie[i + 1];
  1033. if (ie_id == 5) {
  1034. u16 tim_position;
  1035. u16 dtim_period;
  1036. /* This is the TIM Information Element */
  1037. /* Check whether the ie_len is in the beacon data range. */
  1038. if (variable_len < ie_len + 2 + i)
  1039. break;
  1040. /* A valid TIM is at least 4 bytes long. */
  1041. if (ie_len < 4)
  1042. break;
  1043. tim_found = 1;
  1044. tim_position = sizeof(struct b43_plcp_hdr6);
  1045. tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1046. tim_position += i;
  1047. dtim_period = ie[i + 3];
  1048. b43_shm_write16(dev, B43_SHM_SHARED,
  1049. B43_SHM_SH_TIMBPOS, tim_position);
  1050. b43_shm_write16(dev, B43_SHM_SHARED,
  1051. B43_SHM_SH_DTIMPER, dtim_period);
  1052. break;
  1053. }
  1054. i += ie_len + 2;
  1055. }
  1056. if (!tim_found) {
  1057. b43warn(dev->wl, "Did not find a valid TIM IE in "
  1058. "the beacon template packet. AP or IBSS operation "
  1059. "may be broken.\n");
  1060. }
  1061. }
  1062. static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  1063. u16 shm_offset, u16 size, u8 rate)
  1064. {
  1065. struct b43_plcp_hdr4 plcp;
  1066. u32 tmp;
  1067. __le16 dur;
  1068. plcp.data = 0;
  1069. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  1070. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1071. dev->wl->vif, size,
  1072. B43_RATE_TO_BASE100KBPS(rate));
  1073. /* Write PLCP in two parts and timing for packet transfer */
  1074. tmp = le32_to_cpu(plcp.data);
  1075. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
  1076. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
  1077. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
  1078. }
  1079. /* Instead of using custom probe response template, this function
  1080. * just patches custom beacon template by:
  1081. * 1) Changing packet type
  1082. * 2) Patching duration field
  1083. * 3) Stripping TIM
  1084. */
  1085. static const u8 * b43_generate_probe_resp(struct b43_wldev *dev,
  1086. u16 *dest_size, u8 rate)
  1087. {
  1088. const u8 *src_data;
  1089. u8 *dest_data;
  1090. u16 src_size, elem_size, src_pos, dest_pos;
  1091. __le16 dur;
  1092. struct ieee80211_hdr *hdr;
  1093. size_t ie_start;
  1094. src_size = dev->wl->current_beacon->len;
  1095. src_data = (const u8 *)dev->wl->current_beacon->data;
  1096. /* Get the start offset of the variable IEs in the packet. */
  1097. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  1098. B43_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1099. if (B43_WARN_ON(src_size < ie_start))
  1100. return NULL;
  1101. dest_data = kmalloc(src_size, GFP_ATOMIC);
  1102. if (unlikely(!dest_data))
  1103. return NULL;
  1104. /* Copy the static data and all Information Elements, except the TIM. */
  1105. memcpy(dest_data, src_data, ie_start);
  1106. src_pos = ie_start;
  1107. dest_pos = ie_start;
  1108. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  1109. elem_size = src_data[src_pos + 1] + 2;
  1110. if (src_data[src_pos] == 5) {
  1111. /* This is the TIM. */
  1112. continue;
  1113. }
  1114. memcpy(dest_data + dest_pos, src_data + src_pos,
  1115. elem_size);
  1116. dest_pos += elem_size;
  1117. }
  1118. *dest_size = dest_pos;
  1119. hdr = (struct ieee80211_hdr *)dest_data;
  1120. /* Set the frame control. */
  1121. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1122. IEEE80211_STYPE_PROBE_RESP);
  1123. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1124. dev->wl->vif, *dest_size,
  1125. B43_RATE_TO_BASE100KBPS(rate));
  1126. hdr->duration_id = dur;
  1127. return dest_data;
  1128. }
  1129. static void b43_write_probe_resp_template(struct b43_wldev *dev,
  1130. u16 ram_offset,
  1131. u16 shm_size_offset, u8 rate)
  1132. {
  1133. const u8 *probe_resp_data;
  1134. u16 size;
  1135. size = dev->wl->current_beacon->len;
  1136. probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
  1137. if (unlikely(!probe_resp_data))
  1138. return;
  1139. /* Looks like PLCP headers plus packet timings are stored for
  1140. * all possible basic rates
  1141. */
  1142. b43_write_probe_resp_plcp(dev, 0x31A, size, B43_CCK_RATE_1MB);
  1143. b43_write_probe_resp_plcp(dev, 0x32C, size, B43_CCK_RATE_2MB);
  1144. b43_write_probe_resp_plcp(dev, 0x33E, size, B43_CCK_RATE_5MB);
  1145. b43_write_probe_resp_plcp(dev, 0x350, size, B43_CCK_RATE_11MB);
  1146. size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
  1147. b43_write_template_common(dev, probe_resp_data,
  1148. size, ram_offset, shm_size_offset, rate);
  1149. kfree(probe_resp_data);
  1150. }
  1151. /* Asynchronously update the packet templates in template RAM.
  1152. * Locking: Requires wl->irq_lock to be locked. */
  1153. static void b43_update_templates(struct b43_wl *wl, struct sk_buff *beacon)
  1154. {
  1155. /* This is the top half of the ansynchronous beacon update.
  1156. * The bottom half is the beacon IRQ.
  1157. * Beacon update must be asynchronous to avoid sending an
  1158. * invalid beacon. This can happen for example, if the firmware
  1159. * transmits a beacon while we are updating it. */
  1160. if (wl->current_beacon)
  1161. dev_kfree_skb_any(wl->current_beacon);
  1162. wl->current_beacon = beacon;
  1163. wl->beacon0_uploaded = 0;
  1164. wl->beacon1_uploaded = 0;
  1165. }
  1166. static void b43_set_ssid(struct b43_wldev *dev, const u8 * ssid, u8 ssid_len)
  1167. {
  1168. u32 tmp;
  1169. u16 i, len;
  1170. len = min((u16) ssid_len, (u16) 0x100);
  1171. for (i = 0; i < len; i += sizeof(u32)) {
  1172. tmp = (u32) (ssid[i + 0]);
  1173. if (i + 1 < len)
  1174. tmp |= (u32) (ssid[i + 1]) << 8;
  1175. if (i + 2 < len)
  1176. tmp |= (u32) (ssid[i + 2]) << 16;
  1177. if (i + 3 < len)
  1178. tmp |= (u32) (ssid[i + 3]) << 24;
  1179. b43_shm_write32(dev, B43_SHM_SHARED, 0x380 + i, tmp);
  1180. }
  1181. b43_shm_write16(dev, B43_SHM_SHARED, 0x48, len);
  1182. }
  1183. static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
  1184. {
  1185. b43_time_lock(dev);
  1186. if (dev->dev->id.revision >= 3) {
  1187. b43_write32(dev, 0x188, (beacon_int << 16));
  1188. } else {
  1189. b43_write16(dev, 0x606, (beacon_int >> 6));
  1190. b43_write16(dev, 0x610, beacon_int);
  1191. }
  1192. b43_time_unlock(dev);
  1193. }
  1194. static void handle_irq_beacon(struct b43_wldev *dev)
  1195. {
  1196. struct b43_wl *wl = dev->wl;
  1197. u32 cmd;
  1198. if (!b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  1199. return;
  1200. /* This is the bottom half of the asynchronous beacon update. */
  1201. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1202. if (!(cmd & B43_MACCMD_BEACON0_VALID)) {
  1203. if (!wl->beacon0_uploaded) {
  1204. b43_write_beacon_template(dev, 0x68, 0x18,
  1205. B43_CCK_RATE_1MB);
  1206. b43_write_probe_resp_template(dev, 0x268, 0x4A,
  1207. B43_CCK_RATE_11MB);
  1208. wl->beacon0_uploaded = 1;
  1209. }
  1210. cmd |= B43_MACCMD_BEACON0_VALID;
  1211. }
  1212. if (!(cmd & B43_MACCMD_BEACON1_VALID)) {
  1213. if (!wl->beacon1_uploaded) {
  1214. b43_write_beacon_template(dev, 0x468, 0x1A,
  1215. B43_CCK_RATE_1MB);
  1216. wl->beacon1_uploaded = 1;
  1217. }
  1218. cmd |= B43_MACCMD_BEACON1_VALID;
  1219. }
  1220. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1221. }
  1222. static void handle_irq_ucode_debug(struct b43_wldev *dev)
  1223. {
  1224. //TODO
  1225. }
  1226. /* Interrupt handler bottom-half */
  1227. static void b43_interrupt_tasklet(struct b43_wldev *dev)
  1228. {
  1229. u32 reason;
  1230. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1231. u32 merged_dma_reason = 0;
  1232. int i;
  1233. unsigned long flags;
  1234. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1235. B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
  1236. reason = dev->irq_reason;
  1237. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1238. dma_reason[i] = dev->dma_reason[i];
  1239. merged_dma_reason |= dma_reason[i];
  1240. }
  1241. if (unlikely(reason & B43_IRQ_MAC_TXERR))
  1242. b43err(dev->wl, "MAC transmission error\n");
  1243. if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
  1244. b43err(dev->wl, "PHY transmission error\n");
  1245. rmb();
  1246. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1247. atomic_set(&dev->phy.txerr_cnt,
  1248. B43_PHY_TX_BADNESS_LIMIT);
  1249. b43err(dev->wl, "Too many PHY TX errors, "
  1250. "restarting the controller\n");
  1251. b43_controller_restart(dev, "PHY TX errors");
  1252. }
  1253. }
  1254. if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
  1255. B43_DMAIRQ_NONFATALMASK))) {
  1256. if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
  1257. b43err(dev->wl, "Fatal DMA error: "
  1258. "0x%08X, 0x%08X, 0x%08X, "
  1259. "0x%08X, 0x%08X, 0x%08X\n",
  1260. dma_reason[0], dma_reason[1],
  1261. dma_reason[2], dma_reason[3],
  1262. dma_reason[4], dma_reason[5]);
  1263. b43_controller_restart(dev, "DMA error");
  1264. mmiowb();
  1265. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1266. return;
  1267. }
  1268. if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
  1269. b43err(dev->wl, "DMA error: "
  1270. "0x%08X, 0x%08X, 0x%08X, "
  1271. "0x%08X, 0x%08X, 0x%08X\n",
  1272. dma_reason[0], dma_reason[1],
  1273. dma_reason[2], dma_reason[3],
  1274. dma_reason[4], dma_reason[5]);
  1275. }
  1276. }
  1277. if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
  1278. handle_irq_ucode_debug(dev);
  1279. if (reason & B43_IRQ_TBTT_INDI)
  1280. handle_irq_tbtt_indication(dev);
  1281. if (reason & B43_IRQ_ATIM_END)
  1282. handle_irq_atim_end(dev);
  1283. if (reason & B43_IRQ_BEACON)
  1284. handle_irq_beacon(dev);
  1285. if (reason & B43_IRQ_PMQ)
  1286. handle_irq_pmq(dev);
  1287. if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
  1288. ;/* TODO */
  1289. if (reason & B43_IRQ_NOISESAMPLE_OK)
  1290. handle_irq_noise(dev);
  1291. /* Check the DMA reason registers for received data. */
  1292. if (dma_reason[0] & B43_DMAIRQ_RX_DONE)
  1293. b43_dma_rx(dev->dma.rx_ring0);
  1294. if (dma_reason[3] & B43_DMAIRQ_RX_DONE)
  1295. b43_dma_rx(dev->dma.rx_ring3);
  1296. B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
  1297. B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
  1298. B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
  1299. B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
  1300. if (reason & B43_IRQ_TX_OK)
  1301. handle_irq_transmit_status(dev);
  1302. b43_interrupt_enable(dev, dev->irq_savedstate);
  1303. mmiowb();
  1304. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1305. }
  1306. static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
  1307. {
  1308. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
  1309. b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
  1310. b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
  1311. b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
  1312. b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
  1313. b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
  1314. b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
  1315. }
  1316. /* Interrupt handler top-half */
  1317. static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
  1318. {
  1319. irqreturn_t ret = IRQ_NONE;
  1320. struct b43_wldev *dev = dev_id;
  1321. u32 reason;
  1322. if (!dev)
  1323. return IRQ_NONE;
  1324. spin_lock(&dev->wl->irq_lock);
  1325. if (b43_status(dev) < B43_STAT_STARTED)
  1326. goto out;
  1327. reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1328. if (reason == 0xffffffff) /* shared IRQ */
  1329. goto out;
  1330. ret = IRQ_HANDLED;
  1331. reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1332. if (!reason)
  1333. goto out;
  1334. dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
  1335. & 0x0001DC00;
  1336. dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
  1337. & 0x0000DC00;
  1338. dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
  1339. & 0x0000DC00;
  1340. dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
  1341. & 0x0001DC00;
  1342. dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
  1343. & 0x0000DC00;
  1344. dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
  1345. & 0x0000DC00;
  1346. b43_interrupt_ack(dev, reason);
  1347. /* disable all IRQs. They are enabled again in the bottom half. */
  1348. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1349. /* save the reason code and call our bottom half. */
  1350. dev->irq_reason = reason;
  1351. tasklet_schedule(&dev->isr_tasklet);
  1352. out:
  1353. mmiowb();
  1354. spin_unlock(&dev->wl->irq_lock);
  1355. return ret;
  1356. }
  1357. static void do_release_fw(struct b43_firmware_file *fw)
  1358. {
  1359. release_firmware(fw->data);
  1360. fw->data = NULL;
  1361. fw->filename = NULL;
  1362. }
  1363. static void b43_release_firmware(struct b43_wldev *dev)
  1364. {
  1365. do_release_fw(&dev->fw.ucode);
  1366. do_release_fw(&dev->fw.pcm);
  1367. do_release_fw(&dev->fw.initvals);
  1368. do_release_fw(&dev->fw.initvals_band);
  1369. }
  1370. static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
  1371. {
  1372. const char *text;
  1373. text = "You must go to "
  1374. "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
  1375. "and download the latest firmware (version 4).\n";
  1376. if (error)
  1377. b43err(wl, text);
  1378. else
  1379. b43warn(wl, text);
  1380. }
  1381. static int do_request_fw(struct b43_wldev *dev,
  1382. const char *name,
  1383. struct b43_firmware_file *fw)
  1384. {
  1385. char path[sizeof(modparam_fwpostfix) + 32];
  1386. const struct firmware *blob;
  1387. struct b43_fw_header *hdr;
  1388. u32 size;
  1389. int err;
  1390. if (!name) {
  1391. /* Don't fetch anything. Free possibly cached firmware. */
  1392. do_release_fw(fw);
  1393. return 0;
  1394. }
  1395. if (fw->filename) {
  1396. if (strcmp(fw->filename, name) == 0)
  1397. return 0; /* Already have this fw. */
  1398. /* Free the cached firmware first. */
  1399. do_release_fw(fw);
  1400. }
  1401. snprintf(path, ARRAY_SIZE(path),
  1402. "b43%s/%s.fw",
  1403. modparam_fwpostfix, name);
  1404. err = request_firmware(&blob, path, dev->dev->dev);
  1405. if (err) {
  1406. b43err(dev->wl, "Firmware file \"%s\" not found "
  1407. "or load failed.\n", path);
  1408. return err;
  1409. }
  1410. if (blob->size < sizeof(struct b43_fw_header))
  1411. goto err_format;
  1412. hdr = (struct b43_fw_header *)(blob->data);
  1413. switch (hdr->type) {
  1414. case B43_FW_TYPE_UCODE:
  1415. case B43_FW_TYPE_PCM:
  1416. size = be32_to_cpu(hdr->size);
  1417. if (size != blob->size - sizeof(struct b43_fw_header))
  1418. goto err_format;
  1419. /* fallthrough */
  1420. case B43_FW_TYPE_IV:
  1421. if (hdr->ver != 1)
  1422. goto err_format;
  1423. break;
  1424. default:
  1425. goto err_format;
  1426. }
  1427. fw->data = blob;
  1428. fw->filename = name;
  1429. return 0;
  1430. err_format:
  1431. b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1432. release_firmware(blob);
  1433. return -EPROTO;
  1434. }
  1435. static int b43_request_firmware(struct b43_wldev *dev)
  1436. {
  1437. struct b43_firmware *fw = &dev->fw;
  1438. const u8 rev = dev->dev->id.revision;
  1439. const char *filename;
  1440. u32 tmshigh;
  1441. int err;
  1442. /* Get microcode */
  1443. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1444. if ((rev >= 5) && (rev <= 10))
  1445. filename = "ucode5";
  1446. else if ((rev >= 11) && (rev <= 12))
  1447. filename = "ucode11";
  1448. else if (rev >= 13)
  1449. filename = "ucode13";
  1450. else
  1451. goto err_no_ucode;
  1452. err = do_request_fw(dev, filename, &fw->ucode);
  1453. if (err)
  1454. goto err_load;
  1455. /* Get PCM code */
  1456. if ((rev >= 5) && (rev <= 10))
  1457. filename = "pcm5";
  1458. else if (rev >= 11)
  1459. filename = NULL;
  1460. else
  1461. goto err_no_pcm;
  1462. err = do_request_fw(dev, filename, &fw->pcm);
  1463. if (err)
  1464. goto err_load;
  1465. /* Get initvals */
  1466. switch (dev->phy.type) {
  1467. case B43_PHYTYPE_A:
  1468. if ((rev >= 5) && (rev <= 10)) {
  1469. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1470. filename = "a0g1initvals5";
  1471. else
  1472. filename = "a0g0initvals5";
  1473. } else
  1474. goto err_no_initvals;
  1475. break;
  1476. case B43_PHYTYPE_G:
  1477. if ((rev >= 5) && (rev <= 10))
  1478. filename = "b0g0initvals5";
  1479. else if (rev >= 13)
  1480. filename = "lp0initvals13";
  1481. else
  1482. goto err_no_initvals;
  1483. break;
  1484. case B43_PHYTYPE_N:
  1485. if ((rev >= 11) && (rev <= 12))
  1486. filename = "n0initvals11";
  1487. else
  1488. goto err_no_initvals;
  1489. break;
  1490. default:
  1491. goto err_no_initvals;
  1492. }
  1493. err = do_request_fw(dev, filename, &fw->initvals);
  1494. if (err)
  1495. goto err_load;
  1496. /* Get bandswitch initvals */
  1497. switch (dev->phy.type) {
  1498. case B43_PHYTYPE_A:
  1499. if ((rev >= 5) && (rev <= 10)) {
  1500. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1501. filename = "a0g1bsinitvals5";
  1502. else
  1503. filename = "a0g0bsinitvals5";
  1504. } else if (rev >= 11)
  1505. filename = NULL;
  1506. else
  1507. goto err_no_initvals;
  1508. break;
  1509. case B43_PHYTYPE_G:
  1510. if ((rev >= 5) && (rev <= 10))
  1511. filename = "b0g0bsinitvals5";
  1512. else if (rev >= 11)
  1513. filename = NULL;
  1514. else
  1515. goto err_no_initvals;
  1516. break;
  1517. case B43_PHYTYPE_N:
  1518. if ((rev >= 11) && (rev <= 12))
  1519. filename = "n0bsinitvals11";
  1520. else
  1521. goto err_no_initvals;
  1522. break;
  1523. default:
  1524. goto err_no_initvals;
  1525. }
  1526. err = do_request_fw(dev, filename, &fw->initvals_band);
  1527. if (err)
  1528. goto err_load;
  1529. return 0;
  1530. err_load:
  1531. b43_print_fw_helptext(dev->wl, 1);
  1532. goto error;
  1533. err_no_ucode:
  1534. err = -ENODEV;
  1535. b43err(dev->wl, "No microcode available for core rev %u\n", rev);
  1536. goto error;
  1537. err_no_pcm:
  1538. err = -ENODEV;
  1539. b43err(dev->wl, "No PCM available for core rev %u\n", rev);
  1540. goto error;
  1541. err_no_initvals:
  1542. err = -ENODEV;
  1543. b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
  1544. "core rev %u\n", dev->phy.type, rev);
  1545. goto error;
  1546. error:
  1547. b43_release_firmware(dev);
  1548. return err;
  1549. }
  1550. static int b43_upload_microcode(struct b43_wldev *dev)
  1551. {
  1552. const size_t hdr_len = sizeof(struct b43_fw_header);
  1553. const __be32 *data;
  1554. unsigned int i, len;
  1555. u16 fwrev, fwpatch, fwdate, fwtime;
  1556. u32 tmp, macctl;
  1557. int err = 0;
  1558. /* Jump the microcode PSM to offset 0 */
  1559. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1560. B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
  1561. macctl |= B43_MACCTL_PSM_JMP0;
  1562. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1563. /* Zero out all microcode PSM registers and shared memory. */
  1564. for (i = 0; i < 64; i++)
  1565. b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
  1566. for (i = 0; i < 4096; i += 2)
  1567. b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
  1568. /* Upload Microcode. */
  1569. data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
  1570. len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
  1571. b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
  1572. for (i = 0; i < len; i++) {
  1573. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1574. udelay(10);
  1575. }
  1576. if (dev->fw.pcm.data) {
  1577. /* Upload PCM data. */
  1578. data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
  1579. len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
  1580. b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
  1581. b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
  1582. /* No need for autoinc bit in SHM_HW */
  1583. b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
  1584. for (i = 0; i < len; i++) {
  1585. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1586. udelay(10);
  1587. }
  1588. }
  1589. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
  1590. /* Start the microcode PSM */
  1591. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1592. macctl &= ~B43_MACCTL_PSM_JMP0;
  1593. macctl |= B43_MACCTL_PSM_RUN;
  1594. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1595. /* Wait for the microcode to load and respond */
  1596. i = 0;
  1597. while (1) {
  1598. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1599. if (tmp == B43_IRQ_MAC_SUSPENDED)
  1600. break;
  1601. i++;
  1602. if (i >= 20) {
  1603. b43err(dev->wl, "Microcode not responding\n");
  1604. b43_print_fw_helptext(dev->wl, 1);
  1605. err = -ENODEV;
  1606. goto error;
  1607. }
  1608. msleep_interruptible(50);
  1609. if (signal_pending(current)) {
  1610. err = -EINTR;
  1611. goto error;
  1612. }
  1613. }
  1614. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
  1615. /* Get and check the revisions. */
  1616. fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
  1617. fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
  1618. fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
  1619. fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
  1620. if (fwrev <= 0x128) {
  1621. b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
  1622. "binary drivers older than version 4.x is unsupported. "
  1623. "You must upgrade your firmware files.\n");
  1624. b43_print_fw_helptext(dev->wl, 1);
  1625. err = -EOPNOTSUPP;
  1626. goto error;
  1627. }
  1628. b43info(dev->wl, "Loading firmware version %u.%u "
  1629. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
  1630. fwrev, fwpatch,
  1631. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1632. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
  1633. dev->fw.rev = fwrev;
  1634. dev->fw.patch = fwpatch;
  1635. if (b43_is_old_txhdr_format(dev)) {
  1636. b43warn(dev->wl, "You are using an old firmware image. "
  1637. "Support for old firmware will be removed in July 2008.\n");
  1638. b43_print_fw_helptext(dev->wl, 0);
  1639. }
  1640. return 0;
  1641. error:
  1642. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1643. macctl &= ~B43_MACCTL_PSM_RUN;
  1644. macctl |= B43_MACCTL_PSM_JMP0;
  1645. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1646. return err;
  1647. }
  1648. static int b43_write_initvals(struct b43_wldev *dev,
  1649. const struct b43_iv *ivals,
  1650. size_t count,
  1651. size_t array_size)
  1652. {
  1653. const struct b43_iv *iv;
  1654. u16 offset;
  1655. size_t i;
  1656. bool bit32;
  1657. BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
  1658. iv = ivals;
  1659. for (i = 0; i < count; i++) {
  1660. if (array_size < sizeof(iv->offset_size))
  1661. goto err_format;
  1662. array_size -= sizeof(iv->offset_size);
  1663. offset = be16_to_cpu(iv->offset_size);
  1664. bit32 = !!(offset & B43_IV_32BIT);
  1665. offset &= B43_IV_OFFSET_MASK;
  1666. if (offset >= 0x1000)
  1667. goto err_format;
  1668. if (bit32) {
  1669. u32 value;
  1670. if (array_size < sizeof(iv->data.d32))
  1671. goto err_format;
  1672. array_size -= sizeof(iv->data.d32);
  1673. value = be32_to_cpu(get_unaligned(&iv->data.d32));
  1674. b43_write32(dev, offset, value);
  1675. iv = (const struct b43_iv *)((const uint8_t *)iv +
  1676. sizeof(__be16) +
  1677. sizeof(__be32));
  1678. } else {
  1679. u16 value;
  1680. if (array_size < sizeof(iv->data.d16))
  1681. goto err_format;
  1682. array_size -= sizeof(iv->data.d16);
  1683. value = be16_to_cpu(iv->data.d16);
  1684. b43_write16(dev, offset, value);
  1685. iv = (const struct b43_iv *)((const uint8_t *)iv +
  1686. sizeof(__be16) +
  1687. sizeof(__be16));
  1688. }
  1689. }
  1690. if (array_size)
  1691. goto err_format;
  1692. return 0;
  1693. err_format:
  1694. b43err(dev->wl, "Initial Values Firmware file-format error.\n");
  1695. b43_print_fw_helptext(dev->wl, 1);
  1696. return -EPROTO;
  1697. }
  1698. static int b43_upload_initvals(struct b43_wldev *dev)
  1699. {
  1700. const size_t hdr_len = sizeof(struct b43_fw_header);
  1701. const struct b43_fw_header *hdr;
  1702. struct b43_firmware *fw = &dev->fw;
  1703. const struct b43_iv *ivals;
  1704. size_t count;
  1705. int err;
  1706. hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
  1707. ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
  1708. count = be32_to_cpu(hdr->size);
  1709. err = b43_write_initvals(dev, ivals, count,
  1710. fw->initvals.data->size - hdr_len);
  1711. if (err)
  1712. goto out;
  1713. if (fw->initvals_band.data) {
  1714. hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
  1715. ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
  1716. count = be32_to_cpu(hdr->size);
  1717. err = b43_write_initvals(dev, ivals, count,
  1718. fw->initvals_band.data->size - hdr_len);
  1719. if (err)
  1720. goto out;
  1721. }
  1722. out:
  1723. return err;
  1724. }
  1725. /* Initialize the GPIOs
  1726. * http://bcm-specs.sipsolutions.net/GPIO
  1727. */
  1728. static int b43_gpio_init(struct b43_wldev *dev)
  1729. {
  1730. struct ssb_bus *bus = dev->dev->bus;
  1731. struct ssb_device *gpiodev, *pcidev = NULL;
  1732. u32 mask, set;
  1733. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1734. & ~B43_MACCTL_GPOUTSMSK);
  1735. b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
  1736. | 0x000F);
  1737. mask = 0x0000001F;
  1738. set = 0x0000000F;
  1739. if (dev->dev->bus->chip_id == 0x4301) {
  1740. mask |= 0x0060;
  1741. set |= 0x0060;
  1742. }
  1743. if (0 /* FIXME: conditional unknown */ ) {
  1744. b43_write16(dev, B43_MMIO_GPIO_MASK,
  1745. b43_read16(dev, B43_MMIO_GPIO_MASK)
  1746. | 0x0100);
  1747. mask |= 0x0180;
  1748. set |= 0x0180;
  1749. }
  1750. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
  1751. b43_write16(dev, B43_MMIO_GPIO_MASK,
  1752. b43_read16(dev, B43_MMIO_GPIO_MASK)
  1753. | 0x0200);
  1754. mask |= 0x0200;
  1755. set |= 0x0200;
  1756. }
  1757. if (dev->dev->id.revision >= 2)
  1758. mask |= 0x0010; /* FIXME: This is redundant. */
  1759. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1760. pcidev = bus->pcicore.dev;
  1761. #endif
  1762. gpiodev = bus->chipco.dev ? : pcidev;
  1763. if (!gpiodev)
  1764. return 0;
  1765. ssb_write32(gpiodev, B43_GPIO_CONTROL,
  1766. (ssb_read32(gpiodev, B43_GPIO_CONTROL)
  1767. & mask) | set);
  1768. return 0;
  1769. }
  1770. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  1771. static void b43_gpio_cleanup(struct b43_wldev *dev)
  1772. {
  1773. struct ssb_bus *bus = dev->dev->bus;
  1774. struct ssb_device *gpiodev, *pcidev = NULL;
  1775. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1776. pcidev = bus->pcicore.dev;
  1777. #endif
  1778. gpiodev = bus->chipco.dev ? : pcidev;
  1779. if (!gpiodev)
  1780. return;
  1781. ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
  1782. }
  1783. /* http://bcm-specs.sipsolutions.net/EnableMac */
  1784. void b43_mac_enable(struct b43_wldev *dev)
  1785. {
  1786. dev->mac_suspended--;
  1787. B43_WARN_ON(dev->mac_suspended < 0);
  1788. B43_WARN_ON(irqs_disabled());
  1789. if (dev->mac_suspended == 0) {
  1790. b43_write32(dev, B43_MMIO_MACCTL,
  1791. b43_read32(dev, B43_MMIO_MACCTL)
  1792. | B43_MACCTL_ENABLED);
  1793. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  1794. B43_IRQ_MAC_SUSPENDED);
  1795. /* Commit writes */
  1796. b43_read32(dev, B43_MMIO_MACCTL);
  1797. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1798. b43_power_saving_ctl_bits(dev, 0);
  1799. /* Re-enable IRQs. */
  1800. spin_lock_irq(&dev->wl->irq_lock);
  1801. b43_interrupt_enable(dev, dev->irq_savedstate);
  1802. spin_unlock_irq(&dev->wl->irq_lock);
  1803. }
  1804. }
  1805. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1806. void b43_mac_suspend(struct b43_wldev *dev)
  1807. {
  1808. int i;
  1809. u32 tmp;
  1810. might_sleep();
  1811. B43_WARN_ON(irqs_disabled());
  1812. B43_WARN_ON(dev->mac_suspended < 0);
  1813. if (dev->mac_suspended == 0) {
  1814. /* Mask IRQs before suspending MAC. Otherwise
  1815. * the MAC stays busy and won't suspend. */
  1816. spin_lock_irq(&dev->wl->irq_lock);
  1817. tmp = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1818. spin_unlock_irq(&dev->wl->irq_lock);
  1819. b43_synchronize_irq(dev);
  1820. dev->irq_savedstate = tmp;
  1821. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  1822. b43_write32(dev, B43_MMIO_MACCTL,
  1823. b43_read32(dev, B43_MMIO_MACCTL)
  1824. & ~B43_MACCTL_ENABLED);
  1825. /* force pci to flush the write */
  1826. b43_read32(dev, B43_MMIO_MACCTL);
  1827. for (i = 40; i; i--) {
  1828. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1829. if (tmp & B43_IRQ_MAC_SUSPENDED)
  1830. goto out;
  1831. msleep(1);
  1832. }
  1833. b43err(dev->wl, "MAC suspend failed\n");
  1834. }
  1835. out:
  1836. dev->mac_suspended++;
  1837. }
  1838. static void b43_adjust_opmode(struct b43_wldev *dev)
  1839. {
  1840. struct b43_wl *wl = dev->wl;
  1841. u32 ctl;
  1842. u16 cfp_pretbtt;
  1843. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  1844. /* Reset status to STA infrastructure mode. */
  1845. ctl &= ~B43_MACCTL_AP;
  1846. ctl &= ~B43_MACCTL_KEEP_CTL;
  1847. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  1848. ctl &= ~B43_MACCTL_KEEP_BAD;
  1849. ctl &= ~B43_MACCTL_PROMISC;
  1850. ctl &= ~B43_MACCTL_BEACPROMISC;
  1851. ctl |= B43_MACCTL_INFRA;
  1852. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  1853. ctl |= B43_MACCTL_AP;
  1854. else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
  1855. ctl &= ~B43_MACCTL_INFRA;
  1856. if (wl->filter_flags & FIF_CONTROL)
  1857. ctl |= B43_MACCTL_KEEP_CTL;
  1858. if (wl->filter_flags & FIF_FCSFAIL)
  1859. ctl |= B43_MACCTL_KEEP_BAD;
  1860. if (wl->filter_flags & FIF_PLCPFAIL)
  1861. ctl |= B43_MACCTL_KEEP_BADPLCP;
  1862. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1863. ctl |= B43_MACCTL_PROMISC;
  1864. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1865. ctl |= B43_MACCTL_BEACPROMISC;
  1866. /* Workaround: On old hardware the HW-MAC-address-filter
  1867. * doesn't work properly, so always run promisc in filter
  1868. * it in software. */
  1869. if (dev->dev->id.revision <= 4)
  1870. ctl |= B43_MACCTL_PROMISC;
  1871. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  1872. cfp_pretbtt = 2;
  1873. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  1874. if (dev->dev->bus->chip_id == 0x4306 &&
  1875. dev->dev->bus->chip_rev == 3)
  1876. cfp_pretbtt = 100;
  1877. else
  1878. cfp_pretbtt = 50;
  1879. }
  1880. b43_write16(dev, 0x612, cfp_pretbtt);
  1881. }
  1882. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  1883. {
  1884. u16 offset;
  1885. if (is_ofdm) {
  1886. offset = 0x480;
  1887. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1888. } else {
  1889. offset = 0x4C0;
  1890. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1891. }
  1892. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  1893. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  1894. }
  1895. static void b43_rate_memory_init(struct b43_wldev *dev)
  1896. {
  1897. switch (dev->phy.type) {
  1898. case B43_PHYTYPE_A:
  1899. case B43_PHYTYPE_G:
  1900. case B43_PHYTYPE_N:
  1901. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  1902. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  1903. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  1904. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  1905. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  1906. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  1907. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  1908. if (dev->phy.type == B43_PHYTYPE_A)
  1909. break;
  1910. /* fallthrough */
  1911. case B43_PHYTYPE_B:
  1912. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  1913. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  1914. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  1915. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  1916. break;
  1917. default:
  1918. B43_WARN_ON(1);
  1919. }
  1920. }
  1921. /* Set the TX-Antenna for management frames sent by firmware. */
  1922. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  1923. {
  1924. u16 ant = 0;
  1925. u16 tmp;
  1926. switch (antenna) {
  1927. case B43_ANTENNA0:
  1928. ant |= B43_TXH_PHY_ANT0;
  1929. break;
  1930. case B43_ANTENNA1:
  1931. ant |= B43_TXH_PHY_ANT1;
  1932. break;
  1933. case B43_ANTENNA2:
  1934. ant |= B43_TXH_PHY_ANT2;
  1935. break;
  1936. case B43_ANTENNA3:
  1937. ant |= B43_TXH_PHY_ANT3;
  1938. break;
  1939. case B43_ANTENNA_AUTO:
  1940. ant |= B43_TXH_PHY_ANT01AUTO;
  1941. break;
  1942. default:
  1943. B43_WARN_ON(1);
  1944. }
  1945. /* FIXME We also need to set the other flags of the PHY control field somewhere. */
  1946. /* For Beacons */
  1947. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1948. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  1949. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, tmp);
  1950. /* For ACK/CTS */
  1951. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  1952. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  1953. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  1954. /* For Probe Resposes */
  1955. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  1956. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  1957. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  1958. }
  1959. /* This is the opposite of b43_chip_init() */
  1960. static void b43_chip_exit(struct b43_wldev *dev)
  1961. {
  1962. b43_radio_turn_off(dev, 1);
  1963. b43_gpio_cleanup(dev);
  1964. /* firmware is released later */
  1965. }
  1966. /* Initialize the chip
  1967. * http://bcm-specs.sipsolutions.net/ChipInit
  1968. */
  1969. static int b43_chip_init(struct b43_wldev *dev)
  1970. {
  1971. struct b43_phy *phy = &dev->phy;
  1972. int err, tmp;
  1973. u32 value32, macctl;
  1974. u16 value16;
  1975. /* Initialize the MAC control */
  1976. macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
  1977. if (dev->phy.gmode)
  1978. macctl |= B43_MACCTL_GMODE;
  1979. macctl |= B43_MACCTL_INFRA;
  1980. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1981. err = b43_request_firmware(dev);
  1982. if (err)
  1983. goto out;
  1984. err = b43_upload_microcode(dev);
  1985. if (err)
  1986. goto out; /* firmware is released later */
  1987. err = b43_gpio_init(dev);
  1988. if (err)
  1989. goto out; /* firmware is released later */
  1990. err = b43_upload_initvals(dev);
  1991. if (err)
  1992. goto err_gpio_clean;
  1993. b43_radio_turn_on(dev);
  1994. b43_write16(dev, 0x03E6, 0x0000);
  1995. err = b43_phy_init(dev);
  1996. if (err)
  1997. goto err_radio_off;
  1998. /* Select initial Interference Mitigation. */
  1999. tmp = phy->interfmode;
  2000. phy->interfmode = B43_INTERFMODE_NONE;
  2001. b43_radio_set_interference_mitigation(dev, tmp);
  2002. b43_set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  2003. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  2004. if (phy->type == B43_PHYTYPE_B) {
  2005. value16 = b43_read16(dev, 0x005E);
  2006. value16 |= 0x0004;
  2007. b43_write16(dev, 0x005E, value16);
  2008. }
  2009. b43_write32(dev, 0x0100, 0x01000000);
  2010. if (dev->dev->id.revision < 5)
  2011. b43_write32(dev, 0x010C, 0x01000000);
  2012. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2013. & ~B43_MACCTL_INFRA);
  2014. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2015. | B43_MACCTL_INFRA);
  2016. /* Probe Response Timeout value */
  2017. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  2018. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  2019. /* Initially set the wireless operation mode. */
  2020. b43_adjust_opmode(dev);
  2021. if (dev->dev->id.revision < 3) {
  2022. b43_write16(dev, 0x060E, 0x0000);
  2023. b43_write16(dev, 0x0610, 0x8000);
  2024. b43_write16(dev, 0x0604, 0x0000);
  2025. b43_write16(dev, 0x0606, 0x0200);
  2026. } else {
  2027. b43_write32(dev, 0x0188, 0x80000000);
  2028. b43_write32(dev, 0x018C, 0x02000000);
  2029. }
  2030. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  2031. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  2032. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  2033. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  2034. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  2035. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  2036. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  2037. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  2038. value32 |= 0x00100000;
  2039. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  2040. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  2041. dev->dev->bus->chipco.fast_pwrup_delay);
  2042. err = 0;
  2043. b43dbg(dev->wl, "Chip initialized\n");
  2044. out:
  2045. return err;
  2046. err_radio_off:
  2047. b43_radio_turn_off(dev, 1);
  2048. err_gpio_clean:
  2049. b43_gpio_cleanup(dev);
  2050. return err;
  2051. }
  2052. static void b43_periodic_every120sec(struct b43_wldev *dev)
  2053. {
  2054. struct b43_phy *phy = &dev->phy;
  2055. if (phy->type != B43_PHYTYPE_G || phy->rev < 2)
  2056. return;
  2057. b43_mac_suspend(dev);
  2058. b43_lo_g_measure(dev);
  2059. b43_mac_enable(dev);
  2060. if (b43_has_hardware_pctl(phy))
  2061. b43_lo_g_ctl_mark_all_unused(dev);
  2062. }
  2063. static void b43_periodic_every60sec(struct b43_wldev *dev)
  2064. {
  2065. struct b43_phy *phy = &dev->phy;
  2066. if (phy->type != B43_PHYTYPE_G)
  2067. return;
  2068. if (!b43_has_hardware_pctl(phy))
  2069. b43_lo_g_ctl_mark_all_unused(dev);
  2070. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI) {
  2071. b43_mac_suspend(dev);
  2072. b43_calc_nrssi_slope(dev);
  2073. if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 8)) {
  2074. u8 old_chan = phy->channel;
  2075. /* VCO Calibration */
  2076. if (old_chan >= 8)
  2077. b43_radio_selectchannel(dev, 1, 0);
  2078. else
  2079. b43_radio_selectchannel(dev, 13, 0);
  2080. b43_radio_selectchannel(dev, old_chan, 0);
  2081. }
  2082. b43_mac_enable(dev);
  2083. }
  2084. }
  2085. static void b43_periodic_every30sec(struct b43_wldev *dev)
  2086. {
  2087. /* Update device statistics. */
  2088. b43_calculate_link_quality(dev);
  2089. }
  2090. static void b43_periodic_every15sec(struct b43_wldev *dev)
  2091. {
  2092. struct b43_phy *phy = &dev->phy;
  2093. if (phy->type == B43_PHYTYPE_G) {
  2094. //TODO: update_aci_moving_average
  2095. if (phy->aci_enable && phy->aci_wlan_automatic) {
  2096. b43_mac_suspend(dev);
  2097. if (!phy->aci_enable && 1 /*TODO: not scanning? */ ) {
  2098. if (0 /*TODO: bunch of conditions */ ) {
  2099. b43_radio_set_interference_mitigation
  2100. (dev, B43_INTERFMODE_MANUALWLAN);
  2101. }
  2102. } else if (1 /*TODO*/) {
  2103. /*
  2104. if ((aci_average > 1000) && !(b43_radio_aci_scan(dev))) {
  2105. b43_radio_set_interference_mitigation(dev,
  2106. B43_INTERFMODE_NONE);
  2107. }
  2108. */
  2109. }
  2110. b43_mac_enable(dev);
  2111. } else if (phy->interfmode == B43_INTERFMODE_NONWLAN &&
  2112. phy->rev == 1) {
  2113. //TODO: implement rev1 workaround
  2114. }
  2115. }
  2116. b43_phy_xmitpower(dev); //FIXME: unless scanning?
  2117. //TODO for APHY (temperature?)
  2118. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2119. wmb();
  2120. }
  2121. static void do_periodic_work(struct b43_wldev *dev)
  2122. {
  2123. unsigned int state;
  2124. state = dev->periodic_state;
  2125. if (state % 8 == 0)
  2126. b43_periodic_every120sec(dev);
  2127. if (state % 4 == 0)
  2128. b43_periodic_every60sec(dev);
  2129. if (state % 2 == 0)
  2130. b43_periodic_every30sec(dev);
  2131. b43_periodic_every15sec(dev);
  2132. }
  2133. /* Periodic work locking policy:
  2134. * The whole periodic work handler is protected by
  2135. * wl->mutex. If another lock is needed somewhere in the
  2136. * pwork callchain, it's aquired in-place, where it's needed.
  2137. */
  2138. static void b43_periodic_work_handler(struct work_struct *work)
  2139. {
  2140. struct b43_wldev *dev = container_of(work, struct b43_wldev,
  2141. periodic_work.work);
  2142. struct b43_wl *wl = dev->wl;
  2143. unsigned long delay;
  2144. mutex_lock(&wl->mutex);
  2145. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2146. goto out;
  2147. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2148. goto out_requeue;
  2149. do_periodic_work(dev);
  2150. dev->periodic_state++;
  2151. out_requeue:
  2152. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2153. delay = msecs_to_jiffies(50);
  2154. else
  2155. delay = round_jiffies_relative(HZ * 15);
  2156. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  2157. out:
  2158. mutex_unlock(&wl->mutex);
  2159. }
  2160. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2161. {
  2162. struct delayed_work *work = &dev->periodic_work;
  2163. dev->periodic_state = 0;
  2164. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2165. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2166. }
  2167. /* Check if communication with the device works correctly. */
  2168. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2169. {
  2170. u32 v, backup;
  2171. backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2172. /* Check for read/write and endianness problems. */
  2173. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2174. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2175. goto error;
  2176. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2177. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2178. goto error;
  2179. b43_shm_write32(dev, B43_SHM_SHARED, 0, backup);
  2180. if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
  2181. /* The 32bit register shadows the two 16bit registers
  2182. * with update sideeffects. Validate this. */
  2183. b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
  2184. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
  2185. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
  2186. goto error;
  2187. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
  2188. goto error;
  2189. }
  2190. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
  2191. v = b43_read32(dev, B43_MMIO_MACCTL);
  2192. v |= B43_MACCTL_GMODE;
  2193. if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2194. goto error;
  2195. return 0;
  2196. error:
  2197. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2198. return -ENODEV;
  2199. }
  2200. static void b43_security_init(struct b43_wldev *dev)
  2201. {
  2202. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2203. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2204. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2205. /* KTP is a word address, but we address SHM bytewise.
  2206. * So multiply by two.
  2207. */
  2208. dev->ktp *= 2;
  2209. if (dev->dev->id.revision >= 5) {
  2210. /* Number of RCMTA address slots */
  2211. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2212. }
  2213. b43_clear_keys(dev);
  2214. }
  2215. static int b43_rng_read(struct hwrng *rng, u32 * data)
  2216. {
  2217. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2218. unsigned long flags;
  2219. /* Don't take wl->mutex here, as it could deadlock with
  2220. * hwrng internal locking. It's not needed to take
  2221. * wl->mutex here, anyway. */
  2222. spin_lock_irqsave(&wl->irq_lock, flags);
  2223. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2224. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2225. return (sizeof(u16));
  2226. }
  2227. static void b43_rng_exit(struct b43_wl *wl, bool suspended)
  2228. {
  2229. if (wl->rng_initialized)
  2230. __hwrng_unregister(&wl->rng, suspended);
  2231. }
  2232. static int b43_rng_init(struct b43_wl *wl)
  2233. {
  2234. int err;
  2235. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2236. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2237. wl->rng.name = wl->rng_name;
  2238. wl->rng.data_read = b43_rng_read;
  2239. wl->rng.priv = (unsigned long)wl;
  2240. wl->rng_initialized = 1;
  2241. err = hwrng_register(&wl->rng);
  2242. if (err) {
  2243. wl->rng_initialized = 0;
  2244. b43err(wl, "Failed to register the random "
  2245. "number generator (%d)\n", err);
  2246. }
  2247. return err;
  2248. }
  2249. static int b43_op_tx(struct ieee80211_hw *hw,
  2250. struct sk_buff *skb,
  2251. struct ieee80211_tx_control *ctl)
  2252. {
  2253. struct b43_wl *wl = hw_to_b43_wl(hw);
  2254. struct b43_wldev *dev = wl->current_dev;
  2255. int err = -ENODEV;
  2256. if (unlikely(!dev))
  2257. goto out;
  2258. if (unlikely(b43_status(dev) < B43_STAT_STARTED))
  2259. goto out;
  2260. /* DMA-TX is done without a global lock. */
  2261. err = b43_dma_tx(dev, skb, ctl);
  2262. out:
  2263. if (unlikely(err))
  2264. return NETDEV_TX_BUSY;
  2265. return NETDEV_TX_OK;
  2266. }
  2267. static int b43_op_conf_tx(struct ieee80211_hw *hw,
  2268. int queue,
  2269. const struct ieee80211_tx_queue_params *params)
  2270. {
  2271. return 0;
  2272. }
  2273. static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
  2274. struct ieee80211_tx_queue_stats *stats)
  2275. {
  2276. struct b43_wl *wl = hw_to_b43_wl(hw);
  2277. struct b43_wldev *dev = wl->current_dev;
  2278. unsigned long flags;
  2279. int err = -ENODEV;
  2280. if (!dev)
  2281. goto out;
  2282. spin_lock_irqsave(&wl->irq_lock, flags);
  2283. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2284. b43_dma_get_tx_stats(dev, stats);
  2285. err = 0;
  2286. }
  2287. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2288. out:
  2289. return err;
  2290. }
  2291. static int b43_op_get_stats(struct ieee80211_hw *hw,
  2292. struct ieee80211_low_level_stats *stats)
  2293. {
  2294. struct b43_wl *wl = hw_to_b43_wl(hw);
  2295. unsigned long flags;
  2296. spin_lock_irqsave(&wl->irq_lock, flags);
  2297. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2298. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2299. return 0;
  2300. }
  2301. static const char *phymode_to_string(unsigned int phymode)
  2302. {
  2303. switch (phymode) {
  2304. case B43_PHYMODE_A:
  2305. return "A";
  2306. case B43_PHYMODE_B:
  2307. return "B";
  2308. case B43_PHYMODE_G:
  2309. return "G";
  2310. default:
  2311. B43_WARN_ON(1);
  2312. }
  2313. return "";
  2314. }
  2315. static int find_wldev_for_phymode(struct b43_wl *wl,
  2316. unsigned int phymode,
  2317. struct b43_wldev **dev, bool * gmode)
  2318. {
  2319. struct b43_wldev *d;
  2320. list_for_each_entry(d, &wl->devlist, list) {
  2321. if (d->phy.possible_phymodes & phymode) {
  2322. /* Ok, this device supports the PHY-mode.
  2323. * Now figure out how the gmode bit has to be
  2324. * set to support it. */
  2325. if (phymode == B43_PHYMODE_A)
  2326. *gmode = 0;
  2327. else
  2328. *gmode = 1;
  2329. *dev = d;
  2330. return 0;
  2331. }
  2332. }
  2333. return -ESRCH;
  2334. }
  2335. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2336. {
  2337. struct ssb_device *sdev = dev->dev;
  2338. u32 tmslow;
  2339. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2340. tmslow &= ~B43_TMSLOW_GMODE;
  2341. tmslow |= B43_TMSLOW_PHYRESET;
  2342. tmslow |= SSB_TMSLOW_FGC;
  2343. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2344. msleep(1);
  2345. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2346. tmslow &= ~SSB_TMSLOW_FGC;
  2347. tmslow |= B43_TMSLOW_PHYRESET;
  2348. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2349. msleep(1);
  2350. }
  2351. /* Expects wl->mutex locked */
  2352. static int b43_switch_phymode(struct b43_wl *wl, unsigned int new_mode)
  2353. {
  2354. struct b43_wldev *up_dev;
  2355. struct b43_wldev *down_dev;
  2356. int err;
  2357. bool gmode = 0;
  2358. int prev_status;
  2359. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2360. if (err) {
  2361. b43err(wl, "Could not find a device for %s-PHY mode\n",
  2362. phymode_to_string(new_mode));
  2363. return err;
  2364. }
  2365. if ((up_dev == wl->current_dev) &&
  2366. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2367. /* This device is already running. */
  2368. return 0;
  2369. }
  2370. b43dbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2371. phymode_to_string(new_mode));
  2372. down_dev = wl->current_dev;
  2373. prev_status = b43_status(down_dev);
  2374. /* Shutdown the currently running core. */
  2375. if (prev_status >= B43_STAT_STARTED)
  2376. b43_wireless_core_stop(down_dev);
  2377. if (prev_status >= B43_STAT_INITIALIZED)
  2378. b43_wireless_core_exit(down_dev);
  2379. if (down_dev != up_dev) {
  2380. /* We switch to a different core, so we put PHY into
  2381. * RESET on the old core. */
  2382. b43_put_phy_into_reset(down_dev);
  2383. }
  2384. /* Now start the new core. */
  2385. up_dev->phy.gmode = gmode;
  2386. if (prev_status >= B43_STAT_INITIALIZED) {
  2387. err = b43_wireless_core_init(up_dev);
  2388. if (err) {
  2389. b43err(wl, "Fatal: Could not initialize device for "
  2390. "newly selected %s-PHY mode\n",
  2391. phymode_to_string(new_mode));
  2392. goto init_failure;
  2393. }
  2394. }
  2395. if (prev_status >= B43_STAT_STARTED) {
  2396. err = b43_wireless_core_start(up_dev);
  2397. if (err) {
  2398. b43err(wl, "Fatal: Coult not start device for "
  2399. "newly selected %s-PHY mode\n",
  2400. phymode_to_string(new_mode));
  2401. b43_wireless_core_exit(up_dev);
  2402. goto init_failure;
  2403. }
  2404. }
  2405. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2406. wl->current_dev = up_dev;
  2407. return 0;
  2408. init_failure:
  2409. /* Whoops, failed to init the new core. No core is operating now. */
  2410. wl->current_dev = NULL;
  2411. return err;
  2412. }
  2413. /* Check if the use of the antenna that ieee80211 told us to
  2414. * use is possible. This will fall back to DEFAULT.
  2415. * "antenna_nr" is the antenna identifier we got from ieee80211. */
  2416. u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
  2417. u8 antenna_nr)
  2418. {
  2419. u8 antenna_mask;
  2420. if (antenna_nr == 0) {
  2421. /* Zero means "use default antenna". That's always OK. */
  2422. return 0;
  2423. }
  2424. /* Get the mask of available antennas. */
  2425. if (dev->phy.gmode)
  2426. antenna_mask = dev->dev->bus->sprom.ant_available_bg;
  2427. else
  2428. antenna_mask = dev->dev->bus->sprom.ant_available_a;
  2429. if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
  2430. /* This antenna is not available. Fall back to default. */
  2431. return 0;
  2432. }
  2433. return antenna_nr;
  2434. }
  2435. static int b43_antenna_from_ieee80211(struct b43_wldev *dev, u8 antenna)
  2436. {
  2437. antenna = b43_ieee80211_antenna_sanitize(dev, antenna);
  2438. switch (antenna) {
  2439. case 0: /* default/diversity */
  2440. return B43_ANTENNA_DEFAULT;
  2441. case 1: /* Antenna 0 */
  2442. return B43_ANTENNA0;
  2443. case 2: /* Antenna 1 */
  2444. return B43_ANTENNA1;
  2445. case 3: /* Antenna 2 */
  2446. return B43_ANTENNA2;
  2447. case 4: /* Antenna 3 */
  2448. return B43_ANTENNA3;
  2449. default:
  2450. return B43_ANTENNA_DEFAULT;
  2451. }
  2452. }
  2453. static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  2454. {
  2455. struct b43_wl *wl = hw_to_b43_wl(hw);
  2456. struct b43_wldev *dev;
  2457. struct b43_phy *phy;
  2458. unsigned long flags;
  2459. unsigned int new_phymode = 0xFFFF;
  2460. int antenna;
  2461. int err = 0;
  2462. u32 savedirqs;
  2463. mutex_lock(&wl->mutex);
  2464. /* Switch the PHY mode (if necessary). */
  2465. switch (conf->phymode) {
  2466. case MODE_IEEE80211A:
  2467. new_phymode = B43_PHYMODE_A;
  2468. break;
  2469. case MODE_IEEE80211B:
  2470. new_phymode = B43_PHYMODE_B;
  2471. break;
  2472. case MODE_IEEE80211G:
  2473. new_phymode = B43_PHYMODE_G;
  2474. break;
  2475. default:
  2476. B43_WARN_ON(1);
  2477. }
  2478. err = b43_switch_phymode(wl, new_phymode);
  2479. if (err)
  2480. goto out_unlock_mutex;
  2481. dev = wl->current_dev;
  2482. phy = &dev->phy;
  2483. /* Disable IRQs while reconfiguring the device.
  2484. * This makes it possible to drop the spinlock throughout
  2485. * the reconfiguration process. */
  2486. spin_lock_irqsave(&wl->irq_lock, flags);
  2487. if (b43_status(dev) < B43_STAT_STARTED) {
  2488. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2489. goto out_unlock_mutex;
  2490. }
  2491. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2492. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2493. b43_synchronize_irq(dev);
  2494. /* Switch to the requested channel.
  2495. * The firmware takes care of races with the TX handler. */
  2496. if (conf->channel_val != phy->channel)
  2497. b43_radio_selectchannel(dev, conf->channel_val, 0);
  2498. /* Enable/Disable ShortSlot timing. */
  2499. if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
  2500. dev->short_slot) {
  2501. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  2502. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
  2503. b43_short_slot_timing_enable(dev);
  2504. else
  2505. b43_short_slot_timing_disable(dev);
  2506. }
  2507. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  2508. /* Adjust the desired TX power level. */
  2509. if (conf->power_level != 0) {
  2510. if (conf->power_level != phy->power_level) {
  2511. phy->power_level = conf->power_level;
  2512. b43_phy_xmitpower(dev);
  2513. }
  2514. }
  2515. /* Antennas for RX and management frame TX. */
  2516. antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_tx);
  2517. b43_mgmtframe_txantenna(dev, antenna);
  2518. antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_rx);
  2519. b43_set_rx_antenna(dev, antenna);
  2520. /* Update templates for AP mode. */
  2521. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  2522. b43_set_beacon_int(dev, conf->beacon_int);
  2523. if (!!conf->radio_enabled != phy->radio_on) {
  2524. if (conf->radio_enabled) {
  2525. b43_radio_turn_on(dev);
  2526. b43info(dev->wl, "Radio turned on by software\n");
  2527. if (!dev->radio_hw_enable) {
  2528. b43info(dev->wl, "The hardware RF-kill button "
  2529. "still turns the radio physically off. "
  2530. "Press the button to turn it on.\n");
  2531. }
  2532. } else {
  2533. b43_radio_turn_off(dev, 0);
  2534. b43info(dev->wl, "Radio turned off by software\n");
  2535. }
  2536. }
  2537. spin_lock_irqsave(&wl->irq_lock, flags);
  2538. b43_interrupt_enable(dev, savedirqs);
  2539. mmiowb();
  2540. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2541. out_unlock_mutex:
  2542. mutex_unlock(&wl->mutex);
  2543. return err;
  2544. }
  2545. static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2546. const u8 *local_addr, const u8 *addr,
  2547. struct ieee80211_key_conf *key)
  2548. {
  2549. struct b43_wl *wl = hw_to_b43_wl(hw);
  2550. struct b43_wldev *dev;
  2551. unsigned long flags;
  2552. u8 algorithm;
  2553. u8 index;
  2554. int err;
  2555. DECLARE_MAC_BUF(mac);
  2556. if (modparam_nohwcrypt)
  2557. return -ENOSPC; /* User disabled HW-crypto */
  2558. mutex_lock(&wl->mutex);
  2559. spin_lock_irqsave(&wl->irq_lock, flags);
  2560. dev = wl->current_dev;
  2561. err = -ENODEV;
  2562. if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
  2563. goto out_unlock;
  2564. err = -EINVAL;
  2565. switch (key->alg) {
  2566. case ALG_WEP:
  2567. if (key->keylen == 5)
  2568. algorithm = B43_SEC_ALGO_WEP40;
  2569. else
  2570. algorithm = B43_SEC_ALGO_WEP104;
  2571. break;
  2572. case ALG_TKIP:
  2573. algorithm = B43_SEC_ALGO_TKIP;
  2574. break;
  2575. case ALG_CCMP:
  2576. algorithm = B43_SEC_ALGO_AES;
  2577. break;
  2578. default:
  2579. B43_WARN_ON(1);
  2580. goto out_unlock;
  2581. }
  2582. index = (u8) (key->keyidx);
  2583. if (index > 3)
  2584. goto out_unlock;
  2585. switch (cmd) {
  2586. case SET_KEY:
  2587. if (algorithm == B43_SEC_ALGO_TKIP) {
  2588. /* FIXME: No TKIP hardware encryption for now. */
  2589. err = -EOPNOTSUPP;
  2590. goto out_unlock;
  2591. }
  2592. if (is_broadcast_ether_addr(addr)) {
  2593. /* addr is FF:FF:FF:FF:FF:FF for default keys */
  2594. err = b43_key_write(dev, index, algorithm,
  2595. key->key, key->keylen, NULL, key);
  2596. } else {
  2597. /*
  2598. * either pairwise key or address is 00:00:00:00:00:00
  2599. * for transmit-only keys
  2600. */
  2601. err = b43_key_write(dev, -1, algorithm,
  2602. key->key, key->keylen, addr, key);
  2603. }
  2604. if (err)
  2605. goto out_unlock;
  2606. if (algorithm == B43_SEC_ALGO_WEP40 ||
  2607. algorithm == B43_SEC_ALGO_WEP104) {
  2608. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  2609. } else {
  2610. b43_hf_write(dev,
  2611. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  2612. }
  2613. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2614. break;
  2615. case DISABLE_KEY: {
  2616. err = b43_key_clear(dev, key->hw_key_idx);
  2617. if (err)
  2618. goto out_unlock;
  2619. break;
  2620. }
  2621. default:
  2622. B43_WARN_ON(1);
  2623. }
  2624. out_unlock:
  2625. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2626. mutex_unlock(&wl->mutex);
  2627. if (!err) {
  2628. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  2629. "mac: %s\n",
  2630. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  2631. print_mac(mac, addr));
  2632. }
  2633. return err;
  2634. }
  2635. static void b43_op_configure_filter(struct ieee80211_hw *hw,
  2636. unsigned int changed, unsigned int *fflags,
  2637. int mc_count, struct dev_addr_list *mc_list)
  2638. {
  2639. struct b43_wl *wl = hw_to_b43_wl(hw);
  2640. struct b43_wldev *dev = wl->current_dev;
  2641. unsigned long flags;
  2642. if (!dev) {
  2643. *fflags = 0;
  2644. return;
  2645. }
  2646. spin_lock_irqsave(&wl->irq_lock, flags);
  2647. *fflags &= FIF_PROMISC_IN_BSS |
  2648. FIF_ALLMULTI |
  2649. FIF_FCSFAIL |
  2650. FIF_PLCPFAIL |
  2651. FIF_CONTROL |
  2652. FIF_OTHER_BSS |
  2653. FIF_BCN_PRBRESP_PROMISC;
  2654. changed &= FIF_PROMISC_IN_BSS |
  2655. FIF_ALLMULTI |
  2656. FIF_FCSFAIL |
  2657. FIF_PLCPFAIL |
  2658. FIF_CONTROL |
  2659. FIF_OTHER_BSS |
  2660. FIF_BCN_PRBRESP_PROMISC;
  2661. wl->filter_flags = *fflags;
  2662. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  2663. b43_adjust_opmode(dev);
  2664. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2665. }
  2666. static int b43_op_config_interface(struct ieee80211_hw *hw,
  2667. struct ieee80211_vif *vif,
  2668. struct ieee80211_if_conf *conf)
  2669. {
  2670. struct b43_wl *wl = hw_to_b43_wl(hw);
  2671. struct b43_wldev *dev = wl->current_dev;
  2672. unsigned long flags;
  2673. if (!dev)
  2674. return -ENODEV;
  2675. mutex_lock(&wl->mutex);
  2676. spin_lock_irqsave(&wl->irq_lock, flags);
  2677. B43_WARN_ON(wl->vif != vif);
  2678. if (conf->bssid)
  2679. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2680. else
  2681. memset(wl->bssid, 0, ETH_ALEN);
  2682. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  2683. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
  2684. B43_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
  2685. b43_set_ssid(dev, conf->ssid, conf->ssid_len);
  2686. if (conf->beacon)
  2687. b43_update_templates(wl, conf->beacon);
  2688. }
  2689. b43_write_mac_bssid_templates(dev);
  2690. }
  2691. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2692. mutex_unlock(&wl->mutex);
  2693. return 0;
  2694. }
  2695. /* Locking: wl->mutex */
  2696. static void b43_wireless_core_stop(struct b43_wldev *dev)
  2697. {
  2698. struct b43_wl *wl = dev->wl;
  2699. unsigned long flags;
  2700. if (b43_status(dev) < B43_STAT_STARTED)
  2701. return;
  2702. /* Disable and sync interrupts. We must do this before than
  2703. * setting the status to INITIALIZED, as the interrupt handler
  2704. * won't care about IRQs then. */
  2705. spin_lock_irqsave(&wl->irq_lock, flags);
  2706. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2707. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  2708. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2709. b43_synchronize_irq(dev);
  2710. b43_set_status(dev, B43_STAT_INITIALIZED);
  2711. mutex_unlock(&wl->mutex);
  2712. /* Must unlock as it would otherwise deadlock. No races here.
  2713. * Cancel the possibly running self-rearming periodic work. */
  2714. cancel_delayed_work_sync(&dev->periodic_work);
  2715. mutex_lock(&wl->mutex);
  2716. ieee80211_stop_queues(wl->hw); //FIXME this could cause a deadlock, as mac80211 seems buggy.
  2717. b43_mac_suspend(dev);
  2718. free_irq(dev->dev->irq, dev);
  2719. b43dbg(wl, "Wireless interface stopped\n");
  2720. }
  2721. /* Locking: wl->mutex */
  2722. static int b43_wireless_core_start(struct b43_wldev *dev)
  2723. {
  2724. int err;
  2725. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  2726. drain_txstatus_queue(dev);
  2727. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  2728. IRQF_SHARED, KBUILD_MODNAME, dev);
  2729. if (err) {
  2730. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  2731. goto out;
  2732. }
  2733. /* We are ready to run. */
  2734. b43_set_status(dev, B43_STAT_STARTED);
  2735. /* Start data flow (TX/RX). */
  2736. b43_mac_enable(dev);
  2737. b43_interrupt_enable(dev, dev->irq_savedstate);
  2738. ieee80211_start_queues(dev->wl->hw);
  2739. /* Start maintainance work */
  2740. b43_periodic_tasks_setup(dev);
  2741. b43dbg(dev->wl, "Wireless interface started\n");
  2742. out:
  2743. return err;
  2744. }
  2745. /* Get PHY and RADIO versioning numbers */
  2746. static int b43_phy_versioning(struct b43_wldev *dev)
  2747. {
  2748. struct b43_phy *phy = &dev->phy;
  2749. u32 tmp;
  2750. u8 analog_type;
  2751. u8 phy_type;
  2752. u8 phy_rev;
  2753. u16 radio_manuf;
  2754. u16 radio_ver;
  2755. u16 radio_rev;
  2756. int unsupported = 0;
  2757. /* Get PHY versioning */
  2758. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  2759. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  2760. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  2761. phy_rev = (tmp & B43_PHYVER_VERSION);
  2762. switch (phy_type) {
  2763. case B43_PHYTYPE_A:
  2764. if (phy_rev >= 4)
  2765. unsupported = 1;
  2766. break;
  2767. case B43_PHYTYPE_B:
  2768. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  2769. && phy_rev != 7)
  2770. unsupported = 1;
  2771. break;
  2772. case B43_PHYTYPE_G:
  2773. if (phy_rev > 9)
  2774. unsupported = 1;
  2775. break;
  2776. #ifdef CONFIG_B43_NPHY
  2777. case B43_PHYTYPE_N:
  2778. if (phy_rev > 1)
  2779. unsupported = 1;
  2780. break;
  2781. #endif
  2782. default:
  2783. unsupported = 1;
  2784. };
  2785. if (unsupported) {
  2786. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  2787. "(Analog %u, Type %u, Revision %u)\n",
  2788. analog_type, phy_type, phy_rev);
  2789. return -EOPNOTSUPP;
  2790. }
  2791. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2792. analog_type, phy_type, phy_rev);
  2793. /* Get RADIO versioning */
  2794. if (dev->dev->bus->chip_id == 0x4317) {
  2795. if (dev->dev->bus->chip_rev == 0)
  2796. tmp = 0x3205017F;
  2797. else if (dev->dev->bus->chip_rev == 1)
  2798. tmp = 0x4205017F;
  2799. else
  2800. tmp = 0x5205017F;
  2801. } else {
  2802. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2803. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  2804. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2805. tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
  2806. }
  2807. radio_manuf = (tmp & 0x00000FFF);
  2808. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2809. radio_rev = (tmp & 0xF0000000) >> 28;
  2810. if (radio_manuf != 0x17F /* Broadcom */)
  2811. unsupported = 1;
  2812. switch (phy_type) {
  2813. case B43_PHYTYPE_A:
  2814. if (radio_ver != 0x2060)
  2815. unsupported = 1;
  2816. if (radio_rev != 1)
  2817. unsupported = 1;
  2818. if (radio_manuf != 0x17F)
  2819. unsupported = 1;
  2820. break;
  2821. case B43_PHYTYPE_B:
  2822. if ((radio_ver & 0xFFF0) != 0x2050)
  2823. unsupported = 1;
  2824. break;
  2825. case B43_PHYTYPE_G:
  2826. if (radio_ver != 0x2050)
  2827. unsupported = 1;
  2828. break;
  2829. case B43_PHYTYPE_N:
  2830. if (radio_ver != 0x2055)
  2831. unsupported = 1;
  2832. break;
  2833. default:
  2834. B43_WARN_ON(1);
  2835. }
  2836. if (unsupported) {
  2837. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  2838. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2839. radio_manuf, radio_ver, radio_rev);
  2840. return -EOPNOTSUPP;
  2841. }
  2842. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  2843. radio_manuf, radio_ver, radio_rev);
  2844. phy->radio_manuf = radio_manuf;
  2845. phy->radio_ver = radio_ver;
  2846. phy->radio_rev = radio_rev;
  2847. phy->analog = analog_type;
  2848. phy->type = phy_type;
  2849. phy->rev = phy_rev;
  2850. return 0;
  2851. }
  2852. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  2853. struct b43_phy *phy)
  2854. {
  2855. struct b43_txpower_lo_control *lo;
  2856. int i;
  2857. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2858. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2859. phy->aci_enable = 0;
  2860. phy->aci_wlan_automatic = 0;
  2861. phy->aci_hw_rssi = 0;
  2862. phy->radio_off_context.valid = 0;
  2863. lo = phy->lo_control;
  2864. if (lo) {
  2865. memset(lo, 0, sizeof(*(phy->lo_control)));
  2866. lo->rebuild = 1;
  2867. lo->tx_bias = 0xFF;
  2868. }
  2869. phy->max_lb_gain = 0;
  2870. phy->trsw_rx_gain = 0;
  2871. phy->txpwr_offset = 0;
  2872. /* NRSSI */
  2873. phy->nrssislope = 0;
  2874. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2875. phy->nrssi[i] = -1000;
  2876. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2877. phy->nrssi_lt[i] = i;
  2878. phy->lofcal = 0xFFFF;
  2879. phy->initval = 0xFFFF;
  2880. phy->interfmode = B43_INTERFMODE_NONE;
  2881. phy->channel = 0xFF;
  2882. phy->hardware_power_control = !!modparam_hwpctl;
  2883. /* PHY TX errors counter. */
  2884. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2885. /* OFDM-table address caching. */
  2886. phy->ofdmtab_addr_direction = B43_OFDMTAB_DIRECTION_UNKNOWN;
  2887. }
  2888. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  2889. {
  2890. dev->dfq_valid = 0;
  2891. /* Assume the radio is enabled. If it's not enabled, the state will
  2892. * immediately get fixed on the first periodic work run. */
  2893. dev->radio_hw_enable = 1;
  2894. /* Stats */
  2895. memset(&dev->stats, 0, sizeof(dev->stats));
  2896. setup_struct_phy_for_init(dev, &dev->phy);
  2897. /* IRQ related flags */
  2898. dev->irq_reason = 0;
  2899. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2900. dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
  2901. dev->mac_suspended = 1;
  2902. /* Noise calculation context */
  2903. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2904. }
  2905. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  2906. {
  2907. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2908. u32 hf;
  2909. if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
  2910. return;
  2911. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  2912. return;
  2913. hf = b43_hf_read(dev);
  2914. if (sprom->boardflags_lo & B43_BFL_BTCMOD)
  2915. hf |= B43_HF_BTCOEXALT;
  2916. else
  2917. hf |= B43_HF_BTCOEX;
  2918. b43_hf_write(dev, hf);
  2919. //TODO
  2920. }
  2921. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  2922. { //TODO
  2923. }
  2924. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  2925. {
  2926. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2927. struct ssb_bus *bus = dev->dev->bus;
  2928. u32 tmp;
  2929. if (bus->pcicore.dev &&
  2930. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2931. bus->pcicore.dev->id.revision <= 5) {
  2932. /* IMCFGLO timeouts workaround. */
  2933. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2934. tmp &= ~SSB_IMCFGLO_REQTO;
  2935. tmp &= ~SSB_IMCFGLO_SERTO;
  2936. switch (bus->bustype) {
  2937. case SSB_BUSTYPE_PCI:
  2938. case SSB_BUSTYPE_PCMCIA:
  2939. tmp |= 0x32;
  2940. break;
  2941. case SSB_BUSTYPE_SSB:
  2942. tmp |= 0x53;
  2943. break;
  2944. }
  2945. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2946. }
  2947. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2948. }
  2949. /* Write the short and long frame retry limit values. */
  2950. static void b43_set_retry_limits(struct b43_wldev *dev,
  2951. unsigned int short_retry,
  2952. unsigned int long_retry)
  2953. {
  2954. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2955. * the chip-internal counter. */
  2956. short_retry = min(short_retry, (unsigned int)0xF);
  2957. long_retry = min(long_retry, (unsigned int)0xF);
  2958. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
  2959. short_retry);
  2960. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
  2961. long_retry);
  2962. }
  2963. /* Shutdown a wireless core */
  2964. /* Locking: wl->mutex */
  2965. static void b43_wireless_core_exit(struct b43_wldev *dev)
  2966. {
  2967. struct b43_phy *phy = &dev->phy;
  2968. u32 macctl;
  2969. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  2970. if (b43_status(dev) != B43_STAT_INITIALIZED)
  2971. return;
  2972. b43_set_status(dev, B43_STAT_UNINIT);
  2973. /* Stop the microcode PSM. */
  2974. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  2975. macctl &= ~B43_MACCTL_PSM_RUN;
  2976. macctl |= B43_MACCTL_PSM_JMP0;
  2977. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2978. if (!dev->suspend_in_progress) {
  2979. b43_leds_exit(dev);
  2980. b43_rng_exit(dev->wl, false);
  2981. }
  2982. b43_dma_free(dev);
  2983. b43_chip_exit(dev);
  2984. b43_radio_turn_off(dev, 1);
  2985. b43_switch_analog(dev, 0);
  2986. if (phy->dyn_tssi_tbl)
  2987. kfree(phy->tssi2dbm);
  2988. kfree(phy->lo_control);
  2989. phy->lo_control = NULL;
  2990. if (dev->wl->current_beacon) {
  2991. dev_kfree_skb_any(dev->wl->current_beacon);
  2992. dev->wl->current_beacon = NULL;
  2993. }
  2994. ssb_device_disable(dev->dev, 0);
  2995. ssb_bus_may_powerdown(dev->dev->bus);
  2996. }
  2997. /* Initialize a wireless core */
  2998. static int b43_wireless_core_init(struct b43_wldev *dev)
  2999. {
  3000. struct b43_wl *wl = dev->wl;
  3001. struct ssb_bus *bus = dev->dev->bus;
  3002. struct ssb_sprom *sprom = &bus->sprom;
  3003. struct b43_phy *phy = &dev->phy;
  3004. int err;
  3005. u32 hf, tmp;
  3006. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3007. err = ssb_bus_powerup(bus, 0);
  3008. if (err)
  3009. goto out;
  3010. if (!ssb_device_is_enabled(dev->dev)) {
  3011. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  3012. b43_wireless_core_reset(dev, tmp);
  3013. }
  3014. if ((phy->type == B43_PHYTYPE_B) || (phy->type == B43_PHYTYPE_G)) {
  3015. phy->lo_control =
  3016. kzalloc(sizeof(*(phy->lo_control)), GFP_KERNEL);
  3017. if (!phy->lo_control) {
  3018. err = -ENOMEM;
  3019. goto err_busdown;
  3020. }
  3021. }
  3022. setup_struct_wldev_for_init(dev);
  3023. err = b43_phy_init_tssi2dbm_table(dev);
  3024. if (err)
  3025. goto err_kfree_lo_control;
  3026. /* Enable IRQ routing to this device. */
  3027. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  3028. b43_imcfglo_timeouts_workaround(dev);
  3029. b43_bluetooth_coext_disable(dev);
  3030. b43_phy_early_init(dev);
  3031. err = b43_chip_init(dev);
  3032. if (err)
  3033. goto err_kfree_tssitbl;
  3034. b43_shm_write16(dev, B43_SHM_SHARED,
  3035. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  3036. hf = b43_hf_read(dev);
  3037. if (phy->type == B43_PHYTYPE_G) {
  3038. hf |= B43_HF_SYMW;
  3039. if (phy->rev == 1)
  3040. hf |= B43_HF_GDCW;
  3041. if (sprom->boardflags_lo & B43_BFL_PACTRL)
  3042. hf |= B43_HF_OFDMPABOOST;
  3043. } else if (phy->type == B43_PHYTYPE_B) {
  3044. hf |= B43_HF_SYMW;
  3045. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  3046. hf &= ~B43_HF_GDCW;
  3047. }
  3048. b43_hf_write(dev, hf);
  3049. b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
  3050. B43_DEFAULT_LONG_RETRY_LIMIT);
  3051. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  3052. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  3053. /* Disable sending probe responses from firmware.
  3054. * Setting the MaxTime to one usec will always trigger
  3055. * a timeout, so we never send any probe resp.
  3056. * A timeout of zero is infinite. */
  3057. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  3058. b43_rate_memory_init(dev);
  3059. /* Minimum Contention Window */
  3060. if (phy->type == B43_PHYTYPE_B) {
  3061. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  3062. } else {
  3063. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  3064. }
  3065. /* Maximum Contention Window */
  3066. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  3067. err = b43_dma_init(dev);
  3068. if (err)
  3069. goto err_chip_exit;
  3070. b43_qos_init(dev);
  3071. //FIXME
  3072. #if 1
  3073. b43_write16(dev, 0x0612, 0x0050);
  3074. b43_shm_write16(dev, B43_SHM_SHARED, 0x0416, 0x0050);
  3075. b43_shm_write16(dev, B43_SHM_SHARED, 0x0414, 0x01F4);
  3076. #endif
  3077. b43_bluetooth_coext_enable(dev);
  3078. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  3079. b43_upload_card_macaddress(dev);
  3080. b43_security_init(dev);
  3081. if (!dev->suspend_in_progress)
  3082. b43_rng_init(wl);
  3083. b43_set_status(dev, B43_STAT_INITIALIZED);
  3084. if (!dev->suspend_in_progress)
  3085. b43_leds_init(dev);
  3086. out:
  3087. return err;
  3088. err_chip_exit:
  3089. b43_chip_exit(dev);
  3090. err_kfree_tssitbl:
  3091. if (phy->dyn_tssi_tbl)
  3092. kfree(phy->tssi2dbm);
  3093. err_kfree_lo_control:
  3094. kfree(phy->lo_control);
  3095. phy->lo_control = NULL;
  3096. err_busdown:
  3097. ssb_bus_may_powerdown(bus);
  3098. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3099. return err;
  3100. }
  3101. static int b43_op_add_interface(struct ieee80211_hw *hw,
  3102. struct ieee80211_if_init_conf *conf)
  3103. {
  3104. struct b43_wl *wl = hw_to_b43_wl(hw);
  3105. struct b43_wldev *dev;
  3106. unsigned long flags;
  3107. int err = -EOPNOTSUPP;
  3108. /* TODO: allow WDS/AP devices to coexist */
  3109. if (conf->type != IEEE80211_IF_TYPE_AP &&
  3110. conf->type != IEEE80211_IF_TYPE_STA &&
  3111. conf->type != IEEE80211_IF_TYPE_WDS &&
  3112. conf->type != IEEE80211_IF_TYPE_IBSS)
  3113. return -EOPNOTSUPP;
  3114. mutex_lock(&wl->mutex);
  3115. if (wl->operating)
  3116. goto out_mutex_unlock;
  3117. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  3118. dev = wl->current_dev;
  3119. wl->operating = 1;
  3120. wl->vif = conf->vif;
  3121. wl->if_type = conf->type;
  3122. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  3123. spin_lock_irqsave(&wl->irq_lock, flags);
  3124. b43_adjust_opmode(dev);
  3125. b43_upload_card_macaddress(dev);
  3126. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3127. err = 0;
  3128. out_mutex_unlock:
  3129. mutex_unlock(&wl->mutex);
  3130. return err;
  3131. }
  3132. static void b43_op_remove_interface(struct ieee80211_hw *hw,
  3133. struct ieee80211_if_init_conf *conf)
  3134. {
  3135. struct b43_wl *wl = hw_to_b43_wl(hw);
  3136. struct b43_wldev *dev = wl->current_dev;
  3137. unsigned long flags;
  3138. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3139. mutex_lock(&wl->mutex);
  3140. B43_WARN_ON(!wl->operating);
  3141. B43_WARN_ON(wl->vif != conf->vif);
  3142. wl->vif = NULL;
  3143. wl->operating = 0;
  3144. spin_lock_irqsave(&wl->irq_lock, flags);
  3145. b43_adjust_opmode(dev);
  3146. memset(wl->mac_addr, 0, ETH_ALEN);
  3147. b43_upload_card_macaddress(dev);
  3148. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3149. mutex_unlock(&wl->mutex);
  3150. }
  3151. static int b43_op_start(struct ieee80211_hw *hw)
  3152. {
  3153. struct b43_wl *wl = hw_to_b43_wl(hw);
  3154. struct b43_wldev *dev = wl->current_dev;
  3155. int did_init = 0;
  3156. int err = 0;
  3157. bool do_rfkill_exit = 0;
  3158. /* Kill all old instance specific information to make sure
  3159. * the card won't use it in the short timeframe between start
  3160. * and mac80211 reconfiguring it. */
  3161. memset(wl->bssid, 0, ETH_ALEN);
  3162. memset(wl->mac_addr, 0, ETH_ALEN);
  3163. wl->filter_flags = 0;
  3164. wl->radiotap_enabled = 0;
  3165. /* First register RFkill.
  3166. * LEDs that are registered later depend on it. */
  3167. b43_rfkill_init(dev);
  3168. mutex_lock(&wl->mutex);
  3169. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3170. err = b43_wireless_core_init(dev);
  3171. if (err) {
  3172. do_rfkill_exit = 1;
  3173. goto out_mutex_unlock;
  3174. }
  3175. did_init = 1;
  3176. }
  3177. if (b43_status(dev) < B43_STAT_STARTED) {
  3178. err = b43_wireless_core_start(dev);
  3179. if (err) {
  3180. if (did_init)
  3181. b43_wireless_core_exit(dev);
  3182. do_rfkill_exit = 1;
  3183. goto out_mutex_unlock;
  3184. }
  3185. }
  3186. out_mutex_unlock:
  3187. mutex_unlock(&wl->mutex);
  3188. if (do_rfkill_exit)
  3189. b43_rfkill_exit(dev);
  3190. return err;
  3191. }
  3192. static void b43_op_stop(struct ieee80211_hw *hw)
  3193. {
  3194. struct b43_wl *wl = hw_to_b43_wl(hw);
  3195. struct b43_wldev *dev = wl->current_dev;
  3196. b43_rfkill_exit(dev);
  3197. mutex_lock(&wl->mutex);
  3198. if (b43_status(dev) >= B43_STAT_STARTED)
  3199. b43_wireless_core_stop(dev);
  3200. b43_wireless_core_exit(dev);
  3201. mutex_unlock(&wl->mutex);
  3202. }
  3203. static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
  3204. u32 short_retry_limit, u32 long_retry_limit)
  3205. {
  3206. struct b43_wl *wl = hw_to_b43_wl(hw);
  3207. struct b43_wldev *dev;
  3208. int err = 0;
  3209. mutex_lock(&wl->mutex);
  3210. dev = wl->current_dev;
  3211. if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED))) {
  3212. err = -ENODEV;
  3213. goto out_unlock;
  3214. }
  3215. b43_set_retry_limits(dev, short_retry_limit, long_retry_limit);
  3216. out_unlock:
  3217. mutex_unlock(&wl->mutex);
  3218. return err;
  3219. }
  3220. static int b43_op_beacon_set_tim(struct ieee80211_hw *hw, int aid, int set)
  3221. {
  3222. struct b43_wl *wl = hw_to_b43_wl(hw);
  3223. struct sk_buff *beacon;
  3224. unsigned long flags;
  3225. /* We could modify the existing beacon and set the aid bit in
  3226. * the TIM field, but that would probably require resizing and
  3227. * moving of data within the beacon template.
  3228. * Simply request a new beacon and let mac80211 do the hard work. */
  3229. beacon = ieee80211_beacon_get(hw, wl->vif, NULL);
  3230. if (unlikely(!beacon))
  3231. return -ENOMEM;
  3232. spin_lock_irqsave(&wl->irq_lock, flags);
  3233. b43_update_templates(wl, beacon);
  3234. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3235. return 0;
  3236. }
  3237. static int b43_op_ibss_beacon_update(struct ieee80211_hw *hw,
  3238. struct sk_buff *beacon,
  3239. struct ieee80211_tx_control *ctl)
  3240. {
  3241. struct b43_wl *wl = hw_to_b43_wl(hw);
  3242. unsigned long flags;
  3243. spin_lock_irqsave(&wl->irq_lock, flags);
  3244. b43_update_templates(wl, beacon);
  3245. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3246. return 0;
  3247. }
  3248. static const struct ieee80211_ops b43_hw_ops = {
  3249. .tx = b43_op_tx,
  3250. .conf_tx = b43_op_conf_tx,
  3251. .add_interface = b43_op_add_interface,
  3252. .remove_interface = b43_op_remove_interface,
  3253. .config = b43_op_config,
  3254. .config_interface = b43_op_config_interface,
  3255. .configure_filter = b43_op_configure_filter,
  3256. .set_key = b43_op_set_key,
  3257. .get_stats = b43_op_get_stats,
  3258. .get_tx_stats = b43_op_get_tx_stats,
  3259. .start = b43_op_start,
  3260. .stop = b43_op_stop,
  3261. .set_retry_limit = b43_op_set_retry_limit,
  3262. .set_tim = b43_op_beacon_set_tim,
  3263. .beacon_update = b43_op_ibss_beacon_update,
  3264. };
  3265. /* Hard-reset the chip. Do not call this directly.
  3266. * Use b43_controller_restart()
  3267. */
  3268. static void b43_chip_reset(struct work_struct *work)
  3269. {
  3270. struct b43_wldev *dev =
  3271. container_of(work, struct b43_wldev, restart_work);
  3272. struct b43_wl *wl = dev->wl;
  3273. int err = 0;
  3274. int prev_status;
  3275. mutex_lock(&wl->mutex);
  3276. prev_status = b43_status(dev);
  3277. /* Bring the device down... */
  3278. if (prev_status >= B43_STAT_STARTED)
  3279. b43_wireless_core_stop(dev);
  3280. if (prev_status >= B43_STAT_INITIALIZED)
  3281. b43_wireless_core_exit(dev);
  3282. /* ...and up again. */
  3283. if (prev_status >= B43_STAT_INITIALIZED) {
  3284. err = b43_wireless_core_init(dev);
  3285. if (err)
  3286. goto out;
  3287. }
  3288. if (prev_status >= B43_STAT_STARTED) {
  3289. err = b43_wireless_core_start(dev);
  3290. if (err) {
  3291. b43_wireless_core_exit(dev);
  3292. goto out;
  3293. }
  3294. }
  3295. out:
  3296. mutex_unlock(&wl->mutex);
  3297. if (err)
  3298. b43err(wl, "Controller restart FAILED\n");
  3299. else
  3300. b43info(wl, "Controller restarted\n");
  3301. }
  3302. static int b43_setup_modes(struct b43_wldev *dev,
  3303. bool have_2ghz_phy, bool have_5ghz_phy)
  3304. {
  3305. struct ieee80211_hw *hw = dev->wl->hw;
  3306. struct ieee80211_hw_mode *mode;
  3307. struct b43_phy *phy = &dev->phy;
  3308. int err;
  3309. /* XXX: This function will go away soon, when mac80211
  3310. * band stuff is rewritten. So this is just a hack.
  3311. * For now we always claim GPHY mode, as there is no
  3312. * support for NPHY and APHY in the device, yet.
  3313. * This assumption is OK, as any B, N or A PHY will already
  3314. * have died a horrible sanity check death earlier. */
  3315. mode = &phy->hwmodes[0];
  3316. mode->mode = MODE_IEEE80211G;
  3317. mode->num_channels = b43_2ghz_chantable_size;
  3318. mode->channels = b43_2ghz_chantable;
  3319. mode->num_rates = b43_g_ratetable_size;
  3320. mode->rates = b43_g_ratetable;
  3321. err = ieee80211_register_hwmode(hw, mode);
  3322. if (err)
  3323. return err;
  3324. phy->possible_phymodes |= B43_PHYMODE_G;
  3325. return 0;
  3326. }
  3327. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3328. {
  3329. /* We release firmware that late to not be required to re-request
  3330. * is all the time when we reinit the core. */
  3331. b43_release_firmware(dev);
  3332. }
  3333. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3334. {
  3335. struct b43_wl *wl = dev->wl;
  3336. struct ssb_bus *bus = dev->dev->bus;
  3337. struct pci_dev *pdev = bus->host_pci;
  3338. int err;
  3339. bool have_2ghz_phy = 0, have_5ghz_phy = 0;
  3340. u32 tmp;
  3341. /* Do NOT do any device initialization here.
  3342. * Do it in wireless_core_init() instead.
  3343. * This function is for gathering basic information about the HW, only.
  3344. * Also some structs may be set up here. But most likely you want to have
  3345. * that in core_init(), too.
  3346. */
  3347. err = ssb_bus_powerup(bus, 0);
  3348. if (err) {
  3349. b43err(wl, "Bus powerup failed\n");
  3350. goto out;
  3351. }
  3352. /* Get the PHY type. */
  3353. if (dev->dev->id.revision >= 5) {
  3354. u32 tmshigh;
  3355. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3356. have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
  3357. have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
  3358. } else
  3359. B43_WARN_ON(1);
  3360. dev->phy.gmode = have_2ghz_phy;
  3361. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3362. b43_wireless_core_reset(dev, tmp);
  3363. err = b43_phy_versioning(dev);
  3364. if (err)
  3365. goto err_powerdown;
  3366. /* Check if this device supports multiband. */
  3367. if (!pdev ||
  3368. (pdev->device != 0x4312 &&
  3369. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3370. /* No multiband support. */
  3371. have_2ghz_phy = 0;
  3372. have_5ghz_phy = 0;
  3373. switch (dev->phy.type) {
  3374. case B43_PHYTYPE_A:
  3375. have_5ghz_phy = 1;
  3376. break;
  3377. case B43_PHYTYPE_G:
  3378. case B43_PHYTYPE_N:
  3379. have_2ghz_phy = 1;
  3380. break;
  3381. default:
  3382. B43_WARN_ON(1);
  3383. }
  3384. }
  3385. if (dev->phy.type == B43_PHYTYPE_A) {
  3386. /* FIXME */
  3387. b43err(wl, "IEEE 802.11a devices are unsupported\n");
  3388. err = -EOPNOTSUPP;
  3389. goto err_powerdown;
  3390. }
  3391. dev->phy.gmode = have_2ghz_phy;
  3392. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3393. b43_wireless_core_reset(dev, tmp);
  3394. err = b43_validate_chipaccess(dev);
  3395. if (err)
  3396. goto err_powerdown;
  3397. err = b43_setup_modes(dev, have_2ghz_phy, have_5ghz_phy);
  3398. if (err)
  3399. goto err_powerdown;
  3400. /* Now set some default "current_dev" */
  3401. if (!wl->current_dev)
  3402. wl->current_dev = dev;
  3403. INIT_WORK(&dev->restart_work, b43_chip_reset);
  3404. b43_radio_turn_off(dev, 1);
  3405. b43_switch_analog(dev, 0);
  3406. ssb_device_disable(dev->dev, 0);
  3407. ssb_bus_may_powerdown(bus);
  3408. out:
  3409. return err;
  3410. err_powerdown:
  3411. ssb_bus_may_powerdown(bus);
  3412. return err;
  3413. }
  3414. static void b43_one_core_detach(struct ssb_device *dev)
  3415. {
  3416. struct b43_wldev *wldev;
  3417. struct b43_wl *wl;
  3418. wldev = ssb_get_drvdata(dev);
  3419. wl = wldev->wl;
  3420. cancel_work_sync(&wldev->restart_work);
  3421. b43_debugfs_remove_device(wldev);
  3422. b43_wireless_core_detach(wldev);
  3423. list_del(&wldev->list);
  3424. wl->nr_devs--;
  3425. ssb_set_drvdata(dev, NULL);
  3426. kfree(wldev);
  3427. }
  3428. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  3429. {
  3430. struct b43_wldev *wldev;
  3431. struct pci_dev *pdev;
  3432. int err = -ENOMEM;
  3433. if (!list_empty(&wl->devlist)) {
  3434. /* We are not the first core on this chip. */
  3435. pdev = dev->bus->host_pci;
  3436. /* Only special chips support more than one wireless
  3437. * core, although some of the other chips have more than
  3438. * one wireless core as well. Check for this and
  3439. * bail out early.
  3440. */
  3441. if (!pdev ||
  3442. ((pdev->device != 0x4321) &&
  3443. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  3444. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  3445. return -ENODEV;
  3446. }
  3447. }
  3448. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3449. if (!wldev)
  3450. goto out;
  3451. wldev->dev = dev;
  3452. wldev->wl = wl;
  3453. b43_set_status(wldev, B43_STAT_UNINIT);
  3454. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3455. tasklet_init(&wldev->isr_tasklet,
  3456. (void (*)(unsigned long))b43_interrupt_tasklet,
  3457. (unsigned long)wldev);
  3458. INIT_LIST_HEAD(&wldev->list);
  3459. err = b43_wireless_core_attach(wldev);
  3460. if (err)
  3461. goto err_kfree_wldev;
  3462. list_add(&wldev->list, &wl->devlist);
  3463. wl->nr_devs++;
  3464. ssb_set_drvdata(dev, wldev);
  3465. b43_debugfs_add_device(wldev);
  3466. out:
  3467. return err;
  3468. err_kfree_wldev:
  3469. kfree(wldev);
  3470. return err;
  3471. }
  3472. static void b43_sprom_fixup(struct ssb_bus *bus)
  3473. {
  3474. /* boardflags workarounds */
  3475. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  3476. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  3477. bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
  3478. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3479. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  3480. bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
  3481. }
  3482. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  3483. {
  3484. struct ieee80211_hw *hw = wl->hw;
  3485. ssb_set_devtypedata(dev, NULL);
  3486. ieee80211_free_hw(hw);
  3487. }
  3488. static int b43_wireless_init(struct ssb_device *dev)
  3489. {
  3490. struct ssb_sprom *sprom = &dev->bus->sprom;
  3491. struct ieee80211_hw *hw;
  3492. struct b43_wl *wl;
  3493. int err = -ENOMEM;
  3494. b43_sprom_fixup(dev->bus);
  3495. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  3496. if (!hw) {
  3497. b43err(NULL, "Could not allocate ieee80211 device\n");
  3498. goto out;
  3499. }
  3500. /* fill hw info */
  3501. hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  3502. IEEE80211_HW_RX_INCLUDES_FCS;
  3503. hw->max_signal = 100;
  3504. hw->max_rssi = -110;
  3505. hw->max_noise = -110;
  3506. hw->queues = 1; /* FIXME: hardware has more queues */
  3507. SET_IEEE80211_DEV(hw, dev->dev);
  3508. if (is_valid_ether_addr(sprom->et1mac))
  3509. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  3510. else
  3511. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  3512. /* Get and initialize struct b43_wl */
  3513. wl = hw_to_b43_wl(hw);
  3514. memset(wl, 0, sizeof(*wl));
  3515. wl->hw = hw;
  3516. spin_lock_init(&wl->irq_lock);
  3517. spin_lock_init(&wl->leds_lock);
  3518. spin_lock_init(&wl->shm_lock);
  3519. mutex_init(&wl->mutex);
  3520. INIT_LIST_HEAD(&wl->devlist);
  3521. ssb_set_devtypedata(dev, wl);
  3522. b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3523. err = 0;
  3524. out:
  3525. return err;
  3526. }
  3527. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  3528. {
  3529. struct b43_wl *wl;
  3530. int err;
  3531. int first = 0;
  3532. wl = ssb_get_devtypedata(dev);
  3533. if (!wl) {
  3534. /* Probing the first core. Must setup common struct b43_wl */
  3535. first = 1;
  3536. err = b43_wireless_init(dev);
  3537. if (err)
  3538. goto out;
  3539. wl = ssb_get_devtypedata(dev);
  3540. B43_WARN_ON(!wl);
  3541. }
  3542. err = b43_one_core_attach(dev, wl);
  3543. if (err)
  3544. goto err_wireless_exit;
  3545. if (first) {
  3546. err = ieee80211_register_hw(wl->hw);
  3547. if (err)
  3548. goto err_one_core_detach;
  3549. }
  3550. out:
  3551. return err;
  3552. err_one_core_detach:
  3553. b43_one_core_detach(dev);
  3554. err_wireless_exit:
  3555. if (first)
  3556. b43_wireless_exit(dev, wl);
  3557. return err;
  3558. }
  3559. static void b43_remove(struct ssb_device *dev)
  3560. {
  3561. struct b43_wl *wl = ssb_get_devtypedata(dev);
  3562. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3563. B43_WARN_ON(!wl);
  3564. if (wl->current_dev == wldev)
  3565. ieee80211_unregister_hw(wl->hw);
  3566. b43_one_core_detach(dev);
  3567. if (list_empty(&wl->devlist)) {
  3568. /* Last core on the chip unregistered.
  3569. * We can destroy common struct b43_wl.
  3570. */
  3571. b43_wireless_exit(dev, wl);
  3572. }
  3573. }
  3574. /* Perform a hardware reset. This can be called from any context. */
  3575. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  3576. {
  3577. /* Must avoid requeueing, if we are in shutdown. */
  3578. if (b43_status(dev) < B43_STAT_INITIALIZED)
  3579. return;
  3580. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  3581. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  3582. }
  3583. #ifdef CONFIG_PM
  3584. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  3585. {
  3586. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3587. struct b43_wl *wl = wldev->wl;
  3588. b43dbg(wl, "Suspending...\n");
  3589. mutex_lock(&wl->mutex);
  3590. wldev->suspend_in_progress = true;
  3591. wldev->suspend_init_status = b43_status(wldev);
  3592. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  3593. b43_wireless_core_stop(wldev);
  3594. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  3595. b43_wireless_core_exit(wldev);
  3596. mutex_unlock(&wl->mutex);
  3597. b43dbg(wl, "Device suspended.\n");
  3598. return 0;
  3599. }
  3600. static int b43_resume(struct ssb_device *dev)
  3601. {
  3602. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3603. struct b43_wl *wl = wldev->wl;
  3604. int err = 0;
  3605. b43dbg(wl, "Resuming...\n");
  3606. mutex_lock(&wl->mutex);
  3607. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  3608. err = b43_wireless_core_init(wldev);
  3609. if (err) {
  3610. b43err(wl, "Resume failed at core init\n");
  3611. goto out;
  3612. }
  3613. }
  3614. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  3615. err = b43_wireless_core_start(wldev);
  3616. if (err) {
  3617. b43_leds_exit(wldev);
  3618. b43_rng_exit(wldev->wl, true);
  3619. b43_wireless_core_exit(wldev);
  3620. b43err(wl, "Resume failed at core start\n");
  3621. goto out;
  3622. }
  3623. }
  3624. b43dbg(wl, "Device resumed.\n");
  3625. out:
  3626. wldev->suspend_in_progress = false;
  3627. mutex_unlock(&wl->mutex);
  3628. return err;
  3629. }
  3630. #else /* CONFIG_PM */
  3631. # define b43_suspend NULL
  3632. # define b43_resume NULL
  3633. #endif /* CONFIG_PM */
  3634. static struct ssb_driver b43_ssb_driver = {
  3635. .name = KBUILD_MODNAME,
  3636. .id_table = b43_ssb_tbl,
  3637. .probe = b43_probe,
  3638. .remove = b43_remove,
  3639. .suspend = b43_suspend,
  3640. .resume = b43_resume,
  3641. };
  3642. static void b43_print_driverinfo(void)
  3643. {
  3644. const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
  3645. *feat_leds = "", *feat_rfkill = "";
  3646. #ifdef CONFIG_B43_PCI_AUTOSELECT
  3647. feat_pci = "P";
  3648. #endif
  3649. #ifdef CONFIG_B43_PCMCIA
  3650. feat_pcmcia = "M";
  3651. #endif
  3652. #ifdef CONFIG_B43_NPHY
  3653. feat_nphy = "N";
  3654. #endif
  3655. #ifdef CONFIG_B43_LEDS
  3656. feat_leds = "L";
  3657. #endif
  3658. #ifdef CONFIG_B43_RFKILL
  3659. feat_rfkill = "R";
  3660. #endif
  3661. printk(KERN_INFO "Broadcom 43xx driver loaded "
  3662. "[ Features: %s%s%s%s%s, Firmware-ID: "
  3663. B43_SUPPORTED_FIRMWARE_ID " ]\n",
  3664. feat_pci, feat_pcmcia, feat_nphy,
  3665. feat_leds, feat_rfkill);
  3666. }
  3667. static int __init b43_init(void)
  3668. {
  3669. int err;
  3670. b43_debugfs_init();
  3671. err = b43_pcmcia_init();
  3672. if (err)
  3673. goto err_dfs_exit;
  3674. err = ssb_driver_register(&b43_ssb_driver);
  3675. if (err)
  3676. goto err_pcmcia_exit;
  3677. b43_print_driverinfo();
  3678. return err;
  3679. err_pcmcia_exit:
  3680. b43_pcmcia_exit();
  3681. err_dfs_exit:
  3682. b43_debugfs_exit();
  3683. return err;
  3684. }
  3685. static void __exit b43_exit(void)
  3686. {
  3687. ssb_driver_unregister(&b43_ssb_driver);
  3688. b43_pcmcia_exit();
  3689. b43_debugfs_exit();
  3690. }
  3691. module_init(b43_init)
  3692. module_exit(b43_exit)