main.c 103 KB

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