main.c 102 KB

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