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