main.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097
  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. if (dev->mac_suspended == 0) {
  1725. b43_write32(dev, B43_MMIO_MACCTL,
  1726. b43_read32(dev, B43_MMIO_MACCTL)
  1727. | B43_MACCTL_ENABLED);
  1728. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  1729. B43_IRQ_MAC_SUSPENDED);
  1730. /* Commit writes */
  1731. b43_read32(dev, B43_MMIO_MACCTL);
  1732. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1733. b43_power_saving_ctl_bits(dev, 0);
  1734. }
  1735. }
  1736. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1737. void b43_mac_suspend(struct b43_wldev *dev)
  1738. {
  1739. int i;
  1740. u32 tmp;
  1741. B43_WARN_ON(dev->mac_suspended < 0);
  1742. if (dev->mac_suspended == 0) {
  1743. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  1744. b43_write32(dev, B43_MMIO_MACCTL,
  1745. b43_read32(dev, B43_MMIO_MACCTL)
  1746. & ~B43_MACCTL_ENABLED);
  1747. /* force pci to flush the write */
  1748. b43_read32(dev, B43_MMIO_MACCTL);
  1749. for (i = 10000; i; i--) {
  1750. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1751. if (tmp & B43_IRQ_MAC_SUSPENDED)
  1752. goto out;
  1753. udelay(1);
  1754. }
  1755. b43err(dev->wl, "MAC suspend failed\n");
  1756. }
  1757. out:
  1758. dev->mac_suspended++;
  1759. }
  1760. static void b43_adjust_opmode(struct b43_wldev *dev)
  1761. {
  1762. struct b43_wl *wl = dev->wl;
  1763. u32 ctl;
  1764. u16 cfp_pretbtt;
  1765. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  1766. /* Reset status to STA infrastructure mode. */
  1767. ctl &= ~B43_MACCTL_AP;
  1768. ctl &= ~B43_MACCTL_KEEP_CTL;
  1769. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  1770. ctl &= ~B43_MACCTL_KEEP_BAD;
  1771. ctl &= ~B43_MACCTL_PROMISC;
  1772. ctl &= ~B43_MACCTL_BEACPROMISC;
  1773. ctl |= B43_MACCTL_INFRA;
  1774. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  1775. ctl |= B43_MACCTL_AP;
  1776. else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
  1777. ctl &= ~B43_MACCTL_INFRA;
  1778. if (wl->filter_flags & FIF_CONTROL)
  1779. ctl |= B43_MACCTL_KEEP_CTL;
  1780. if (wl->filter_flags & FIF_FCSFAIL)
  1781. ctl |= B43_MACCTL_KEEP_BAD;
  1782. if (wl->filter_flags & FIF_PLCPFAIL)
  1783. ctl |= B43_MACCTL_KEEP_BADPLCP;
  1784. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1785. ctl |= B43_MACCTL_PROMISC;
  1786. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1787. ctl |= B43_MACCTL_BEACPROMISC;
  1788. /* Workaround: On old hardware the HW-MAC-address-filter
  1789. * doesn't work properly, so always run promisc in filter
  1790. * it in software. */
  1791. if (dev->dev->id.revision <= 4)
  1792. ctl |= B43_MACCTL_PROMISC;
  1793. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  1794. cfp_pretbtt = 2;
  1795. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  1796. if (dev->dev->bus->chip_id == 0x4306 &&
  1797. dev->dev->bus->chip_rev == 3)
  1798. cfp_pretbtt = 100;
  1799. else
  1800. cfp_pretbtt = 50;
  1801. }
  1802. b43_write16(dev, 0x612, cfp_pretbtt);
  1803. }
  1804. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  1805. {
  1806. u16 offset;
  1807. if (is_ofdm) {
  1808. offset = 0x480;
  1809. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1810. } else {
  1811. offset = 0x4C0;
  1812. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1813. }
  1814. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  1815. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  1816. }
  1817. static void b43_rate_memory_init(struct b43_wldev *dev)
  1818. {
  1819. switch (dev->phy.type) {
  1820. case B43_PHYTYPE_A:
  1821. case B43_PHYTYPE_G:
  1822. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  1823. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  1824. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  1825. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  1826. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  1827. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  1828. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  1829. if (dev->phy.type == B43_PHYTYPE_A)
  1830. break;
  1831. /* fallthrough */
  1832. case B43_PHYTYPE_B:
  1833. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  1834. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  1835. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  1836. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  1837. break;
  1838. default:
  1839. B43_WARN_ON(1);
  1840. }
  1841. }
  1842. /* Set the TX-Antenna for management frames sent by firmware. */
  1843. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  1844. {
  1845. u16 ant = 0;
  1846. u16 tmp;
  1847. switch (antenna) {
  1848. case B43_ANTENNA0:
  1849. ant |= B43_TX4_PHY_ANT0;
  1850. break;
  1851. case B43_ANTENNA1:
  1852. ant |= B43_TX4_PHY_ANT1;
  1853. break;
  1854. case B43_ANTENNA_AUTO:
  1855. ant |= B43_TX4_PHY_ANTLAST;
  1856. break;
  1857. default:
  1858. B43_WARN_ON(1);
  1859. }
  1860. /* FIXME We also need to set the other flags of the PHY control field somewhere. */
  1861. /* For Beacons */
  1862. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1863. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1864. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, tmp);
  1865. /* For ACK/CTS */
  1866. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  1867. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1868. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  1869. /* For Probe Resposes */
  1870. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  1871. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1872. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  1873. }
  1874. /* This is the opposite of b43_chip_init() */
  1875. static void b43_chip_exit(struct b43_wldev *dev)
  1876. {
  1877. b43_radio_turn_off(dev, 1);
  1878. b43_leds_exit(dev);
  1879. b43_gpio_cleanup(dev);
  1880. /* firmware is released later */
  1881. }
  1882. /* Initialize the chip
  1883. * http://bcm-specs.sipsolutions.net/ChipInit
  1884. */
  1885. static int b43_chip_init(struct b43_wldev *dev)
  1886. {
  1887. struct b43_phy *phy = &dev->phy;
  1888. int err, tmp;
  1889. u32 value32;
  1890. u16 value16;
  1891. b43_write32(dev, B43_MMIO_MACCTL,
  1892. B43_MACCTL_PSM_JMP0 | B43_MACCTL_IHR_ENABLED);
  1893. err = b43_request_firmware(dev);
  1894. if (err)
  1895. goto out;
  1896. err = b43_upload_microcode(dev);
  1897. if (err)
  1898. goto out; /* firmware is released later */
  1899. err = b43_gpio_init(dev);
  1900. if (err)
  1901. goto out; /* firmware is released later */
  1902. b43_leds_init(dev);
  1903. err = b43_upload_initvals(dev);
  1904. if (err)
  1905. goto err_leds_exit;
  1906. b43_radio_turn_on(dev);
  1907. b43_write16(dev, 0x03E6, 0x0000);
  1908. err = b43_phy_init(dev);
  1909. if (err)
  1910. goto err_radio_off;
  1911. /* Select initial Interference Mitigation. */
  1912. tmp = phy->interfmode;
  1913. phy->interfmode = B43_INTERFMODE_NONE;
  1914. b43_radio_set_interference_mitigation(dev, tmp);
  1915. b43_set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  1916. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  1917. if (phy->type == B43_PHYTYPE_B) {
  1918. value16 = b43_read16(dev, 0x005E);
  1919. value16 |= 0x0004;
  1920. b43_write16(dev, 0x005E, value16);
  1921. }
  1922. b43_write32(dev, 0x0100, 0x01000000);
  1923. if (dev->dev->id.revision < 5)
  1924. b43_write32(dev, 0x010C, 0x01000000);
  1925. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1926. & ~B43_MACCTL_INFRA);
  1927. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1928. | B43_MACCTL_INFRA);
  1929. if (b43_using_pio(dev)) {
  1930. b43_write32(dev, 0x0210, 0x00000100);
  1931. b43_write32(dev, 0x0230, 0x00000100);
  1932. b43_write32(dev, 0x0250, 0x00000100);
  1933. b43_write32(dev, 0x0270, 0x00000100);
  1934. b43_shm_write16(dev, B43_SHM_SHARED, 0x0034, 0x0000);
  1935. }
  1936. /* Probe Response Timeout value */
  1937. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  1938. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  1939. /* Initially set the wireless operation mode. */
  1940. b43_adjust_opmode(dev);
  1941. if (dev->dev->id.revision < 3) {
  1942. b43_write16(dev, 0x060E, 0x0000);
  1943. b43_write16(dev, 0x0610, 0x8000);
  1944. b43_write16(dev, 0x0604, 0x0000);
  1945. b43_write16(dev, 0x0606, 0x0200);
  1946. } else {
  1947. b43_write32(dev, 0x0188, 0x80000000);
  1948. b43_write32(dev, 0x018C, 0x02000000);
  1949. }
  1950. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  1951. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  1952. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  1953. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  1954. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  1955. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  1956. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  1957. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  1958. value32 |= 0x00100000;
  1959. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  1960. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  1961. dev->dev->bus->chipco.fast_pwrup_delay);
  1962. err = 0;
  1963. b43dbg(dev->wl, "Chip initialized\n");
  1964. out:
  1965. return err;
  1966. err_radio_off:
  1967. b43_radio_turn_off(dev, 1);
  1968. err_leds_exit:
  1969. b43_leds_exit(dev);
  1970. b43_gpio_cleanup(dev);
  1971. return err;
  1972. }
  1973. static void b43_periodic_every120sec(struct b43_wldev *dev)
  1974. {
  1975. struct b43_phy *phy = &dev->phy;
  1976. if (phy->type != B43_PHYTYPE_G || phy->rev < 2)
  1977. return;
  1978. b43_mac_suspend(dev);
  1979. b43_lo_g_measure(dev);
  1980. b43_mac_enable(dev);
  1981. if (b43_has_hardware_pctl(phy))
  1982. b43_lo_g_ctl_mark_all_unused(dev);
  1983. }
  1984. static void b43_periodic_every60sec(struct b43_wldev *dev)
  1985. {
  1986. struct b43_phy *phy = &dev->phy;
  1987. if (!b43_has_hardware_pctl(phy))
  1988. b43_lo_g_ctl_mark_all_unused(dev);
  1989. if (dev->dev->bus->sprom.r1.boardflags_lo & B43_BFL_RSSI) {
  1990. b43_mac_suspend(dev);
  1991. b43_calc_nrssi_slope(dev);
  1992. if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 8)) {
  1993. u8 old_chan = phy->channel;
  1994. /* VCO Calibration */
  1995. if (old_chan >= 8)
  1996. b43_radio_selectchannel(dev, 1, 0);
  1997. else
  1998. b43_radio_selectchannel(dev, 13, 0);
  1999. b43_radio_selectchannel(dev, old_chan, 0);
  2000. }
  2001. b43_mac_enable(dev);
  2002. }
  2003. }
  2004. static void b43_periodic_every30sec(struct b43_wldev *dev)
  2005. {
  2006. /* Update device statistics. */
  2007. b43_calculate_link_quality(dev);
  2008. }
  2009. static void b43_periodic_every15sec(struct b43_wldev *dev)
  2010. {
  2011. struct b43_phy *phy = &dev->phy;
  2012. if (phy->type == B43_PHYTYPE_G) {
  2013. //TODO: update_aci_moving_average
  2014. if (phy->aci_enable && phy->aci_wlan_automatic) {
  2015. b43_mac_suspend(dev);
  2016. if (!phy->aci_enable && 1 /*TODO: not scanning? */ ) {
  2017. if (0 /*TODO: bunch of conditions */ ) {
  2018. b43_radio_set_interference_mitigation
  2019. (dev, B43_INTERFMODE_MANUALWLAN);
  2020. }
  2021. } else if (1 /*TODO*/) {
  2022. /*
  2023. if ((aci_average > 1000) && !(b43_radio_aci_scan(dev))) {
  2024. b43_radio_set_interference_mitigation(dev,
  2025. B43_INTERFMODE_NONE);
  2026. }
  2027. */
  2028. }
  2029. b43_mac_enable(dev);
  2030. } else if (phy->interfmode == B43_INTERFMODE_NONWLAN &&
  2031. phy->rev == 1) {
  2032. //TODO: implement rev1 workaround
  2033. }
  2034. }
  2035. b43_phy_xmitpower(dev); //FIXME: unless scanning?
  2036. //TODO for APHY (temperature?)
  2037. }
  2038. static void do_periodic_work(struct b43_wldev *dev)
  2039. {
  2040. unsigned int state;
  2041. state = dev->periodic_state;
  2042. if (state % 8 == 0)
  2043. b43_periodic_every120sec(dev);
  2044. if (state % 4 == 0)
  2045. b43_periodic_every60sec(dev);
  2046. if (state % 2 == 0)
  2047. b43_periodic_every30sec(dev);
  2048. b43_periodic_every15sec(dev);
  2049. }
  2050. /* Estimate a "Badness" value based on the periodic work
  2051. * state-machine state. "Badness" is worse (bigger), if the
  2052. * periodic work will take longer.
  2053. */
  2054. static int estimate_periodic_work_badness(unsigned int state)
  2055. {
  2056. int badness = 0;
  2057. if (state % 8 == 0) /* every 120 sec */
  2058. badness += 10;
  2059. if (state % 4 == 0) /* every 60 sec */
  2060. badness += 5;
  2061. if (state % 2 == 0) /* every 30 sec */
  2062. badness += 1;
  2063. #define BADNESS_LIMIT 4
  2064. return badness;
  2065. }
  2066. static void b43_periodic_work_handler(struct work_struct *work)
  2067. {
  2068. struct b43_wldev *dev =
  2069. container_of(work, struct b43_wldev, periodic_work.work);
  2070. unsigned long flags, delay;
  2071. u32 savedirqs = 0;
  2072. int badness;
  2073. mutex_lock(&dev->wl->mutex);
  2074. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2075. goto out;
  2076. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2077. goto out_requeue;
  2078. badness = estimate_periodic_work_badness(dev->periodic_state);
  2079. if (badness > BADNESS_LIMIT) {
  2080. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  2081. /* Suspend TX as we don't want to transmit packets while
  2082. * we recalibrate the hardware. */
  2083. b43_tx_suspend(dev);
  2084. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2085. /* Periodic work will take a long time, so we want it to
  2086. * be preemtible and release the spinlock. */
  2087. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  2088. b43_synchronize_irq(dev);
  2089. do_periodic_work(dev);
  2090. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  2091. b43_interrupt_enable(dev, savedirqs);
  2092. b43_tx_resume(dev);
  2093. mmiowb();
  2094. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  2095. } else {
  2096. /* Take the global driver lock. This will lock any operation. */
  2097. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  2098. do_periodic_work(dev);
  2099. mmiowb();
  2100. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  2101. }
  2102. dev->periodic_state++;
  2103. out_requeue:
  2104. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2105. delay = msecs_to_jiffies(50);
  2106. else
  2107. delay = round_jiffies(HZ * 15);
  2108. queue_delayed_work(dev->wl->hw->workqueue, &dev->periodic_work, delay);
  2109. out:
  2110. mutex_unlock(&dev->wl->mutex);
  2111. }
  2112. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2113. {
  2114. struct delayed_work *work = &dev->periodic_work;
  2115. dev->periodic_state = 0;
  2116. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2117. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2118. }
  2119. /* Validate access to the chip (SHM) */
  2120. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2121. {
  2122. u32 value;
  2123. u32 shm_backup;
  2124. shm_backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2125. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2126. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2127. goto error;
  2128. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2129. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2130. goto error;
  2131. b43_shm_write32(dev, B43_SHM_SHARED, 0, shm_backup);
  2132. value = b43_read32(dev, B43_MMIO_MACCTL);
  2133. if ((value | B43_MACCTL_GMODE) !=
  2134. (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2135. goto error;
  2136. value = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2137. if (value)
  2138. goto error;
  2139. return 0;
  2140. error:
  2141. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2142. return -ENODEV;
  2143. }
  2144. static void b43_security_init(struct b43_wldev *dev)
  2145. {
  2146. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2147. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2148. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2149. /* KTP is a word address, but we address SHM bytewise.
  2150. * So multiply by two.
  2151. */
  2152. dev->ktp *= 2;
  2153. if (dev->dev->id.revision >= 5) {
  2154. /* Number of RCMTA address slots */
  2155. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2156. }
  2157. b43_clear_keys(dev);
  2158. }
  2159. static int b43_rng_read(struct hwrng *rng, u32 * data)
  2160. {
  2161. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2162. unsigned long flags;
  2163. /* Don't take wl->mutex here, as it could deadlock with
  2164. * hwrng internal locking. It's not needed to take
  2165. * wl->mutex here, anyway. */
  2166. spin_lock_irqsave(&wl->irq_lock, flags);
  2167. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2168. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2169. return (sizeof(u16));
  2170. }
  2171. static void b43_rng_exit(struct b43_wl *wl)
  2172. {
  2173. if (wl->rng_initialized)
  2174. hwrng_unregister(&wl->rng);
  2175. }
  2176. static int b43_rng_init(struct b43_wl *wl)
  2177. {
  2178. int err;
  2179. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2180. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2181. wl->rng.name = wl->rng_name;
  2182. wl->rng.data_read = b43_rng_read;
  2183. wl->rng.priv = (unsigned long)wl;
  2184. wl->rng_initialized = 1;
  2185. err = hwrng_register(&wl->rng);
  2186. if (err) {
  2187. wl->rng_initialized = 0;
  2188. b43err(wl, "Failed to register the random "
  2189. "number generator (%d)\n", err);
  2190. }
  2191. return err;
  2192. }
  2193. static int b43_tx(struct ieee80211_hw *hw,
  2194. struct sk_buff *skb, struct ieee80211_tx_control *ctl)
  2195. {
  2196. struct b43_wl *wl = hw_to_b43_wl(hw);
  2197. struct b43_wldev *dev = wl->current_dev;
  2198. int err = -ENODEV;
  2199. unsigned long flags;
  2200. if (unlikely(!dev))
  2201. goto out;
  2202. if (unlikely(b43_status(dev) < B43_STAT_STARTED))
  2203. goto out;
  2204. /* DMA-TX is done without a global lock. */
  2205. if (b43_using_pio(dev)) {
  2206. spin_lock_irqsave(&wl->irq_lock, flags);
  2207. err = b43_pio_tx(dev, skb, ctl);
  2208. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2209. } else
  2210. err = b43_dma_tx(dev, skb, ctl);
  2211. out:
  2212. if (unlikely(err))
  2213. return NETDEV_TX_BUSY;
  2214. return NETDEV_TX_OK;
  2215. }
  2216. static int b43_conf_tx(struct ieee80211_hw *hw,
  2217. int queue,
  2218. const struct ieee80211_tx_queue_params *params)
  2219. {
  2220. return 0;
  2221. }
  2222. static int b43_get_tx_stats(struct ieee80211_hw *hw,
  2223. struct ieee80211_tx_queue_stats *stats)
  2224. {
  2225. struct b43_wl *wl = hw_to_b43_wl(hw);
  2226. struct b43_wldev *dev = wl->current_dev;
  2227. unsigned long flags;
  2228. int err = -ENODEV;
  2229. if (!dev)
  2230. goto out;
  2231. spin_lock_irqsave(&wl->irq_lock, flags);
  2232. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2233. if (b43_using_pio(dev))
  2234. b43_pio_get_tx_stats(dev, stats);
  2235. else
  2236. b43_dma_get_tx_stats(dev, stats);
  2237. err = 0;
  2238. }
  2239. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2240. out:
  2241. return err;
  2242. }
  2243. static int b43_get_stats(struct ieee80211_hw *hw,
  2244. struct ieee80211_low_level_stats *stats)
  2245. {
  2246. struct b43_wl *wl = hw_to_b43_wl(hw);
  2247. unsigned long flags;
  2248. spin_lock_irqsave(&wl->irq_lock, flags);
  2249. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2250. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2251. return 0;
  2252. }
  2253. static const char *phymode_to_string(unsigned int phymode)
  2254. {
  2255. switch (phymode) {
  2256. case B43_PHYMODE_A:
  2257. return "A";
  2258. case B43_PHYMODE_B:
  2259. return "B";
  2260. case B43_PHYMODE_G:
  2261. return "G";
  2262. default:
  2263. B43_WARN_ON(1);
  2264. }
  2265. return "";
  2266. }
  2267. static int find_wldev_for_phymode(struct b43_wl *wl,
  2268. unsigned int phymode,
  2269. struct b43_wldev **dev, bool * gmode)
  2270. {
  2271. struct b43_wldev *d;
  2272. list_for_each_entry(d, &wl->devlist, list) {
  2273. if (d->phy.possible_phymodes & phymode) {
  2274. /* Ok, this device supports the PHY-mode.
  2275. * Now figure out how the gmode bit has to be
  2276. * set to support it. */
  2277. if (phymode == B43_PHYMODE_A)
  2278. *gmode = 0;
  2279. else
  2280. *gmode = 1;
  2281. *dev = d;
  2282. return 0;
  2283. }
  2284. }
  2285. return -ESRCH;
  2286. }
  2287. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2288. {
  2289. struct ssb_device *sdev = dev->dev;
  2290. u32 tmslow;
  2291. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2292. tmslow &= ~B43_TMSLOW_GMODE;
  2293. tmslow |= B43_TMSLOW_PHYRESET;
  2294. tmslow |= SSB_TMSLOW_FGC;
  2295. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2296. msleep(1);
  2297. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2298. tmslow &= ~SSB_TMSLOW_FGC;
  2299. tmslow |= B43_TMSLOW_PHYRESET;
  2300. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2301. msleep(1);
  2302. }
  2303. /* Expects wl->mutex locked */
  2304. static int b43_switch_phymode(struct b43_wl *wl, unsigned int new_mode)
  2305. {
  2306. struct b43_wldev *up_dev;
  2307. struct b43_wldev *down_dev;
  2308. int err;
  2309. bool gmode = 0;
  2310. int prev_status;
  2311. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2312. if (err) {
  2313. b43err(wl, "Could not find a device for %s-PHY mode\n",
  2314. phymode_to_string(new_mode));
  2315. return err;
  2316. }
  2317. if ((up_dev == wl->current_dev) &&
  2318. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2319. /* This device is already running. */
  2320. return 0;
  2321. }
  2322. b43dbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2323. phymode_to_string(new_mode));
  2324. down_dev = wl->current_dev;
  2325. prev_status = b43_status(down_dev);
  2326. /* Shutdown the currently running core. */
  2327. if (prev_status >= B43_STAT_STARTED)
  2328. b43_wireless_core_stop(down_dev);
  2329. if (prev_status >= B43_STAT_INITIALIZED)
  2330. b43_wireless_core_exit(down_dev);
  2331. if (down_dev != up_dev) {
  2332. /* We switch to a different core, so we put PHY into
  2333. * RESET on the old core. */
  2334. b43_put_phy_into_reset(down_dev);
  2335. }
  2336. /* Now start the new core. */
  2337. up_dev->phy.gmode = gmode;
  2338. if (prev_status >= B43_STAT_INITIALIZED) {
  2339. err = b43_wireless_core_init(up_dev);
  2340. if (err) {
  2341. b43err(wl, "Fatal: Could not initialize device for "
  2342. "newly selected %s-PHY mode\n",
  2343. phymode_to_string(new_mode));
  2344. goto init_failure;
  2345. }
  2346. }
  2347. if (prev_status >= B43_STAT_STARTED) {
  2348. err = b43_wireless_core_start(up_dev);
  2349. if (err) {
  2350. b43err(wl, "Fatal: Coult not start device for "
  2351. "newly selected %s-PHY mode\n",
  2352. phymode_to_string(new_mode));
  2353. b43_wireless_core_exit(up_dev);
  2354. goto init_failure;
  2355. }
  2356. }
  2357. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2358. wl->current_dev = up_dev;
  2359. return 0;
  2360. init_failure:
  2361. /* Whoops, failed to init the new core. No core is operating now. */
  2362. wl->current_dev = NULL;
  2363. return err;
  2364. }
  2365. static int b43_antenna_from_ieee80211(u8 antenna)
  2366. {
  2367. switch (antenna) {
  2368. case 0: /* default/diversity */
  2369. return B43_ANTENNA_DEFAULT;
  2370. case 1: /* Antenna 0 */
  2371. return B43_ANTENNA0;
  2372. case 2: /* Antenna 1 */
  2373. return B43_ANTENNA1;
  2374. default:
  2375. return B43_ANTENNA_DEFAULT;
  2376. }
  2377. }
  2378. static int b43_dev_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  2379. {
  2380. struct b43_wl *wl = hw_to_b43_wl(hw);
  2381. struct b43_wldev *dev;
  2382. struct b43_phy *phy;
  2383. unsigned long flags;
  2384. unsigned int new_phymode = 0xFFFF;
  2385. int antenna_tx;
  2386. int antenna_rx;
  2387. int err = 0;
  2388. u32 savedirqs;
  2389. antenna_tx = b43_antenna_from_ieee80211(conf->antenna_sel_tx);
  2390. antenna_rx = b43_antenna_from_ieee80211(conf->antenna_sel_rx);
  2391. mutex_lock(&wl->mutex);
  2392. /* Switch the PHY mode (if necessary). */
  2393. switch (conf->phymode) {
  2394. case MODE_IEEE80211A:
  2395. new_phymode = B43_PHYMODE_A;
  2396. break;
  2397. case MODE_IEEE80211B:
  2398. new_phymode = B43_PHYMODE_B;
  2399. break;
  2400. case MODE_IEEE80211G:
  2401. new_phymode = B43_PHYMODE_G;
  2402. break;
  2403. default:
  2404. B43_WARN_ON(1);
  2405. }
  2406. err = b43_switch_phymode(wl, new_phymode);
  2407. if (err)
  2408. goto out_unlock_mutex;
  2409. dev = wl->current_dev;
  2410. phy = &dev->phy;
  2411. /* Disable IRQs while reconfiguring the device.
  2412. * This makes it possible to drop the spinlock throughout
  2413. * the reconfiguration process. */
  2414. spin_lock_irqsave(&wl->irq_lock, flags);
  2415. if (b43_status(dev) < B43_STAT_STARTED) {
  2416. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2417. goto out_unlock_mutex;
  2418. }
  2419. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2420. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2421. b43_synchronize_irq(dev);
  2422. /* Switch to the requested channel.
  2423. * The firmware takes care of races with the TX handler. */
  2424. if (conf->channel_val != phy->channel)
  2425. b43_radio_selectchannel(dev, conf->channel_val, 0);
  2426. /* Enable/Disable ShortSlot timing. */
  2427. if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
  2428. dev->short_slot) {
  2429. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  2430. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
  2431. b43_short_slot_timing_enable(dev);
  2432. else
  2433. b43_short_slot_timing_disable(dev);
  2434. }
  2435. /* Adjust the desired TX power level. */
  2436. if (conf->power_level != 0) {
  2437. if (conf->power_level != phy->power_level) {
  2438. phy->power_level = conf->power_level;
  2439. b43_phy_xmitpower(dev);
  2440. }
  2441. }
  2442. /* Antennas for RX and management frame TX. */
  2443. b43_mgmtframe_txantenna(dev, antenna_tx);
  2444. b43_set_rx_antenna(dev, antenna_rx);
  2445. /* Update templates for AP mode. */
  2446. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  2447. b43_set_beacon_int(dev, conf->beacon_int);
  2448. if (!!conf->radio_enabled != phy->radio_on) {
  2449. if (conf->radio_enabled) {
  2450. b43_radio_turn_on(dev);
  2451. b43info(dev->wl, "Radio turned on by software\n");
  2452. if (!dev->radio_hw_enable) {
  2453. b43info(dev->wl, "The hardware RF-kill button "
  2454. "still turns the radio physically off. "
  2455. "Press the button to turn it on.\n");
  2456. }
  2457. } else {
  2458. b43_radio_turn_off(dev, 0);
  2459. b43info(dev->wl, "Radio turned off by software\n");
  2460. }
  2461. }
  2462. spin_lock_irqsave(&wl->irq_lock, flags);
  2463. b43_interrupt_enable(dev, savedirqs);
  2464. mmiowb();
  2465. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2466. out_unlock_mutex:
  2467. mutex_unlock(&wl->mutex);
  2468. return err;
  2469. }
  2470. static int b43_dev_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2471. const u8 *local_addr, const u8 *addr,
  2472. struct ieee80211_key_conf *key)
  2473. {
  2474. struct b43_wl *wl = hw_to_b43_wl(hw);
  2475. struct b43_wldev *dev = wl->current_dev;
  2476. unsigned long flags;
  2477. u8 algorithm;
  2478. u8 index;
  2479. int err = -EINVAL;
  2480. DECLARE_MAC_BUF(mac);
  2481. if (modparam_nohwcrypt)
  2482. return -ENOSPC; /* User disabled HW-crypto */
  2483. if (!dev)
  2484. return -ENODEV;
  2485. switch (key->alg) {
  2486. case ALG_NONE:
  2487. algorithm = B43_SEC_ALGO_NONE;
  2488. break;
  2489. case ALG_WEP:
  2490. if (key->keylen == 5)
  2491. algorithm = B43_SEC_ALGO_WEP40;
  2492. else
  2493. algorithm = B43_SEC_ALGO_WEP104;
  2494. break;
  2495. case ALG_TKIP:
  2496. algorithm = B43_SEC_ALGO_TKIP;
  2497. break;
  2498. case ALG_CCMP:
  2499. algorithm = B43_SEC_ALGO_AES;
  2500. break;
  2501. default:
  2502. B43_WARN_ON(1);
  2503. goto out;
  2504. }
  2505. index = (u8) (key->keyidx);
  2506. if (index > 3)
  2507. goto out;
  2508. mutex_lock(&wl->mutex);
  2509. spin_lock_irqsave(&wl->irq_lock, flags);
  2510. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  2511. err = -ENODEV;
  2512. goto out_unlock;
  2513. }
  2514. switch (cmd) {
  2515. case SET_KEY:
  2516. if (algorithm == B43_SEC_ALGO_TKIP) {
  2517. /* FIXME: No TKIP hardware encryption for now. */
  2518. err = -EOPNOTSUPP;
  2519. goto out_unlock;
  2520. }
  2521. if (is_broadcast_ether_addr(addr)) {
  2522. /* addr is FF:FF:FF:FF:FF:FF for default keys */
  2523. err = b43_key_write(dev, index, algorithm,
  2524. key->key, key->keylen, NULL, key);
  2525. } else {
  2526. /*
  2527. * either pairwise key or address is 00:00:00:00:00:00
  2528. * for transmit-only keys
  2529. */
  2530. err = b43_key_write(dev, -1, algorithm,
  2531. key->key, key->keylen, addr, key);
  2532. }
  2533. if (err)
  2534. goto out_unlock;
  2535. if (algorithm == B43_SEC_ALGO_WEP40 ||
  2536. algorithm == B43_SEC_ALGO_WEP104) {
  2537. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  2538. } else {
  2539. b43_hf_write(dev,
  2540. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  2541. }
  2542. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2543. break;
  2544. case DISABLE_KEY: {
  2545. err = b43_key_clear(dev, key->hw_key_idx);
  2546. if (err)
  2547. goto out_unlock;
  2548. break;
  2549. }
  2550. default:
  2551. B43_WARN_ON(1);
  2552. }
  2553. out_unlock:
  2554. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2555. mutex_unlock(&wl->mutex);
  2556. out:
  2557. if (!err) {
  2558. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  2559. "mac: %s\n",
  2560. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  2561. print_mac(mac, addr));
  2562. }
  2563. return err;
  2564. }
  2565. static void b43_configure_filter(struct ieee80211_hw *hw,
  2566. unsigned int changed, unsigned int *fflags,
  2567. int mc_count, struct dev_addr_list *mc_list)
  2568. {
  2569. struct b43_wl *wl = hw_to_b43_wl(hw);
  2570. struct b43_wldev *dev = wl->current_dev;
  2571. unsigned long flags;
  2572. if (!dev) {
  2573. *fflags = 0;
  2574. return;
  2575. }
  2576. spin_lock_irqsave(&wl->irq_lock, flags);
  2577. *fflags &= FIF_PROMISC_IN_BSS |
  2578. FIF_ALLMULTI |
  2579. FIF_FCSFAIL |
  2580. FIF_PLCPFAIL |
  2581. FIF_CONTROL |
  2582. FIF_OTHER_BSS |
  2583. FIF_BCN_PRBRESP_PROMISC;
  2584. changed &= FIF_PROMISC_IN_BSS |
  2585. FIF_ALLMULTI |
  2586. FIF_FCSFAIL |
  2587. FIF_PLCPFAIL |
  2588. FIF_CONTROL |
  2589. FIF_OTHER_BSS |
  2590. FIF_BCN_PRBRESP_PROMISC;
  2591. wl->filter_flags = *fflags;
  2592. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  2593. b43_adjust_opmode(dev);
  2594. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2595. }
  2596. static int b43_config_interface(struct ieee80211_hw *hw,
  2597. int if_id, struct ieee80211_if_conf *conf)
  2598. {
  2599. struct b43_wl *wl = hw_to_b43_wl(hw);
  2600. struct b43_wldev *dev = wl->current_dev;
  2601. unsigned long flags;
  2602. if (!dev)
  2603. return -ENODEV;
  2604. mutex_lock(&wl->mutex);
  2605. spin_lock_irqsave(&wl->irq_lock, flags);
  2606. B43_WARN_ON(wl->if_id != if_id);
  2607. if (conf->bssid)
  2608. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2609. else
  2610. memset(wl->bssid, 0, ETH_ALEN);
  2611. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  2612. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
  2613. B43_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
  2614. b43_set_ssid(dev, conf->ssid, conf->ssid_len);
  2615. if (conf->beacon)
  2616. b43_refresh_templates(dev, conf->beacon);
  2617. }
  2618. b43_write_mac_bssid_templates(dev);
  2619. }
  2620. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2621. mutex_unlock(&wl->mutex);
  2622. return 0;
  2623. }
  2624. /* Locking: wl->mutex */
  2625. static void b43_wireless_core_stop(struct b43_wldev *dev)
  2626. {
  2627. struct b43_wl *wl = dev->wl;
  2628. unsigned long flags;
  2629. if (b43_status(dev) < B43_STAT_STARTED)
  2630. return;
  2631. b43_set_status(dev, B43_STAT_INITIALIZED);
  2632. mutex_unlock(&wl->mutex);
  2633. /* Must unlock as it would otherwise deadlock. No races here.
  2634. * Cancel the possibly running self-rearming periodic work. */
  2635. cancel_delayed_work_sync(&dev->periodic_work);
  2636. mutex_lock(&wl->mutex);
  2637. ieee80211_stop_queues(wl->hw); //FIXME this could cause a deadlock, as mac80211 seems buggy.
  2638. /* Disable and sync interrupts. */
  2639. spin_lock_irqsave(&wl->irq_lock, flags);
  2640. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2641. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  2642. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2643. b43_synchronize_irq(dev);
  2644. b43_mac_suspend(dev);
  2645. free_irq(dev->dev->irq, dev);
  2646. b43dbg(wl, "Wireless interface stopped\n");
  2647. }
  2648. /* Locking: wl->mutex */
  2649. static int b43_wireless_core_start(struct b43_wldev *dev)
  2650. {
  2651. int err;
  2652. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  2653. drain_txstatus_queue(dev);
  2654. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  2655. IRQF_SHARED, KBUILD_MODNAME, dev);
  2656. if (err) {
  2657. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  2658. goto out;
  2659. }
  2660. /* We are ready to run. */
  2661. b43_set_status(dev, B43_STAT_STARTED);
  2662. /* Start data flow (TX/RX). */
  2663. b43_mac_enable(dev);
  2664. b43_interrupt_enable(dev, dev->irq_savedstate);
  2665. ieee80211_start_queues(dev->wl->hw);
  2666. /* Start maintainance work */
  2667. b43_periodic_tasks_setup(dev);
  2668. b43dbg(dev->wl, "Wireless interface started\n");
  2669. out:
  2670. return err;
  2671. }
  2672. /* Get PHY and RADIO versioning numbers */
  2673. static int b43_phy_versioning(struct b43_wldev *dev)
  2674. {
  2675. struct b43_phy *phy = &dev->phy;
  2676. u32 tmp;
  2677. u8 analog_type;
  2678. u8 phy_type;
  2679. u8 phy_rev;
  2680. u16 radio_manuf;
  2681. u16 radio_ver;
  2682. u16 radio_rev;
  2683. int unsupported = 0;
  2684. /* Get PHY versioning */
  2685. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  2686. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  2687. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  2688. phy_rev = (tmp & B43_PHYVER_VERSION);
  2689. switch (phy_type) {
  2690. case B43_PHYTYPE_A:
  2691. if (phy_rev >= 4)
  2692. unsupported = 1;
  2693. break;
  2694. case B43_PHYTYPE_B:
  2695. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  2696. && phy_rev != 7)
  2697. unsupported = 1;
  2698. break;
  2699. case B43_PHYTYPE_G:
  2700. if (phy_rev > 8)
  2701. unsupported = 1;
  2702. break;
  2703. default:
  2704. unsupported = 1;
  2705. };
  2706. if (unsupported) {
  2707. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  2708. "(Analog %u, Type %u, Revision %u)\n",
  2709. analog_type, phy_type, phy_rev);
  2710. return -EOPNOTSUPP;
  2711. }
  2712. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2713. analog_type, phy_type, phy_rev);
  2714. /* Get RADIO versioning */
  2715. if (dev->dev->bus->chip_id == 0x4317) {
  2716. if (dev->dev->bus->chip_rev == 0)
  2717. tmp = 0x3205017F;
  2718. else if (dev->dev->bus->chip_rev == 1)
  2719. tmp = 0x4205017F;
  2720. else
  2721. tmp = 0x5205017F;
  2722. } else {
  2723. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2724. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH);
  2725. tmp <<= 16;
  2726. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2727. tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  2728. }
  2729. radio_manuf = (tmp & 0x00000FFF);
  2730. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2731. radio_rev = (tmp & 0xF0000000) >> 28;
  2732. switch (phy_type) {
  2733. case B43_PHYTYPE_A:
  2734. if (radio_ver != 0x2060)
  2735. unsupported = 1;
  2736. if (radio_rev != 1)
  2737. unsupported = 1;
  2738. if (radio_manuf != 0x17F)
  2739. unsupported = 1;
  2740. break;
  2741. case B43_PHYTYPE_B:
  2742. if ((radio_ver & 0xFFF0) != 0x2050)
  2743. unsupported = 1;
  2744. break;
  2745. case B43_PHYTYPE_G:
  2746. if (radio_ver != 0x2050)
  2747. unsupported = 1;
  2748. break;
  2749. default:
  2750. B43_WARN_ON(1);
  2751. }
  2752. if (unsupported) {
  2753. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  2754. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2755. radio_manuf, radio_ver, radio_rev);
  2756. return -EOPNOTSUPP;
  2757. }
  2758. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  2759. radio_manuf, radio_ver, radio_rev);
  2760. phy->radio_manuf = radio_manuf;
  2761. phy->radio_ver = radio_ver;
  2762. phy->radio_rev = radio_rev;
  2763. phy->analog = analog_type;
  2764. phy->type = phy_type;
  2765. phy->rev = phy_rev;
  2766. return 0;
  2767. }
  2768. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  2769. struct b43_phy *phy)
  2770. {
  2771. struct b43_txpower_lo_control *lo;
  2772. int i;
  2773. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2774. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2775. /* Flags */
  2776. phy->locked = 0;
  2777. phy->aci_enable = 0;
  2778. phy->aci_wlan_automatic = 0;
  2779. phy->aci_hw_rssi = 0;
  2780. phy->radio_off_context.valid = 0;
  2781. lo = phy->lo_control;
  2782. if (lo) {
  2783. memset(lo, 0, sizeof(*(phy->lo_control)));
  2784. lo->rebuild = 1;
  2785. lo->tx_bias = 0xFF;
  2786. }
  2787. phy->max_lb_gain = 0;
  2788. phy->trsw_rx_gain = 0;
  2789. phy->txpwr_offset = 0;
  2790. /* NRSSI */
  2791. phy->nrssislope = 0;
  2792. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2793. phy->nrssi[i] = -1000;
  2794. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2795. phy->nrssi_lt[i] = i;
  2796. phy->lofcal = 0xFFFF;
  2797. phy->initval = 0xFFFF;
  2798. spin_lock_init(&phy->lock);
  2799. phy->interfmode = B43_INTERFMODE_NONE;
  2800. phy->channel = 0xFF;
  2801. phy->hardware_power_control = !!modparam_hwpctl;
  2802. }
  2803. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  2804. {
  2805. /* Flags */
  2806. dev->reg124_set_0x4 = 0;
  2807. /* Assume the radio is enabled. If it's not enabled, the state will
  2808. * immediately get fixed on the first periodic work run. */
  2809. dev->radio_hw_enable = 1;
  2810. /* Stats */
  2811. memset(&dev->stats, 0, sizeof(dev->stats));
  2812. setup_struct_phy_for_init(dev, &dev->phy);
  2813. /* IRQ related flags */
  2814. dev->irq_reason = 0;
  2815. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2816. dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
  2817. dev->mac_suspended = 1;
  2818. /* Noise calculation context */
  2819. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2820. }
  2821. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  2822. {
  2823. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2824. u32 hf;
  2825. if (!(sprom->r1.boardflags_lo & B43_BFL_BTCOEXIST))
  2826. return;
  2827. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  2828. return;
  2829. hf = b43_hf_read(dev);
  2830. if (sprom->r1.boardflags_lo & B43_BFL_BTCMOD)
  2831. hf |= B43_HF_BTCOEXALT;
  2832. else
  2833. hf |= B43_HF_BTCOEX;
  2834. b43_hf_write(dev, hf);
  2835. //TODO
  2836. }
  2837. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  2838. { //TODO
  2839. }
  2840. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  2841. {
  2842. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2843. struct ssb_bus *bus = dev->dev->bus;
  2844. u32 tmp;
  2845. if (bus->pcicore.dev &&
  2846. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2847. bus->pcicore.dev->id.revision <= 5) {
  2848. /* IMCFGLO timeouts workaround. */
  2849. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2850. tmp &= ~SSB_IMCFGLO_REQTO;
  2851. tmp &= ~SSB_IMCFGLO_SERTO;
  2852. switch (bus->bustype) {
  2853. case SSB_BUSTYPE_PCI:
  2854. case SSB_BUSTYPE_PCMCIA:
  2855. tmp |= 0x32;
  2856. break;
  2857. case SSB_BUSTYPE_SSB:
  2858. tmp |= 0x53;
  2859. break;
  2860. }
  2861. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2862. }
  2863. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2864. }
  2865. /* Shutdown a wireless core */
  2866. /* Locking: wl->mutex */
  2867. static void b43_wireless_core_exit(struct b43_wldev *dev)
  2868. {
  2869. struct b43_phy *phy = &dev->phy;
  2870. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  2871. if (b43_status(dev) != B43_STAT_INITIALIZED)
  2872. return;
  2873. b43_set_status(dev, B43_STAT_UNINIT);
  2874. mutex_unlock(&dev->wl->mutex);
  2875. b43_rfkill_exit(dev);
  2876. mutex_lock(&dev->wl->mutex);
  2877. b43_rng_exit(dev->wl);
  2878. b43_pio_free(dev);
  2879. b43_dma_free(dev);
  2880. b43_chip_exit(dev);
  2881. b43_radio_turn_off(dev, 1);
  2882. b43_switch_analog(dev, 0);
  2883. if (phy->dyn_tssi_tbl)
  2884. kfree(phy->tssi2dbm);
  2885. kfree(phy->lo_control);
  2886. phy->lo_control = NULL;
  2887. ssb_device_disable(dev->dev, 0);
  2888. ssb_bus_may_powerdown(dev->dev->bus);
  2889. }
  2890. /* Initialize a wireless core */
  2891. static int b43_wireless_core_init(struct b43_wldev *dev)
  2892. {
  2893. struct b43_wl *wl = dev->wl;
  2894. struct ssb_bus *bus = dev->dev->bus;
  2895. struct ssb_sprom *sprom = &bus->sprom;
  2896. struct b43_phy *phy = &dev->phy;
  2897. int err;
  2898. u32 hf, tmp;
  2899. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  2900. err = ssb_bus_powerup(bus, 0);
  2901. if (err)
  2902. goto out;
  2903. if (!ssb_device_is_enabled(dev->dev)) {
  2904. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  2905. b43_wireless_core_reset(dev, tmp);
  2906. }
  2907. if ((phy->type == B43_PHYTYPE_B) || (phy->type == B43_PHYTYPE_G)) {
  2908. phy->lo_control =
  2909. kzalloc(sizeof(*(phy->lo_control)), GFP_KERNEL);
  2910. if (!phy->lo_control) {
  2911. err = -ENOMEM;
  2912. goto err_busdown;
  2913. }
  2914. }
  2915. setup_struct_wldev_for_init(dev);
  2916. err = b43_phy_init_tssi2dbm_table(dev);
  2917. if (err)
  2918. goto err_kfree_lo_control;
  2919. /* Enable IRQ routing to this device. */
  2920. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  2921. b43_imcfglo_timeouts_workaround(dev);
  2922. b43_bluetooth_coext_disable(dev);
  2923. b43_phy_early_init(dev);
  2924. err = b43_chip_init(dev);
  2925. if (err)
  2926. goto err_kfree_tssitbl;
  2927. b43_shm_write16(dev, B43_SHM_SHARED,
  2928. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  2929. hf = b43_hf_read(dev);
  2930. if (phy->type == B43_PHYTYPE_G) {
  2931. hf |= B43_HF_SYMW;
  2932. if (phy->rev == 1)
  2933. hf |= B43_HF_GDCW;
  2934. if (sprom->r1.boardflags_lo & B43_BFL_PACTRL)
  2935. hf |= B43_HF_OFDMPABOOST;
  2936. } else if (phy->type == B43_PHYTYPE_B) {
  2937. hf |= B43_HF_SYMW;
  2938. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  2939. hf &= ~B43_HF_GDCW;
  2940. }
  2941. b43_hf_write(dev, hf);
  2942. /* Short/Long Retry Limit.
  2943. * The retry-limit is a 4-bit counter. Enforce this to avoid overflowing
  2944. * the chip-internal counter.
  2945. */
  2946. tmp = limit_value(modparam_short_retry, 0, 0xF);
  2947. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT, tmp);
  2948. tmp = limit_value(modparam_long_retry, 0, 0xF);
  2949. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT, tmp);
  2950. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  2951. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  2952. /* Disable sending probe responses from firmware.
  2953. * Setting the MaxTime to one usec will always trigger
  2954. * a timeout, so we never send any probe resp.
  2955. * A timeout of zero is infinite. */
  2956. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  2957. b43_rate_memory_init(dev);
  2958. /* Minimum Contention Window */
  2959. if (phy->type == B43_PHYTYPE_B) {
  2960. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  2961. } else {
  2962. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  2963. }
  2964. /* Maximum Contention Window */
  2965. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  2966. do {
  2967. if (b43_using_pio(dev)) {
  2968. err = b43_pio_init(dev);
  2969. } else {
  2970. err = b43_dma_init(dev);
  2971. if (!err)
  2972. b43_qos_init(dev);
  2973. }
  2974. } while (err == -EAGAIN);
  2975. if (err)
  2976. goto err_chip_exit;
  2977. //FIXME
  2978. #if 1
  2979. b43_write16(dev, 0x0612, 0x0050);
  2980. b43_shm_write16(dev, B43_SHM_SHARED, 0x0416, 0x0050);
  2981. b43_shm_write16(dev, B43_SHM_SHARED, 0x0414, 0x01F4);
  2982. #endif
  2983. b43_bluetooth_coext_enable(dev);
  2984. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  2985. memset(wl->bssid, 0, ETH_ALEN);
  2986. memset(wl->mac_addr, 0, ETH_ALEN);
  2987. b43_upload_card_macaddress(dev);
  2988. b43_security_init(dev);
  2989. b43_rfkill_init(dev);
  2990. b43_rng_init(wl);
  2991. b43_set_status(dev, B43_STAT_INITIALIZED);
  2992. out:
  2993. return err;
  2994. err_chip_exit:
  2995. b43_chip_exit(dev);
  2996. err_kfree_tssitbl:
  2997. if (phy->dyn_tssi_tbl)
  2998. kfree(phy->tssi2dbm);
  2999. err_kfree_lo_control:
  3000. kfree(phy->lo_control);
  3001. phy->lo_control = NULL;
  3002. err_busdown:
  3003. ssb_bus_may_powerdown(bus);
  3004. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3005. return err;
  3006. }
  3007. static int b43_add_interface(struct ieee80211_hw *hw,
  3008. struct ieee80211_if_init_conf *conf)
  3009. {
  3010. struct b43_wl *wl = hw_to_b43_wl(hw);
  3011. struct b43_wldev *dev;
  3012. unsigned long flags;
  3013. int err = -EOPNOTSUPP;
  3014. /* TODO: allow WDS/AP devices to coexist */
  3015. if (conf->type != IEEE80211_IF_TYPE_AP &&
  3016. conf->type != IEEE80211_IF_TYPE_STA &&
  3017. conf->type != IEEE80211_IF_TYPE_WDS &&
  3018. conf->type != IEEE80211_IF_TYPE_IBSS)
  3019. return -EOPNOTSUPP;
  3020. mutex_lock(&wl->mutex);
  3021. if (wl->operating)
  3022. goto out_mutex_unlock;
  3023. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  3024. dev = wl->current_dev;
  3025. wl->operating = 1;
  3026. wl->if_id = conf->if_id;
  3027. wl->if_type = conf->type;
  3028. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  3029. spin_lock_irqsave(&wl->irq_lock, flags);
  3030. b43_adjust_opmode(dev);
  3031. b43_upload_card_macaddress(dev);
  3032. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3033. err = 0;
  3034. out_mutex_unlock:
  3035. mutex_unlock(&wl->mutex);
  3036. return err;
  3037. }
  3038. static void b43_remove_interface(struct ieee80211_hw *hw,
  3039. struct ieee80211_if_init_conf *conf)
  3040. {
  3041. struct b43_wl *wl = hw_to_b43_wl(hw);
  3042. struct b43_wldev *dev = wl->current_dev;
  3043. unsigned long flags;
  3044. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3045. mutex_lock(&wl->mutex);
  3046. B43_WARN_ON(!wl->operating);
  3047. B43_WARN_ON(wl->if_id != conf->if_id);
  3048. wl->operating = 0;
  3049. spin_lock_irqsave(&wl->irq_lock, flags);
  3050. b43_adjust_opmode(dev);
  3051. memset(wl->mac_addr, 0, ETH_ALEN);
  3052. b43_upload_card_macaddress(dev);
  3053. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3054. mutex_unlock(&wl->mutex);
  3055. }
  3056. static int b43_start(struct ieee80211_hw *hw)
  3057. {
  3058. struct b43_wl *wl = hw_to_b43_wl(hw);
  3059. struct b43_wldev *dev = wl->current_dev;
  3060. int did_init = 0;
  3061. int err;
  3062. mutex_lock(&wl->mutex);
  3063. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3064. err = b43_wireless_core_init(dev);
  3065. if (err)
  3066. goto out_mutex_unlock;
  3067. did_init = 1;
  3068. }
  3069. if (b43_status(dev) < B43_STAT_STARTED) {
  3070. err = b43_wireless_core_start(dev);
  3071. if (err) {
  3072. if (did_init)
  3073. b43_wireless_core_exit(dev);
  3074. goto out_mutex_unlock;
  3075. }
  3076. }
  3077. out_mutex_unlock:
  3078. mutex_unlock(&wl->mutex);
  3079. return err;
  3080. }
  3081. void b43_stop(struct ieee80211_hw *hw)
  3082. {
  3083. struct b43_wl *wl = hw_to_b43_wl(hw);
  3084. struct b43_wldev *dev = wl->current_dev;
  3085. mutex_lock(&wl->mutex);
  3086. if (b43_status(dev) >= B43_STAT_STARTED)
  3087. b43_wireless_core_stop(dev);
  3088. b43_wireless_core_exit(dev);
  3089. mutex_unlock(&wl->mutex);
  3090. }
  3091. static const struct ieee80211_ops b43_hw_ops = {
  3092. .tx = b43_tx,
  3093. .conf_tx = b43_conf_tx,
  3094. .add_interface = b43_add_interface,
  3095. .remove_interface = b43_remove_interface,
  3096. .config = b43_dev_config,
  3097. .config_interface = b43_config_interface,
  3098. .configure_filter = b43_configure_filter,
  3099. .set_key = b43_dev_set_key,
  3100. .get_stats = b43_get_stats,
  3101. .get_tx_stats = b43_get_tx_stats,
  3102. .start = b43_start,
  3103. .stop = b43_stop,
  3104. };
  3105. /* Hard-reset the chip. Do not call this directly.
  3106. * Use b43_controller_restart()
  3107. */
  3108. static void b43_chip_reset(struct work_struct *work)
  3109. {
  3110. struct b43_wldev *dev =
  3111. container_of(work, struct b43_wldev, restart_work);
  3112. struct b43_wl *wl = dev->wl;
  3113. int err = 0;
  3114. int prev_status;
  3115. mutex_lock(&wl->mutex);
  3116. prev_status = b43_status(dev);
  3117. /* Bring the device down... */
  3118. if (prev_status >= B43_STAT_STARTED)
  3119. b43_wireless_core_stop(dev);
  3120. if (prev_status >= B43_STAT_INITIALIZED)
  3121. b43_wireless_core_exit(dev);
  3122. /* ...and up again. */
  3123. if (prev_status >= B43_STAT_INITIALIZED) {
  3124. err = b43_wireless_core_init(dev);
  3125. if (err)
  3126. goto out;
  3127. }
  3128. if (prev_status >= B43_STAT_STARTED) {
  3129. err = b43_wireless_core_start(dev);
  3130. if (err) {
  3131. b43_wireless_core_exit(dev);
  3132. goto out;
  3133. }
  3134. }
  3135. out:
  3136. mutex_unlock(&wl->mutex);
  3137. if (err)
  3138. b43err(wl, "Controller restart FAILED\n");
  3139. else
  3140. b43info(wl, "Controller restarted\n");
  3141. }
  3142. static int b43_setup_modes(struct b43_wldev *dev,
  3143. int have_aphy, int have_bphy, int have_gphy)
  3144. {
  3145. struct ieee80211_hw *hw = dev->wl->hw;
  3146. struct ieee80211_hw_mode *mode;
  3147. struct b43_phy *phy = &dev->phy;
  3148. int cnt = 0;
  3149. int err;
  3150. /*FIXME: Don't tell ieee80211 about an A-PHY, because we currently don't support A-PHY. */
  3151. have_aphy = 0;
  3152. phy->possible_phymodes = 0;
  3153. for (; 1; cnt++) {
  3154. if (have_aphy) {
  3155. B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
  3156. mode = &phy->hwmodes[cnt];
  3157. mode->mode = MODE_IEEE80211A;
  3158. mode->num_channels = b43_a_chantable_size;
  3159. mode->channels = b43_a_chantable;
  3160. mode->num_rates = b43_a_ratetable_size;
  3161. mode->rates = b43_a_ratetable;
  3162. err = ieee80211_register_hwmode(hw, mode);
  3163. if (err)
  3164. return err;
  3165. phy->possible_phymodes |= B43_PHYMODE_A;
  3166. have_aphy = 0;
  3167. continue;
  3168. }
  3169. if (have_bphy) {
  3170. B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
  3171. mode = &phy->hwmodes[cnt];
  3172. mode->mode = MODE_IEEE80211B;
  3173. mode->num_channels = b43_bg_chantable_size;
  3174. mode->channels = b43_bg_chantable;
  3175. mode->num_rates = b43_b_ratetable_size;
  3176. mode->rates = b43_b_ratetable;
  3177. err = ieee80211_register_hwmode(hw, mode);
  3178. if (err)
  3179. return err;
  3180. phy->possible_phymodes |= B43_PHYMODE_B;
  3181. have_bphy = 0;
  3182. continue;
  3183. }
  3184. if (have_gphy) {
  3185. B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
  3186. mode = &phy->hwmodes[cnt];
  3187. mode->mode = MODE_IEEE80211G;
  3188. mode->num_channels = b43_bg_chantable_size;
  3189. mode->channels = b43_bg_chantable;
  3190. mode->num_rates = b43_g_ratetable_size;
  3191. mode->rates = b43_g_ratetable;
  3192. err = ieee80211_register_hwmode(hw, mode);
  3193. if (err)
  3194. return err;
  3195. phy->possible_phymodes |= B43_PHYMODE_G;
  3196. have_gphy = 0;
  3197. continue;
  3198. }
  3199. break;
  3200. }
  3201. return 0;
  3202. }
  3203. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3204. {
  3205. b43_rfkill_free(dev);
  3206. /* We release firmware that late to not be required to re-request
  3207. * is all the time when we reinit the core. */
  3208. b43_release_firmware(dev);
  3209. }
  3210. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3211. {
  3212. struct b43_wl *wl = dev->wl;
  3213. struct ssb_bus *bus = dev->dev->bus;
  3214. struct pci_dev *pdev = bus->host_pci;
  3215. int err;
  3216. int have_aphy = 0, have_bphy = 0, have_gphy = 0;
  3217. u32 tmp;
  3218. /* Do NOT do any device initialization here.
  3219. * Do it in wireless_core_init() instead.
  3220. * This function is for gathering basic information about the HW, only.
  3221. * Also some structs may be set up here. But most likely you want to have
  3222. * that in core_init(), too.
  3223. */
  3224. err = ssb_bus_powerup(bus, 0);
  3225. if (err) {
  3226. b43err(wl, "Bus powerup failed\n");
  3227. goto out;
  3228. }
  3229. /* Get the PHY type. */
  3230. if (dev->dev->id.revision >= 5) {
  3231. u32 tmshigh;
  3232. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3233. have_aphy = !!(tmshigh & B43_TMSHIGH_APHY);
  3234. have_gphy = !!(tmshigh & B43_TMSHIGH_GPHY);
  3235. if (!have_aphy && !have_gphy)
  3236. have_bphy = 1;
  3237. } else if (dev->dev->id.revision == 4) {
  3238. have_gphy = 1;
  3239. have_aphy = 1;
  3240. } else
  3241. have_bphy = 1;
  3242. dev->phy.gmode = (have_gphy || have_bphy);
  3243. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3244. b43_wireless_core_reset(dev, tmp);
  3245. err = b43_phy_versioning(dev);
  3246. if (err)
  3247. goto err_powerdown;
  3248. /* Check if this device supports multiband. */
  3249. if (!pdev ||
  3250. (pdev->device != 0x4312 &&
  3251. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3252. /* No multiband support. */
  3253. have_aphy = 0;
  3254. have_bphy = 0;
  3255. have_gphy = 0;
  3256. switch (dev->phy.type) {
  3257. case B43_PHYTYPE_A:
  3258. have_aphy = 1;
  3259. break;
  3260. case B43_PHYTYPE_B:
  3261. have_bphy = 1;
  3262. break;
  3263. case B43_PHYTYPE_G:
  3264. have_gphy = 1;
  3265. break;
  3266. default:
  3267. B43_WARN_ON(1);
  3268. }
  3269. }
  3270. dev->phy.gmode = (have_gphy || have_bphy);
  3271. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3272. b43_wireless_core_reset(dev, tmp);
  3273. err = b43_validate_chipaccess(dev);
  3274. if (err)
  3275. goto err_powerdown;
  3276. err = b43_setup_modes(dev, have_aphy, have_bphy, have_gphy);
  3277. if (err)
  3278. goto err_powerdown;
  3279. /* Now set some default "current_dev" */
  3280. if (!wl->current_dev)
  3281. wl->current_dev = dev;
  3282. INIT_WORK(&dev->restart_work, b43_chip_reset);
  3283. b43_rfkill_alloc(dev);
  3284. b43_radio_turn_off(dev, 1);
  3285. b43_switch_analog(dev, 0);
  3286. ssb_device_disable(dev->dev, 0);
  3287. ssb_bus_may_powerdown(bus);
  3288. out:
  3289. return err;
  3290. err_powerdown:
  3291. ssb_bus_may_powerdown(bus);
  3292. return err;
  3293. }
  3294. static void b43_one_core_detach(struct ssb_device *dev)
  3295. {
  3296. struct b43_wldev *wldev;
  3297. struct b43_wl *wl;
  3298. wldev = ssb_get_drvdata(dev);
  3299. wl = wldev->wl;
  3300. cancel_work_sync(&wldev->restart_work);
  3301. b43_debugfs_remove_device(wldev);
  3302. b43_wireless_core_detach(wldev);
  3303. list_del(&wldev->list);
  3304. wl->nr_devs--;
  3305. ssb_set_drvdata(dev, NULL);
  3306. kfree(wldev);
  3307. }
  3308. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  3309. {
  3310. struct b43_wldev *wldev;
  3311. struct pci_dev *pdev;
  3312. int err = -ENOMEM;
  3313. if (!list_empty(&wl->devlist)) {
  3314. /* We are not the first core on this chip. */
  3315. pdev = dev->bus->host_pci;
  3316. /* Only special chips support more than one wireless
  3317. * core, although some of the other chips have more than
  3318. * one wireless core as well. Check for this and
  3319. * bail out early.
  3320. */
  3321. if (!pdev ||
  3322. ((pdev->device != 0x4321) &&
  3323. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  3324. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  3325. return -ENODEV;
  3326. }
  3327. }
  3328. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3329. if (!wldev)
  3330. goto out;
  3331. wldev->dev = dev;
  3332. wldev->wl = wl;
  3333. b43_set_status(wldev, B43_STAT_UNINIT);
  3334. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3335. tasklet_init(&wldev->isr_tasklet,
  3336. (void (*)(unsigned long))b43_interrupt_tasklet,
  3337. (unsigned long)wldev);
  3338. if (modparam_pio)
  3339. wldev->__using_pio = 1;
  3340. INIT_LIST_HEAD(&wldev->list);
  3341. err = b43_wireless_core_attach(wldev);
  3342. if (err)
  3343. goto err_kfree_wldev;
  3344. list_add(&wldev->list, &wl->devlist);
  3345. wl->nr_devs++;
  3346. ssb_set_drvdata(dev, wldev);
  3347. b43_debugfs_add_device(wldev);
  3348. out:
  3349. return err;
  3350. err_kfree_wldev:
  3351. kfree(wldev);
  3352. return err;
  3353. }
  3354. static void b43_sprom_fixup(struct ssb_bus *bus)
  3355. {
  3356. /* boardflags workarounds */
  3357. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  3358. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  3359. bus->sprom.r1.boardflags_lo |= B43_BFL_BTCOEXIST;
  3360. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3361. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  3362. bus->sprom.r1.boardflags_lo |= B43_BFL_PACTRL;
  3363. /* Handle case when gain is not set in sprom */
  3364. if (bus->sprom.r1.antenna_gain_a == 0xFF)
  3365. bus->sprom.r1.antenna_gain_a = 2;
  3366. if (bus->sprom.r1.antenna_gain_bg == 0xFF)
  3367. bus->sprom.r1.antenna_gain_bg = 2;
  3368. /* Convert Antennagain values to Q5.2 */
  3369. bus->sprom.r1.antenna_gain_a <<= 2;
  3370. bus->sprom.r1.antenna_gain_bg <<= 2;
  3371. }
  3372. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  3373. {
  3374. struct ieee80211_hw *hw = wl->hw;
  3375. ssb_set_devtypedata(dev, NULL);
  3376. ieee80211_free_hw(hw);
  3377. }
  3378. static int b43_wireless_init(struct ssb_device *dev)
  3379. {
  3380. struct ssb_sprom *sprom = &dev->bus->sprom;
  3381. struct ieee80211_hw *hw;
  3382. struct b43_wl *wl;
  3383. int err = -ENOMEM;
  3384. b43_sprom_fixup(dev->bus);
  3385. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  3386. if (!hw) {
  3387. b43err(NULL, "Could not allocate ieee80211 device\n");
  3388. goto out;
  3389. }
  3390. /* fill hw info */
  3391. hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
  3392. hw->max_signal = 100;
  3393. hw->max_rssi = -110;
  3394. hw->max_noise = -110;
  3395. hw->queues = 1; /* FIXME: hardware has more queues */
  3396. SET_IEEE80211_DEV(hw, dev->dev);
  3397. if (is_valid_ether_addr(sprom->r1.et1mac))
  3398. SET_IEEE80211_PERM_ADDR(hw, sprom->r1.et1mac);
  3399. else
  3400. SET_IEEE80211_PERM_ADDR(hw, sprom->r1.il0mac);
  3401. /* Get and initialize struct b43_wl */
  3402. wl = hw_to_b43_wl(hw);
  3403. memset(wl, 0, sizeof(*wl));
  3404. wl->hw = hw;
  3405. spin_lock_init(&wl->irq_lock);
  3406. spin_lock_init(&wl->leds_lock);
  3407. mutex_init(&wl->mutex);
  3408. INIT_LIST_HEAD(&wl->devlist);
  3409. ssb_set_devtypedata(dev, wl);
  3410. b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3411. err = 0;
  3412. out:
  3413. return err;
  3414. }
  3415. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  3416. {
  3417. struct b43_wl *wl;
  3418. int err;
  3419. int first = 0;
  3420. wl = ssb_get_devtypedata(dev);
  3421. if (!wl) {
  3422. /* Probing the first core. Must setup common struct b43_wl */
  3423. first = 1;
  3424. err = b43_wireless_init(dev);
  3425. if (err)
  3426. goto out;
  3427. wl = ssb_get_devtypedata(dev);
  3428. B43_WARN_ON(!wl);
  3429. }
  3430. err = b43_one_core_attach(dev, wl);
  3431. if (err)
  3432. goto err_wireless_exit;
  3433. if (first) {
  3434. err = ieee80211_register_hw(wl->hw);
  3435. if (err)
  3436. goto err_one_core_detach;
  3437. }
  3438. out:
  3439. return err;
  3440. err_one_core_detach:
  3441. b43_one_core_detach(dev);
  3442. err_wireless_exit:
  3443. if (first)
  3444. b43_wireless_exit(dev, wl);
  3445. return err;
  3446. }
  3447. static void b43_remove(struct ssb_device *dev)
  3448. {
  3449. struct b43_wl *wl = ssb_get_devtypedata(dev);
  3450. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3451. B43_WARN_ON(!wl);
  3452. if (wl->current_dev == wldev)
  3453. ieee80211_unregister_hw(wl->hw);
  3454. b43_one_core_detach(dev);
  3455. if (list_empty(&wl->devlist)) {
  3456. /* Last core on the chip unregistered.
  3457. * We can destroy common struct b43_wl.
  3458. */
  3459. b43_wireless_exit(dev, wl);
  3460. }
  3461. }
  3462. /* Perform a hardware reset. This can be called from any context. */
  3463. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  3464. {
  3465. /* Must avoid requeueing, if we are in shutdown. */
  3466. if (b43_status(dev) < B43_STAT_INITIALIZED)
  3467. return;
  3468. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  3469. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  3470. }
  3471. #ifdef CONFIG_PM
  3472. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  3473. {
  3474. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3475. struct b43_wl *wl = wldev->wl;
  3476. b43dbg(wl, "Suspending...\n");
  3477. mutex_lock(&wl->mutex);
  3478. wldev->suspend_init_status = b43_status(wldev);
  3479. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  3480. b43_wireless_core_stop(wldev);
  3481. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  3482. b43_wireless_core_exit(wldev);
  3483. mutex_unlock(&wl->mutex);
  3484. b43dbg(wl, "Device suspended.\n");
  3485. return 0;
  3486. }
  3487. static int b43_resume(struct ssb_device *dev)
  3488. {
  3489. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3490. struct b43_wl *wl = wldev->wl;
  3491. int err = 0;
  3492. b43dbg(wl, "Resuming...\n");
  3493. mutex_lock(&wl->mutex);
  3494. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  3495. err = b43_wireless_core_init(wldev);
  3496. if (err) {
  3497. b43err(wl, "Resume failed at core init\n");
  3498. goto out;
  3499. }
  3500. }
  3501. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  3502. err = b43_wireless_core_start(wldev);
  3503. if (err) {
  3504. b43_wireless_core_exit(wldev);
  3505. b43err(wl, "Resume failed at core start\n");
  3506. goto out;
  3507. }
  3508. }
  3509. mutex_unlock(&wl->mutex);
  3510. b43dbg(wl, "Device resumed.\n");
  3511. out:
  3512. return err;
  3513. }
  3514. #else /* CONFIG_PM */
  3515. # define b43_suspend NULL
  3516. # define b43_resume NULL
  3517. #endif /* CONFIG_PM */
  3518. static struct ssb_driver b43_ssb_driver = {
  3519. .name = KBUILD_MODNAME,
  3520. .id_table = b43_ssb_tbl,
  3521. .probe = b43_probe,
  3522. .remove = b43_remove,
  3523. .suspend = b43_suspend,
  3524. .resume = b43_resume,
  3525. };
  3526. static int __init b43_init(void)
  3527. {
  3528. int err;
  3529. b43_debugfs_init();
  3530. err = b43_pcmcia_init();
  3531. if (err)
  3532. goto err_dfs_exit;
  3533. err = ssb_driver_register(&b43_ssb_driver);
  3534. if (err)
  3535. goto err_pcmcia_exit;
  3536. return err;
  3537. err_pcmcia_exit:
  3538. b43_pcmcia_exit();
  3539. err_dfs_exit:
  3540. b43_debugfs_exit();
  3541. return err;
  3542. }
  3543. static void __exit b43_exit(void)
  3544. {
  3545. ssb_driver_unregister(&b43_ssb_driver);
  3546. b43_pcmcia_exit();
  3547. b43_debugfs_exit();
  3548. }
  3549. module_init(b43_init)
  3550. module_exit(b43_exit)