main.c 104 KB

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