main.c 102 KB

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