main.c 103 KB

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