main.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018
  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-2009 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/firmware.h>
  29. #include <linux/wireless.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/io.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_common.h"
  39. #include "phy_g.h"
  40. #include "phy_n.h"
  41. #include "dma.h"
  42. #include "pio.h"
  43. #include "sysfs.h"
  44. #include "xmit.h"
  45. #include "lo.h"
  46. #include "pcmcia.h"
  47. MODULE_DESCRIPTION("Broadcom B43 wireless driver");
  48. MODULE_AUTHOR("Martin Langer");
  49. MODULE_AUTHOR("Stefano Brivio");
  50. MODULE_AUTHOR("Michael Buesch");
  51. MODULE_LICENSE("GPL");
  52. MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
  53. static int modparam_bad_frames_preempt;
  54. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  55. MODULE_PARM_DESC(bad_frames_preempt,
  56. "enable(1) / disable(0) Bad Frames Preemption");
  57. static char modparam_fwpostfix[16];
  58. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  59. MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
  60. static int modparam_hwpctl;
  61. module_param_named(hwpctl, modparam_hwpctl, int, 0444);
  62. MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
  63. static int modparam_nohwcrypt;
  64. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  65. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  66. static int modparam_qos = 1;
  67. module_param_named(qos, modparam_qos, int, 0444);
  68. MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
  69. static int modparam_btcoex = 1;
  70. module_param_named(btcoex, modparam_btcoex, int, 0444);
  71. MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
  72. int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
  73. module_param_named(verbose, b43_modparam_verbose, int, 0644);
  74. MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
  75. static const struct ssb_device_id b43_ssb_tbl[] = {
  76. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
  77. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
  78. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
  79. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
  80. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
  81. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
  82. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
  83. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
  84. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
  85. SSB_DEVTABLE_END
  86. };
  87. MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
  88. /* Channel and ratetables are shared for all devices.
  89. * They can't be const, because ieee80211 puts some precalculated
  90. * data in there. This data is the same for all devices, so we don't
  91. * get concurrency issues */
  92. #define RATETAB_ENT(_rateid, _flags) \
  93. { \
  94. .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
  95. .hw_value = (_rateid), \
  96. .flags = (_flags), \
  97. }
  98. /*
  99. * NOTE: When changing this, sync with xmit.c's
  100. * b43_plcp_get_bitrate_idx_* functions!
  101. */
  102. static struct ieee80211_rate __b43_ratetable[] = {
  103. RATETAB_ENT(B43_CCK_RATE_1MB, 0),
  104. RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
  105. RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
  106. RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
  107. RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
  108. RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
  109. RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
  110. RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
  111. RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
  112. RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
  113. RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
  114. RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
  115. };
  116. #define b43_a_ratetable (__b43_ratetable + 4)
  117. #define b43_a_ratetable_size 8
  118. #define b43_b_ratetable (__b43_ratetable + 0)
  119. #define b43_b_ratetable_size 4
  120. #define b43_g_ratetable (__b43_ratetable + 0)
  121. #define b43_g_ratetable_size 12
  122. #define CHAN4G(_channel, _freq, _flags) { \
  123. .band = IEEE80211_BAND_2GHZ, \
  124. .center_freq = (_freq), \
  125. .hw_value = (_channel), \
  126. .flags = (_flags), \
  127. .max_antenna_gain = 0, \
  128. .max_power = 30, \
  129. }
  130. static struct ieee80211_channel b43_2ghz_chantable[] = {
  131. CHAN4G(1, 2412, 0),
  132. CHAN4G(2, 2417, 0),
  133. CHAN4G(3, 2422, 0),
  134. CHAN4G(4, 2427, 0),
  135. CHAN4G(5, 2432, 0),
  136. CHAN4G(6, 2437, 0),
  137. CHAN4G(7, 2442, 0),
  138. CHAN4G(8, 2447, 0),
  139. CHAN4G(9, 2452, 0),
  140. CHAN4G(10, 2457, 0),
  141. CHAN4G(11, 2462, 0),
  142. CHAN4G(12, 2467, 0),
  143. CHAN4G(13, 2472, 0),
  144. CHAN4G(14, 2484, 0),
  145. };
  146. #undef CHAN4G
  147. #define CHAN5G(_channel, _flags) { \
  148. .band = IEEE80211_BAND_5GHZ, \
  149. .center_freq = 5000 + (5 * (_channel)), \
  150. .hw_value = (_channel), \
  151. .flags = (_flags), \
  152. .max_antenna_gain = 0, \
  153. .max_power = 30, \
  154. }
  155. static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
  156. CHAN5G(32, 0), CHAN5G(34, 0),
  157. CHAN5G(36, 0), CHAN5G(38, 0),
  158. CHAN5G(40, 0), CHAN5G(42, 0),
  159. CHAN5G(44, 0), CHAN5G(46, 0),
  160. CHAN5G(48, 0), CHAN5G(50, 0),
  161. CHAN5G(52, 0), CHAN5G(54, 0),
  162. CHAN5G(56, 0), CHAN5G(58, 0),
  163. CHAN5G(60, 0), CHAN5G(62, 0),
  164. CHAN5G(64, 0), CHAN5G(66, 0),
  165. CHAN5G(68, 0), CHAN5G(70, 0),
  166. CHAN5G(72, 0), CHAN5G(74, 0),
  167. CHAN5G(76, 0), CHAN5G(78, 0),
  168. CHAN5G(80, 0), CHAN5G(82, 0),
  169. CHAN5G(84, 0), CHAN5G(86, 0),
  170. CHAN5G(88, 0), CHAN5G(90, 0),
  171. CHAN5G(92, 0), CHAN5G(94, 0),
  172. CHAN5G(96, 0), CHAN5G(98, 0),
  173. CHAN5G(100, 0), CHAN5G(102, 0),
  174. CHAN5G(104, 0), CHAN5G(106, 0),
  175. CHAN5G(108, 0), CHAN5G(110, 0),
  176. CHAN5G(112, 0), CHAN5G(114, 0),
  177. CHAN5G(116, 0), CHAN5G(118, 0),
  178. CHAN5G(120, 0), CHAN5G(122, 0),
  179. CHAN5G(124, 0), CHAN5G(126, 0),
  180. CHAN5G(128, 0), CHAN5G(130, 0),
  181. CHAN5G(132, 0), CHAN5G(134, 0),
  182. CHAN5G(136, 0), CHAN5G(138, 0),
  183. CHAN5G(140, 0), CHAN5G(142, 0),
  184. CHAN5G(144, 0), CHAN5G(145, 0),
  185. CHAN5G(146, 0), CHAN5G(147, 0),
  186. CHAN5G(148, 0), CHAN5G(149, 0),
  187. CHAN5G(150, 0), CHAN5G(151, 0),
  188. CHAN5G(152, 0), CHAN5G(153, 0),
  189. CHAN5G(154, 0), CHAN5G(155, 0),
  190. CHAN5G(156, 0), CHAN5G(157, 0),
  191. CHAN5G(158, 0), CHAN5G(159, 0),
  192. CHAN5G(160, 0), CHAN5G(161, 0),
  193. CHAN5G(162, 0), CHAN5G(163, 0),
  194. CHAN5G(164, 0), CHAN5G(165, 0),
  195. CHAN5G(166, 0), CHAN5G(168, 0),
  196. CHAN5G(170, 0), CHAN5G(172, 0),
  197. CHAN5G(174, 0), CHAN5G(176, 0),
  198. CHAN5G(178, 0), CHAN5G(180, 0),
  199. CHAN5G(182, 0), CHAN5G(184, 0),
  200. CHAN5G(186, 0), CHAN5G(188, 0),
  201. CHAN5G(190, 0), CHAN5G(192, 0),
  202. CHAN5G(194, 0), CHAN5G(196, 0),
  203. CHAN5G(198, 0), CHAN5G(200, 0),
  204. CHAN5G(202, 0), CHAN5G(204, 0),
  205. CHAN5G(206, 0), CHAN5G(208, 0),
  206. CHAN5G(210, 0), CHAN5G(212, 0),
  207. CHAN5G(214, 0), CHAN5G(216, 0),
  208. CHAN5G(218, 0), CHAN5G(220, 0),
  209. CHAN5G(222, 0), CHAN5G(224, 0),
  210. CHAN5G(226, 0), CHAN5G(228, 0),
  211. };
  212. static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
  213. CHAN5G(34, 0), CHAN5G(36, 0),
  214. CHAN5G(38, 0), CHAN5G(40, 0),
  215. CHAN5G(42, 0), CHAN5G(44, 0),
  216. CHAN5G(46, 0), CHAN5G(48, 0),
  217. CHAN5G(52, 0), CHAN5G(56, 0),
  218. CHAN5G(60, 0), CHAN5G(64, 0),
  219. CHAN5G(100, 0), CHAN5G(104, 0),
  220. CHAN5G(108, 0), CHAN5G(112, 0),
  221. CHAN5G(116, 0), CHAN5G(120, 0),
  222. CHAN5G(124, 0), CHAN5G(128, 0),
  223. CHAN5G(132, 0), CHAN5G(136, 0),
  224. CHAN5G(140, 0), CHAN5G(149, 0),
  225. CHAN5G(153, 0), CHAN5G(157, 0),
  226. CHAN5G(161, 0), CHAN5G(165, 0),
  227. CHAN5G(184, 0), CHAN5G(188, 0),
  228. CHAN5G(192, 0), CHAN5G(196, 0),
  229. CHAN5G(200, 0), CHAN5G(204, 0),
  230. CHAN5G(208, 0), CHAN5G(212, 0),
  231. CHAN5G(216, 0),
  232. };
  233. #undef CHAN5G
  234. static struct ieee80211_supported_band b43_band_5GHz_nphy = {
  235. .band = IEEE80211_BAND_5GHZ,
  236. .channels = b43_5ghz_nphy_chantable,
  237. .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
  238. .bitrates = b43_a_ratetable,
  239. .n_bitrates = b43_a_ratetable_size,
  240. };
  241. static struct ieee80211_supported_band b43_band_5GHz_aphy = {
  242. .band = IEEE80211_BAND_5GHZ,
  243. .channels = b43_5ghz_aphy_chantable,
  244. .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
  245. .bitrates = b43_a_ratetable,
  246. .n_bitrates = b43_a_ratetable_size,
  247. };
  248. static struct ieee80211_supported_band b43_band_2GHz = {
  249. .band = IEEE80211_BAND_2GHZ,
  250. .channels = b43_2ghz_chantable,
  251. .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
  252. .bitrates = b43_g_ratetable,
  253. .n_bitrates = b43_g_ratetable_size,
  254. };
  255. static void b43_wireless_core_exit(struct b43_wldev *dev);
  256. static int b43_wireless_core_init(struct b43_wldev *dev);
  257. static void b43_wireless_core_stop(struct b43_wldev *dev);
  258. static int b43_wireless_core_start(struct b43_wldev *dev);
  259. static int b43_ratelimit(struct b43_wl *wl)
  260. {
  261. if (!wl || !wl->current_dev)
  262. return 1;
  263. if (b43_status(wl->current_dev) < B43_STAT_STARTED)
  264. return 1;
  265. /* We are up and running.
  266. * Ratelimit the messages to avoid DoS over the net. */
  267. return net_ratelimit();
  268. }
  269. void b43info(struct b43_wl *wl, const char *fmt, ...)
  270. {
  271. va_list args;
  272. if (b43_modparam_verbose < B43_VERBOSITY_INFO)
  273. return;
  274. if (!b43_ratelimit(wl))
  275. return;
  276. va_start(args, fmt);
  277. printk(KERN_INFO "b43-%s: ",
  278. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  279. vprintk(fmt, args);
  280. va_end(args);
  281. }
  282. void b43err(struct b43_wl *wl, const char *fmt, ...)
  283. {
  284. va_list args;
  285. if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
  286. return;
  287. if (!b43_ratelimit(wl))
  288. return;
  289. va_start(args, fmt);
  290. printk(KERN_ERR "b43-%s ERROR: ",
  291. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  292. vprintk(fmt, args);
  293. va_end(args);
  294. }
  295. void b43warn(struct b43_wl *wl, const char *fmt, ...)
  296. {
  297. va_list args;
  298. if (b43_modparam_verbose < B43_VERBOSITY_WARN)
  299. return;
  300. if (!b43_ratelimit(wl))
  301. return;
  302. va_start(args, fmt);
  303. printk(KERN_WARNING "b43-%s warning: ",
  304. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  305. vprintk(fmt, args);
  306. va_end(args);
  307. }
  308. void b43dbg(struct b43_wl *wl, const char *fmt, ...)
  309. {
  310. va_list args;
  311. if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
  312. return;
  313. va_start(args, fmt);
  314. printk(KERN_DEBUG "b43-%s debug: ",
  315. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  316. vprintk(fmt, args);
  317. va_end(args);
  318. }
  319. static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
  320. {
  321. u32 macctl;
  322. B43_WARN_ON(offset % 4 != 0);
  323. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  324. if (macctl & B43_MACCTL_BE)
  325. val = swab32(val);
  326. b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
  327. mmiowb();
  328. b43_write32(dev, B43_MMIO_RAM_DATA, val);
  329. }
  330. static inline void b43_shm_control_word(struct b43_wldev *dev,
  331. u16 routing, u16 offset)
  332. {
  333. u32 control;
  334. /* "offset" is the WORD offset. */
  335. control = routing;
  336. control <<= 16;
  337. control |= offset;
  338. b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
  339. }
  340. u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  341. {
  342. u32 ret;
  343. if (routing == B43_SHM_SHARED) {
  344. B43_WARN_ON(offset & 0x0001);
  345. if (offset & 0x0003) {
  346. /* Unaligned access */
  347. b43_shm_control_word(dev, routing, offset >> 2);
  348. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  349. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  350. ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
  351. goto out;
  352. }
  353. offset >>= 2;
  354. }
  355. b43_shm_control_word(dev, routing, offset);
  356. ret = b43_read32(dev, B43_MMIO_SHM_DATA);
  357. out:
  358. return ret;
  359. }
  360. u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  361. {
  362. struct b43_wl *wl = dev->wl;
  363. unsigned long flags;
  364. u32 ret;
  365. spin_lock_irqsave(&wl->shm_lock, flags);
  366. ret = __b43_shm_read32(dev, routing, offset);
  367. spin_unlock_irqrestore(&wl->shm_lock, flags);
  368. return ret;
  369. }
  370. u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  371. {
  372. u16 ret;
  373. if (routing == B43_SHM_SHARED) {
  374. B43_WARN_ON(offset & 0x0001);
  375. if (offset & 0x0003) {
  376. /* Unaligned access */
  377. b43_shm_control_word(dev, routing, offset >> 2);
  378. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  379. goto out;
  380. }
  381. offset >>= 2;
  382. }
  383. b43_shm_control_word(dev, routing, offset);
  384. ret = b43_read16(dev, B43_MMIO_SHM_DATA);
  385. out:
  386. return ret;
  387. }
  388. u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  389. {
  390. struct b43_wl *wl = dev->wl;
  391. unsigned long flags;
  392. u16 ret;
  393. spin_lock_irqsave(&wl->shm_lock, flags);
  394. ret = __b43_shm_read16(dev, routing, offset);
  395. spin_unlock_irqrestore(&wl->shm_lock, flags);
  396. return ret;
  397. }
  398. void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  399. {
  400. if (routing == B43_SHM_SHARED) {
  401. B43_WARN_ON(offset & 0x0001);
  402. if (offset & 0x0003) {
  403. /* Unaligned access */
  404. b43_shm_control_word(dev, routing, offset >> 2);
  405. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
  406. value & 0xFFFF);
  407. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  408. b43_write16(dev, B43_MMIO_SHM_DATA,
  409. (value >> 16) & 0xFFFF);
  410. return;
  411. }
  412. offset >>= 2;
  413. }
  414. b43_shm_control_word(dev, routing, offset);
  415. b43_write32(dev, B43_MMIO_SHM_DATA, value);
  416. }
  417. void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  418. {
  419. struct b43_wl *wl = dev->wl;
  420. unsigned long flags;
  421. spin_lock_irqsave(&wl->shm_lock, flags);
  422. __b43_shm_write32(dev, routing, offset, value);
  423. spin_unlock_irqrestore(&wl->shm_lock, flags);
  424. }
  425. void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  426. {
  427. if (routing == B43_SHM_SHARED) {
  428. B43_WARN_ON(offset & 0x0001);
  429. if (offset & 0x0003) {
  430. /* Unaligned access */
  431. b43_shm_control_word(dev, routing, offset >> 2);
  432. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
  433. return;
  434. }
  435. offset >>= 2;
  436. }
  437. b43_shm_control_word(dev, routing, offset);
  438. b43_write16(dev, B43_MMIO_SHM_DATA, value);
  439. }
  440. void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  441. {
  442. struct b43_wl *wl = dev->wl;
  443. unsigned long flags;
  444. spin_lock_irqsave(&wl->shm_lock, flags);
  445. __b43_shm_write16(dev, routing, offset, value);
  446. spin_unlock_irqrestore(&wl->shm_lock, flags);
  447. }
  448. /* Read HostFlags */
  449. u64 b43_hf_read(struct b43_wldev *dev)
  450. {
  451. u64 ret;
  452. ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
  453. ret <<= 16;
  454. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI);
  455. ret <<= 16;
  456. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
  457. return ret;
  458. }
  459. /* Write HostFlags */
  460. void b43_hf_write(struct b43_wldev *dev, u64 value)
  461. {
  462. u16 lo, mi, hi;
  463. lo = (value & 0x00000000FFFFULL);
  464. mi = (value & 0x0000FFFF0000ULL) >> 16;
  465. hi = (value & 0xFFFF00000000ULL) >> 32;
  466. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO, lo);
  467. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI, mi);
  468. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
  469. }
  470. /* Read the firmware capabilities bitmask (Opensource firmware only) */
  471. static u16 b43_fwcapa_read(struct b43_wldev *dev)
  472. {
  473. B43_WARN_ON(!dev->fw.opensource);
  474. return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
  475. }
  476. void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
  477. {
  478. u32 low, high;
  479. B43_WARN_ON(dev->dev->id.revision < 3);
  480. /* The hardware guarantees us an atomic read, if we
  481. * read the low register first. */
  482. low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
  483. high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  484. *tsf = high;
  485. *tsf <<= 32;
  486. *tsf |= low;
  487. }
  488. static void b43_time_lock(struct b43_wldev *dev)
  489. {
  490. u32 macctl;
  491. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  492. macctl |= B43_MACCTL_TBTTHOLD;
  493. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  494. /* Commit the write */
  495. b43_read32(dev, B43_MMIO_MACCTL);
  496. }
  497. static void b43_time_unlock(struct b43_wldev *dev)
  498. {
  499. u32 macctl;
  500. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  501. macctl &= ~B43_MACCTL_TBTTHOLD;
  502. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  503. /* Commit the write */
  504. b43_read32(dev, B43_MMIO_MACCTL);
  505. }
  506. static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
  507. {
  508. u32 low, high;
  509. B43_WARN_ON(dev->dev->id.revision < 3);
  510. low = tsf;
  511. high = (tsf >> 32);
  512. /* The hardware guarantees us an atomic write, if we
  513. * write the low register first. */
  514. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
  515. mmiowb();
  516. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
  517. mmiowb();
  518. }
  519. void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
  520. {
  521. b43_time_lock(dev);
  522. b43_tsf_write_locked(dev, tsf);
  523. b43_time_unlock(dev);
  524. }
  525. static
  526. void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
  527. {
  528. static const u8 zero_addr[ETH_ALEN] = { 0 };
  529. u16 data;
  530. if (!mac)
  531. mac = zero_addr;
  532. offset |= 0x0020;
  533. b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
  534. data = mac[0];
  535. data |= mac[1] << 8;
  536. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  537. data = mac[2];
  538. data |= mac[3] << 8;
  539. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  540. data = mac[4];
  541. data |= mac[5] << 8;
  542. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  543. }
  544. static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
  545. {
  546. const u8 *mac;
  547. const u8 *bssid;
  548. u8 mac_bssid[ETH_ALEN * 2];
  549. int i;
  550. u32 tmp;
  551. bssid = dev->wl->bssid;
  552. mac = dev->wl->mac_addr;
  553. b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
  554. memcpy(mac_bssid, mac, ETH_ALEN);
  555. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  556. /* Write our MAC address and BSSID to template ram */
  557. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  558. tmp = (u32) (mac_bssid[i + 0]);
  559. tmp |= (u32) (mac_bssid[i + 1]) << 8;
  560. tmp |= (u32) (mac_bssid[i + 2]) << 16;
  561. tmp |= (u32) (mac_bssid[i + 3]) << 24;
  562. b43_ram_write(dev, 0x20 + i, tmp);
  563. }
  564. }
  565. static void b43_upload_card_macaddress(struct b43_wldev *dev)
  566. {
  567. b43_write_mac_bssid_templates(dev);
  568. b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
  569. }
  570. static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
  571. {
  572. /* slot_time is in usec. */
  573. if (dev->phy.type != B43_PHYTYPE_G)
  574. return;
  575. b43_write16(dev, 0x684, 510 + slot_time);
  576. b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
  577. }
  578. static void b43_short_slot_timing_enable(struct b43_wldev *dev)
  579. {
  580. b43_set_slot_time(dev, 9);
  581. }
  582. static void b43_short_slot_timing_disable(struct b43_wldev *dev)
  583. {
  584. b43_set_slot_time(dev, 20);
  585. }
  586. /* Synchronize IRQ top- and bottom-half.
  587. * IRQs must be masked before calling this.
  588. * This must not be called with the irq_lock held.
  589. */
  590. static void b43_synchronize_irq(struct b43_wldev *dev)
  591. {
  592. synchronize_irq(dev->dev->irq);
  593. tasklet_kill(&dev->isr_tasklet);
  594. }
  595. /* DummyTransmission function, as documented on
  596. * http://bcm-specs.sipsolutions.net/DummyTransmission
  597. */
  598. void b43_dummy_transmission(struct b43_wldev *dev)
  599. {
  600. struct b43_wl *wl = dev->wl;
  601. struct b43_phy *phy = &dev->phy;
  602. unsigned int i, max_loop;
  603. u16 value;
  604. u32 buffer[5] = {
  605. 0x00000000,
  606. 0x00D40000,
  607. 0x00000000,
  608. 0x01000000,
  609. 0x00000000,
  610. };
  611. switch (phy->type) {
  612. case B43_PHYTYPE_A:
  613. max_loop = 0x1E;
  614. buffer[0] = 0x000201CC;
  615. break;
  616. case B43_PHYTYPE_B:
  617. case B43_PHYTYPE_G:
  618. max_loop = 0xFA;
  619. buffer[0] = 0x000B846E;
  620. break;
  621. default:
  622. B43_WARN_ON(1);
  623. return;
  624. }
  625. spin_lock_irq(&wl->irq_lock);
  626. write_lock(&wl->tx_lock);
  627. for (i = 0; i < 5; i++)
  628. b43_ram_write(dev, i * 4, buffer[i]);
  629. /* Commit writes */
  630. b43_read32(dev, B43_MMIO_MACCTL);
  631. b43_write16(dev, 0x0568, 0x0000);
  632. b43_write16(dev, 0x07C0, 0x0000);
  633. value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
  634. b43_write16(dev, 0x050C, value);
  635. b43_write16(dev, 0x0508, 0x0000);
  636. b43_write16(dev, 0x050A, 0x0000);
  637. b43_write16(dev, 0x054C, 0x0000);
  638. b43_write16(dev, 0x056A, 0x0014);
  639. b43_write16(dev, 0x0568, 0x0826);
  640. b43_write16(dev, 0x0500, 0x0000);
  641. b43_write16(dev, 0x0502, 0x0030);
  642. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  643. b43_radio_write16(dev, 0x0051, 0x0017);
  644. for (i = 0x00; i < max_loop; i++) {
  645. value = b43_read16(dev, 0x050E);
  646. if (value & 0x0080)
  647. break;
  648. udelay(10);
  649. }
  650. for (i = 0x00; i < 0x0A; i++) {
  651. value = b43_read16(dev, 0x050E);
  652. if (value & 0x0400)
  653. break;
  654. udelay(10);
  655. }
  656. for (i = 0x00; i < 0x19; i++) {
  657. value = b43_read16(dev, 0x0690);
  658. if (!(value & 0x0100))
  659. break;
  660. udelay(10);
  661. }
  662. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  663. b43_radio_write16(dev, 0x0051, 0x0037);
  664. write_unlock(&wl->tx_lock);
  665. spin_unlock_irq(&wl->irq_lock);
  666. }
  667. static void key_write(struct b43_wldev *dev,
  668. u8 index, u8 algorithm, const u8 *key)
  669. {
  670. unsigned int i;
  671. u32 offset;
  672. u16 value;
  673. u16 kidx;
  674. /* Key index/algo block */
  675. kidx = b43_kidx_to_fw(dev, index);
  676. value = ((kidx << 4) | algorithm);
  677. b43_shm_write16(dev, B43_SHM_SHARED,
  678. B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
  679. /* Write the key to the Key Table Pointer offset */
  680. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  681. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  682. value = key[i];
  683. value |= (u16) (key[i + 1]) << 8;
  684. b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
  685. }
  686. }
  687. static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
  688. {
  689. u32 addrtmp[2] = { 0, 0, };
  690. u8 per_sta_keys_start = 8;
  691. if (b43_new_kidx_api(dev))
  692. per_sta_keys_start = 4;
  693. B43_WARN_ON(index < per_sta_keys_start);
  694. /* We have two default TX keys and possibly two default RX keys.
  695. * Physical mac 0 is mapped to physical key 4 or 8, depending
  696. * on the firmware version.
  697. * So we must adjust the index here.
  698. */
  699. index -= per_sta_keys_start;
  700. if (addr) {
  701. addrtmp[0] = addr[0];
  702. addrtmp[0] |= ((u32) (addr[1]) << 8);
  703. addrtmp[0] |= ((u32) (addr[2]) << 16);
  704. addrtmp[0] |= ((u32) (addr[3]) << 24);
  705. addrtmp[1] = addr[4];
  706. addrtmp[1] |= ((u32) (addr[5]) << 8);
  707. }
  708. if (dev->dev->id.revision >= 5) {
  709. /* Receive match transmitter address mechanism */
  710. b43_shm_write32(dev, B43_SHM_RCMTA,
  711. (index * 2) + 0, addrtmp[0]);
  712. b43_shm_write16(dev, B43_SHM_RCMTA,
  713. (index * 2) + 1, addrtmp[1]);
  714. } else {
  715. /* RXE (Receive Engine) and
  716. * PSM (Programmable State Machine) mechanism
  717. */
  718. if (index < 8) {
  719. /* TODO write to RCM 16, 19, 22 and 25 */
  720. } else {
  721. b43_shm_write32(dev, B43_SHM_SHARED,
  722. B43_SHM_SH_PSM + (index * 6) + 0,
  723. addrtmp[0]);
  724. b43_shm_write16(dev, B43_SHM_SHARED,
  725. B43_SHM_SH_PSM + (index * 6) + 4,
  726. addrtmp[1]);
  727. }
  728. }
  729. }
  730. static void do_key_write(struct b43_wldev *dev,
  731. u8 index, u8 algorithm,
  732. const u8 *key, size_t key_len, const u8 *mac_addr)
  733. {
  734. u8 buf[B43_SEC_KEYSIZE] = { 0, };
  735. u8 per_sta_keys_start = 8;
  736. if (b43_new_kidx_api(dev))
  737. per_sta_keys_start = 4;
  738. B43_WARN_ON(index >= dev->max_nr_keys);
  739. B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
  740. if (index >= per_sta_keys_start)
  741. keymac_write(dev, index, NULL); /* First zero out mac. */
  742. if (key)
  743. memcpy(buf, key, key_len);
  744. key_write(dev, index, algorithm, buf);
  745. if (index >= per_sta_keys_start)
  746. keymac_write(dev, index, mac_addr);
  747. dev->key[index].algorithm = algorithm;
  748. }
  749. static int b43_key_write(struct b43_wldev *dev,
  750. int index, u8 algorithm,
  751. const u8 *key, size_t key_len,
  752. const u8 *mac_addr,
  753. struct ieee80211_key_conf *keyconf)
  754. {
  755. int i;
  756. int sta_keys_start;
  757. if (key_len > B43_SEC_KEYSIZE)
  758. return -EINVAL;
  759. for (i = 0; i < dev->max_nr_keys; i++) {
  760. /* Check that we don't already have this key. */
  761. B43_WARN_ON(dev->key[i].keyconf == keyconf);
  762. }
  763. if (index < 0) {
  764. /* Pairwise key. Get an empty slot for the key. */
  765. if (b43_new_kidx_api(dev))
  766. sta_keys_start = 4;
  767. else
  768. sta_keys_start = 8;
  769. for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
  770. if (!dev->key[i].keyconf) {
  771. /* found empty */
  772. index = i;
  773. break;
  774. }
  775. }
  776. if (index < 0) {
  777. b43warn(dev->wl, "Out of hardware key memory\n");
  778. return -ENOSPC;
  779. }
  780. } else
  781. B43_WARN_ON(index > 3);
  782. do_key_write(dev, index, algorithm, key, key_len, mac_addr);
  783. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  784. /* Default RX key */
  785. B43_WARN_ON(mac_addr);
  786. do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
  787. }
  788. keyconf->hw_key_idx = index;
  789. dev->key[index].keyconf = keyconf;
  790. return 0;
  791. }
  792. static int b43_key_clear(struct b43_wldev *dev, int index)
  793. {
  794. if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
  795. return -EINVAL;
  796. do_key_write(dev, index, B43_SEC_ALGO_NONE,
  797. NULL, B43_SEC_KEYSIZE, NULL);
  798. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  799. do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
  800. NULL, B43_SEC_KEYSIZE, NULL);
  801. }
  802. dev->key[index].keyconf = NULL;
  803. return 0;
  804. }
  805. static void b43_clear_keys(struct b43_wldev *dev)
  806. {
  807. int i;
  808. for (i = 0; i < dev->max_nr_keys; i++)
  809. b43_key_clear(dev, i);
  810. }
  811. static void b43_dump_keymemory(struct b43_wldev *dev)
  812. {
  813. unsigned int i, index, offset;
  814. u8 mac[ETH_ALEN];
  815. u16 algo;
  816. u32 rcmta0;
  817. u16 rcmta1;
  818. u64 hf;
  819. struct b43_key *key;
  820. if (!b43_debug(dev, B43_DBG_KEYS))
  821. return;
  822. hf = b43_hf_read(dev);
  823. b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
  824. !!(hf & B43_HF_USEDEFKEYS));
  825. for (index = 0; index < dev->max_nr_keys; index++) {
  826. key = &(dev->key[index]);
  827. printk(KERN_DEBUG "Key slot %02u: %s",
  828. index, (key->keyconf == NULL) ? " " : "*");
  829. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  830. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  831. u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
  832. printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
  833. }
  834. algo = b43_shm_read16(dev, B43_SHM_SHARED,
  835. B43_SHM_SH_KEYIDXBLOCK + (index * 2));
  836. printk(" Algo: %04X/%02X", algo, key->algorithm);
  837. if (index >= 4) {
  838. rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
  839. ((index - 4) * 2) + 0);
  840. rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
  841. ((index - 4) * 2) + 1);
  842. *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
  843. *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
  844. printk(" MAC: %pM", mac);
  845. } else
  846. printk(" DEFAULT KEY");
  847. printk("\n");
  848. }
  849. }
  850. void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
  851. {
  852. u32 macctl;
  853. u16 ucstat;
  854. bool hwps;
  855. bool awake;
  856. int i;
  857. B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
  858. (ps_flags & B43_PS_DISABLED));
  859. B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
  860. if (ps_flags & B43_PS_ENABLED) {
  861. hwps = 1;
  862. } else if (ps_flags & B43_PS_DISABLED) {
  863. hwps = 0;
  864. } else {
  865. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  866. // and thus is not an AP and we are associated, set bit 25
  867. }
  868. if (ps_flags & B43_PS_AWAKE) {
  869. awake = 1;
  870. } else if (ps_flags & B43_PS_ASLEEP) {
  871. awake = 0;
  872. } else {
  873. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  874. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  875. // successful, set bit26
  876. }
  877. /* FIXME: For now we force awake-on and hwps-off */
  878. hwps = 0;
  879. awake = 1;
  880. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  881. if (hwps)
  882. macctl |= B43_MACCTL_HWPS;
  883. else
  884. macctl &= ~B43_MACCTL_HWPS;
  885. if (awake)
  886. macctl |= B43_MACCTL_AWAKE;
  887. else
  888. macctl &= ~B43_MACCTL_AWAKE;
  889. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  890. /* Commit write */
  891. b43_read32(dev, B43_MMIO_MACCTL);
  892. if (awake && dev->dev->id.revision >= 5) {
  893. /* Wait for the microcode to wake up. */
  894. for (i = 0; i < 100; i++) {
  895. ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
  896. B43_SHM_SH_UCODESTAT);
  897. if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
  898. break;
  899. udelay(10);
  900. }
  901. }
  902. }
  903. void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
  904. {
  905. u32 tmslow;
  906. u32 macctl;
  907. flags |= B43_TMSLOW_PHYCLKEN;
  908. flags |= B43_TMSLOW_PHYRESET;
  909. ssb_device_enable(dev->dev, flags);
  910. msleep(2); /* Wait for the PLL to turn on. */
  911. /* Now take the PHY out of Reset again */
  912. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  913. tmslow |= SSB_TMSLOW_FGC;
  914. tmslow &= ~B43_TMSLOW_PHYRESET;
  915. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  916. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  917. msleep(1);
  918. tmslow &= ~SSB_TMSLOW_FGC;
  919. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  920. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  921. msleep(1);
  922. /* Turn Analog ON, but only if we already know the PHY-type.
  923. * This protects against very early setup where we don't know the
  924. * PHY-type, yet. wireless_core_reset will be called once again later,
  925. * when we know the PHY-type. */
  926. if (dev->phy.ops)
  927. dev->phy.ops->switch_analog(dev, 1);
  928. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  929. macctl &= ~B43_MACCTL_GMODE;
  930. if (flags & B43_TMSLOW_GMODE)
  931. macctl |= B43_MACCTL_GMODE;
  932. macctl |= B43_MACCTL_IHR_ENABLED;
  933. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  934. }
  935. static void handle_irq_transmit_status(struct b43_wldev *dev)
  936. {
  937. u32 v0, v1;
  938. u16 tmp;
  939. struct b43_txstatus stat;
  940. while (1) {
  941. v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  942. if (!(v0 & 0x00000001))
  943. break;
  944. v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  945. stat.cookie = (v0 >> 16);
  946. stat.seq = (v1 & 0x0000FFFF);
  947. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  948. tmp = (v0 & 0x0000FFFF);
  949. stat.frame_count = ((tmp & 0xF000) >> 12);
  950. stat.rts_count = ((tmp & 0x0F00) >> 8);
  951. stat.supp_reason = ((tmp & 0x001C) >> 2);
  952. stat.pm_indicated = !!(tmp & 0x0080);
  953. stat.intermediate = !!(tmp & 0x0040);
  954. stat.for_ampdu = !!(tmp & 0x0020);
  955. stat.acked = !!(tmp & 0x0002);
  956. b43_handle_txstatus(dev, &stat);
  957. }
  958. }
  959. static void drain_txstatus_queue(struct b43_wldev *dev)
  960. {
  961. u32 dummy;
  962. if (dev->dev->id.revision < 5)
  963. return;
  964. /* Read all entries from the microcode TXstatus FIFO
  965. * and throw them away.
  966. */
  967. while (1) {
  968. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  969. if (!(dummy & 0x00000001))
  970. break;
  971. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  972. }
  973. }
  974. static u32 b43_jssi_read(struct b43_wldev *dev)
  975. {
  976. u32 val = 0;
  977. val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
  978. val <<= 16;
  979. val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
  980. return val;
  981. }
  982. static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
  983. {
  984. b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
  985. b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
  986. }
  987. static void b43_generate_noise_sample(struct b43_wldev *dev)
  988. {
  989. b43_jssi_write(dev, 0x7F7F7F7F);
  990. b43_write32(dev, B43_MMIO_MACCMD,
  991. b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
  992. }
  993. static void b43_calculate_link_quality(struct b43_wldev *dev)
  994. {
  995. /* Top half of Link Quality calculation. */
  996. if (dev->phy.type != B43_PHYTYPE_G)
  997. return;
  998. if (dev->noisecalc.calculation_running)
  999. return;
  1000. dev->noisecalc.calculation_running = 1;
  1001. dev->noisecalc.nr_samples = 0;
  1002. b43_generate_noise_sample(dev);
  1003. }
  1004. static void handle_irq_noise(struct b43_wldev *dev)
  1005. {
  1006. struct b43_phy_g *phy = dev->phy.g;
  1007. u16 tmp;
  1008. u8 noise[4];
  1009. u8 i, j;
  1010. s32 average;
  1011. /* Bottom half of Link Quality calculation. */
  1012. if (dev->phy.type != B43_PHYTYPE_G)
  1013. return;
  1014. /* Possible race condition: It might be possible that the user
  1015. * changed to a different channel in the meantime since we
  1016. * started the calculation. We ignore that fact, since it's
  1017. * not really that much of a problem. The background noise is
  1018. * an estimation only anyway. Slightly wrong results will get damped
  1019. * by the averaging of the 8 sample rounds. Additionally the
  1020. * value is shortlived. So it will be replaced by the next noise
  1021. * calculation round soon. */
  1022. B43_WARN_ON(!dev->noisecalc.calculation_running);
  1023. *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
  1024. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  1025. noise[2] == 0x7F || noise[3] == 0x7F)
  1026. goto generate_new;
  1027. /* Get the noise samples. */
  1028. B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
  1029. i = dev->noisecalc.nr_samples;
  1030. noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1031. noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1032. noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1033. noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1034. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  1035. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  1036. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  1037. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  1038. dev->noisecalc.nr_samples++;
  1039. if (dev->noisecalc.nr_samples == 8) {
  1040. /* Calculate the Link Quality by the noise samples. */
  1041. average = 0;
  1042. for (i = 0; i < 8; i++) {
  1043. for (j = 0; j < 4; j++)
  1044. average += dev->noisecalc.samples[i][j];
  1045. }
  1046. average /= (8 * 4);
  1047. average *= 125;
  1048. average += 64;
  1049. average /= 128;
  1050. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
  1051. tmp = (tmp / 128) & 0x1F;
  1052. if (tmp >= 8)
  1053. average += 2;
  1054. else
  1055. average -= 25;
  1056. if (tmp == 8)
  1057. average -= 72;
  1058. else
  1059. average -= 48;
  1060. dev->stats.link_noise = average;
  1061. dev->noisecalc.calculation_running = 0;
  1062. return;
  1063. }
  1064. generate_new:
  1065. b43_generate_noise_sample(dev);
  1066. }
  1067. static void handle_irq_tbtt_indication(struct b43_wldev *dev)
  1068. {
  1069. if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
  1070. ///TODO: PS TBTT
  1071. } else {
  1072. if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
  1073. b43_power_saving_ctl_bits(dev, 0);
  1074. }
  1075. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  1076. dev->dfq_valid = 1;
  1077. }
  1078. static void handle_irq_atim_end(struct b43_wldev *dev)
  1079. {
  1080. if (dev->dfq_valid) {
  1081. b43_write32(dev, B43_MMIO_MACCMD,
  1082. b43_read32(dev, B43_MMIO_MACCMD)
  1083. | B43_MACCMD_DFQ_VALID);
  1084. dev->dfq_valid = 0;
  1085. }
  1086. }
  1087. static void handle_irq_pmq(struct b43_wldev *dev)
  1088. {
  1089. u32 tmp;
  1090. //TODO: AP mode.
  1091. while (1) {
  1092. tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
  1093. if (!(tmp & 0x00000008))
  1094. break;
  1095. }
  1096. /* 16bit write is odd, but correct. */
  1097. b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
  1098. }
  1099. static void b43_write_template_common(struct b43_wldev *dev,
  1100. const u8 *data, u16 size,
  1101. u16 ram_offset,
  1102. u16 shm_size_offset, u8 rate)
  1103. {
  1104. u32 i, tmp;
  1105. struct b43_plcp_hdr4 plcp;
  1106. plcp.data = 0;
  1107. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  1108. b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  1109. ram_offset += sizeof(u32);
  1110. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  1111. * So leave the first two bytes of the next write blank.
  1112. */
  1113. tmp = (u32) (data[0]) << 16;
  1114. tmp |= (u32) (data[1]) << 24;
  1115. b43_ram_write(dev, ram_offset, tmp);
  1116. ram_offset += sizeof(u32);
  1117. for (i = 2; i < size; i += sizeof(u32)) {
  1118. tmp = (u32) (data[i + 0]);
  1119. if (i + 1 < size)
  1120. tmp |= (u32) (data[i + 1]) << 8;
  1121. if (i + 2 < size)
  1122. tmp |= (u32) (data[i + 2]) << 16;
  1123. if (i + 3 < size)
  1124. tmp |= (u32) (data[i + 3]) << 24;
  1125. b43_ram_write(dev, ram_offset + i - 2, tmp);
  1126. }
  1127. b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
  1128. size + sizeof(struct b43_plcp_hdr6));
  1129. }
  1130. /* Check if the use of the antenna that ieee80211 told us to
  1131. * use is possible. This will fall back to DEFAULT.
  1132. * "antenna_nr" is the antenna identifier we got from ieee80211. */
  1133. u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
  1134. u8 antenna_nr)
  1135. {
  1136. u8 antenna_mask;
  1137. if (antenna_nr == 0) {
  1138. /* Zero means "use default antenna". That's always OK. */
  1139. return 0;
  1140. }
  1141. /* Get the mask of available antennas. */
  1142. if (dev->phy.gmode)
  1143. antenna_mask = dev->dev->bus->sprom.ant_available_bg;
  1144. else
  1145. antenna_mask = dev->dev->bus->sprom.ant_available_a;
  1146. if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
  1147. /* This antenna is not available. Fall back to default. */
  1148. return 0;
  1149. }
  1150. return antenna_nr;
  1151. }
  1152. /* Convert a b43 antenna number value to the PHY TX control value. */
  1153. static u16 b43_antenna_to_phyctl(int antenna)
  1154. {
  1155. switch (antenna) {
  1156. case B43_ANTENNA0:
  1157. return B43_TXH_PHY_ANT0;
  1158. case B43_ANTENNA1:
  1159. return B43_TXH_PHY_ANT1;
  1160. case B43_ANTENNA2:
  1161. return B43_TXH_PHY_ANT2;
  1162. case B43_ANTENNA3:
  1163. return B43_TXH_PHY_ANT3;
  1164. case B43_ANTENNA_AUTO:
  1165. return B43_TXH_PHY_ANT01AUTO;
  1166. }
  1167. B43_WARN_ON(1);
  1168. return 0;
  1169. }
  1170. static void b43_write_beacon_template(struct b43_wldev *dev,
  1171. u16 ram_offset,
  1172. u16 shm_size_offset)
  1173. {
  1174. unsigned int i, len, variable_len;
  1175. const struct ieee80211_mgmt *bcn;
  1176. const u8 *ie;
  1177. bool tim_found = 0;
  1178. unsigned int rate;
  1179. u16 ctl;
  1180. int antenna;
  1181. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
  1182. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  1183. len = min((size_t) dev->wl->current_beacon->len,
  1184. 0x200 - sizeof(struct b43_plcp_hdr6));
  1185. rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
  1186. b43_write_template_common(dev, (const u8 *)bcn,
  1187. len, ram_offset, shm_size_offset, rate);
  1188. /* Write the PHY TX control parameters. */
  1189. antenna = B43_ANTENNA_DEFAULT;
  1190. antenna = b43_antenna_to_phyctl(antenna);
  1191. ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1192. /* We can't send beacons with short preamble. Would get PHY errors. */
  1193. ctl &= ~B43_TXH_PHY_SHORTPRMBL;
  1194. ctl &= ~B43_TXH_PHY_ANT;
  1195. ctl &= ~B43_TXH_PHY_ENC;
  1196. ctl |= antenna;
  1197. if (b43_is_cck_rate(rate))
  1198. ctl |= B43_TXH_PHY_ENC_CCK;
  1199. else
  1200. ctl |= B43_TXH_PHY_ENC_OFDM;
  1201. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  1202. /* Find the position of the TIM and the DTIM_period value
  1203. * and write them to SHM. */
  1204. ie = bcn->u.beacon.variable;
  1205. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1206. for (i = 0; i < variable_len - 2; ) {
  1207. uint8_t ie_id, ie_len;
  1208. ie_id = ie[i];
  1209. ie_len = ie[i + 1];
  1210. if (ie_id == 5) {
  1211. u16 tim_position;
  1212. u16 dtim_period;
  1213. /* This is the TIM Information Element */
  1214. /* Check whether the ie_len is in the beacon data range. */
  1215. if (variable_len < ie_len + 2 + i)
  1216. break;
  1217. /* A valid TIM is at least 4 bytes long. */
  1218. if (ie_len < 4)
  1219. break;
  1220. tim_found = 1;
  1221. tim_position = sizeof(struct b43_plcp_hdr6);
  1222. tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1223. tim_position += i;
  1224. dtim_period = ie[i + 3];
  1225. b43_shm_write16(dev, B43_SHM_SHARED,
  1226. B43_SHM_SH_TIMBPOS, tim_position);
  1227. b43_shm_write16(dev, B43_SHM_SHARED,
  1228. B43_SHM_SH_DTIMPER, dtim_period);
  1229. break;
  1230. }
  1231. i += ie_len + 2;
  1232. }
  1233. if (!tim_found) {
  1234. /*
  1235. * If ucode wants to modify TIM do it behind the beacon, this
  1236. * will happen, for example, when doing mesh networking.
  1237. */
  1238. b43_shm_write16(dev, B43_SHM_SHARED,
  1239. B43_SHM_SH_TIMBPOS,
  1240. len + sizeof(struct b43_plcp_hdr6));
  1241. b43_shm_write16(dev, B43_SHM_SHARED,
  1242. B43_SHM_SH_DTIMPER, 0);
  1243. }
  1244. b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
  1245. }
  1246. static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  1247. u16 shm_offset, u16 size,
  1248. struct ieee80211_rate *rate)
  1249. {
  1250. struct b43_plcp_hdr4 plcp;
  1251. u32 tmp;
  1252. __le16 dur;
  1253. plcp.data = 0;
  1254. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
  1255. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1256. dev->wl->vif, size,
  1257. rate);
  1258. /* Write PLCP in two parts and timing for packet transfer */
  1259. tmp = le32_to_cpu(plcp.data);
  1260. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
  1261. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
  1262. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
  1263. }
  1264. /* Instead of using custom probe response template, this function
  1265. * just patches custom beacon template by:
  1266. * 1) Changing packet type
  1267. * 2) Patching duration field
  1268. * 3) Stripping TIM
  1269. */
  1270. static const u8 *b43_generate_probe_resp(struct b43_wldev *dev,
  1271. u16 *dest_size,
  1272. struct ieee80211_rate *rate)
  1273. {
  1274. const u8 *src_data;
  1275. u8 *dest_data;
  1276. u16 src_size, elem_size, src_pos, dest_pos;
  1277. __le16 dur;
  1278. struct ieee80211_hdr *hdr;
  1279. size_t ie_start;
  1280. src_size = dev->wl->current_beacon->len;
  1281. src_data = (const u8 *)dev->wl->current_beacon->data;
  1282. /* Get the start offset of the variable IEs in the packet. */
  1283. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  1284. B43_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1285. if (B43_WARN_ON(src_size < ie_start))
  1286. return NULL;
  1287. dest_data = kmalloc(src_size, GFP_ATOMIC);
  1288. if (unlikely(!dest_data))
  1289. return NULL;
  1290. /* Copy the static data and all Information Elements, except the TIM. */
  1291. memcpy(dest_data, src_data, ie_start);
  1292. src_pos = ie_start;
  1293. dest_pos = ie_start;
  1294. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  1295. elem_size = src_data[src_pos + 1] + 2;
  1296. if (src_data[src_pos] == 5) {
  1297. /* This is the TIM. */
  1298. continue;
  1299. }
  1300. memcpy(dest_data + dest_pos, src_data + src_pos,
  1301. elem_size);
  1302. dest_pos += elem_size;
  1303. }
  1304. *dest_size = dest_pos;
  1305. hdr = (struct ieee80211_hdr *)dest_data;
  1306. /* Set the frame control. */
  1307. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1308. IEEE80211_STYPE_PROBE_RESP);
  1309. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1310. dev->wl->vif, *dest_size,
  1311. rate);
  1312. hdr->duration_id = dur;
  1313. return dest_data;
  1314. }
  1315. static void b43_write_probe_resp_template(struct b43_wldev *dev,
  1316. u16 ram_offset,
  1317. u16 shm_size_offset,
  1318. struct ieee80211_rate *rate)
  1319. {
  1320. const u8 *probe_resp_data;
  1321. u16 size;
  1322. size = dev->wl->current_beacon->len;
  1323. probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
  1324. if (unlikely(!probe_resp_data))
  1325. return;
  1326. /* Looks like PLCP headers plus packet timings are stored for
  1327. * all possible basic rates
  1328. */
  1329. b43_write_probe_resp_plcp(dev, 0x31A, size, &b43_b_ratetable[0]);
  1330. b43_write_probe_resp_plcp(dev, 0x32C, size, &b43_b_ratetable[1]);
  1331. b43_write_probe_resp_plcp(dev, 0x33E, size, &b43_b_ratetable[2]);
  1332. b43_write_probe_resp_plcp(dev, 0x350, size, &b43_b_ratetable[3]);
  1333. size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
  1334. b43_write_template_common(dev, probe_resp_data,
  1335. size, ram_offset, shm_size_offset,
  1336. rate->hw_value);
  1337. kfree(probe_resp_data);
  1338. }
  1339. static void b43_upload_beacon0(struct b43_wldev *dev)
  1340. {
  1341. struct b43_wl *wl = dev->wl;
  1342. if (wl->beacon0_uploaded)
  1343. return;
  1344. b43_write_beacon_template(dev, 0x68, 0x18);
  1345. /* FIXME: Probe resp upload doesn't really belong here,
  1346. * but we don't use that feature anyway. */
  1347. b43_write_probe_resp_template(dev, 0x268, 0x4A,
  1348. &__b43_ratetable[3]);
  1349. wl->beacon0_uploaded = 1;
  1350. }
  1351. static void b43_upload_beacon1(struct b43_wldev *dev)
  1352. {
  1353. struct b43_wl *wl = dev->wl;
  1354. if (wl->beacon1_uploaded)
  1355. return;
  1356. b43_write_beacon_template(dev, 0x468, 0x1A);
  1357. wl->beacon1_uploaded = 1;
  1358. }
  1359. static void handle_irq_beacon(struct b43_wldev *dev)
  1360. {
  1361. struct b43_wl *wl = dev->wl;
  1362. u32 cmd, beacon0_valid, beacon1_valid;
  1363. if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
  1364. !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  1365. return;
  1366. /* This is the bottom half of the asynchronous beacon update. */
  1367. /* Ignore interrupt in the future. */
  1368. dev->irq_mask &= ~B43_IRQ_BEACON;
  1369. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1370. beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
  1371. beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
  1372. /* Schedule interrupt manually, if busy. */
  1373. if (beacon0_valid && beacon1_valid) {
  1374. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
  1375. dev->irq_mask |= B43_IRQ_BEACON;
  1376. return;
  1377. }
  1378. if (unlikely(wl->beacon_templates_virgin)) {
  1379. /* We never uploaded a beacon before.
  1380. * Upload both templates now, but only mark one valid. */
  1381. wl->beacon_templates_virgin = 0;
  1382. b43_upload_beacon0(dev);
  1383. b43_upload_beacon1(dev);
  1384. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1385. cmd |= B43_MACCMD_BEACON0_VALID;
  1386. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1387. } else {
  1388. if (!beacon0_valid) {
  1389. b43_upload_beacon0(dev);
  1390. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1391. cmd |= B43_MACCMD_BEACON0_VALID;
  1392. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1393. } else if (!beacon1_valid) {
  1394. b43_upload_beacon1(dev);
  1395. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1396. cmd |= B43_MACCMD_BEACON1_VALID;
  1397. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1398. }
  1399. }
  1400. }
  1401. static void b43_beacon_update_trigger_work(struct work_struct *work)
  1402. {
  1403. struct b43_wl *wl = container_of(work, struct b43_wl,
  1404. beacon_update_trigger);
  1405. struct b43_wldev *dev;
  1406. mutex_lock(&wl->mutex);
  1407. dev = wl->current_dev;
  1408. if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
  1409. spin_lock_irq(&wl->irq_lock);
  1410. /* update beacon right away or defer to irq */
  1411. handle_irq_beacon(dev);
  1412. /* The handler might have updated the IRQ mask. */
  1413. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  1414. mmiowb();
  1415. spin_unlock_irq(&wl->irq_lock);
  1416. }
  1417. mutex_unlock(&wl->mutex);
  1418. }
  1419. /* Asynchronously update the packet templates in template RAM.
  1420. * Locking: Requires wl->irq_lock to be locked. */
  1421. static void b43_update_templates(struct b43_wl *wl)
  1422. {
  1423. struct sk_buff *beacon;
  1424. /* This is the top half of the ansynchronous beacon update.
  1425. * The bottom half is the beacon IRQ.
  1426. * Beacon update must be asynchronous to avoid sending an
  1427. * invalid beacon. This can happen for example, if the firmware
  1428. * transmits a beacon while we are updating it. */
  1429. /* We could modify the existing beacon and set the aid bit in
  1430. * the TIM field, but that would probably require resizing and
  1431. * moving of data within the beacon template.
  1432. * Simply request a new beacon and let mac80211 do the hard work. */
  1433. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1434. if (unlikely(!beacon))
  1435. return;
  1436. if (wl->current_beacon)
  1437. dev_kfree_skb_any(wl->current_beacon);
  1438. wl->current_beacon = beacon;
  1439. wl->beacon0_uploaded = 0;
  1440. wl->beacon1_uploaded = 0;
  1441. ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
  1442. }
  1443. static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
  1444. {
  1445. b43_time_lock(dev);
  1446. if (dev->dev->id.revision >= 3) {
  1447. b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
  1448. b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
  1449. } else {
  1450. b43_write16(dev, 0x606, (beacon_int >> 6));
  1451. b43_write16(dev, 0x610, beacon_int);
  1452. }
  1453. b43_time_unlock(dev);
  1454. b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
  1455. }
  1456. static void b43_handle_firmware_panic(struct b43_wldev *dev)
  1457. {
  1458. u16 reason;
  1459. /* Read the register that contains the reason code for the panic. */
  1460. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
  1461. b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
  1462. switch (reason) {
  1463. default:
  1464. b43dbg(dev->wl, "The panic reason is unknown.\n");
  1465. /* fallthrough */
  1466. case B43_FWPANIC_DIE:
  1467. /* Do not restart the controller or firmware.
  1468. * The device is nonfunctional from now on.
  1469. * Restarting would result in this panic to trigger again,
  1470. * so we avoid that recursion. */
  1471. break;
  1472. case B43_FWPANIC_RESTART:
  1473. b43_controller_restart(dev, "Microcode panic");
  1474. break;
  1475. }
  1476. }
  1477. static void handle_irq_ucode_debug(struct b43_wldev *dev)
  1478. {
  1479. unsigned int i, cnt;
  1480. u16 reason, marker_id, marker_line;
  1481. __le16 *buf;
  1482. /* The proprietary firmware doesn't have this IRQ. */
  1483. if (!dev->fw.opensource)
  1484. return;
  1485. /* Read the register that contains the reason code for this IRQ. */
  1486. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
  1487. switch (reason) {
  1488. case B43_DEBUGIRQ_PANIC:
  1489. b43_handle_firmware_panic(dev);
  1490. break;
  1491. case B43_DEBUGIRQ_DUMP_SHM:
  1492. if (!B43_DEBUG)
  1493. break; /* Only with driver debugging enabled. */
  1494. buf = kmalloc(4096, GFP_ATOMIC);
  1495. if (!buf) {
  1496. b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
  1497. goto out;
  1498. }
  1499. for (i = 0; i < 4096; i += 2) {
  1500. u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
  1501. buf[i / 2] = cpu_to_le16(tmp);
  1502. }
  1503. b43info(dev->wl, "Shared memory dump:\n");
  1504. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
  1505. 16, 2, buf, 4096, 1);
  1506. kfree(buf);
  1507. break;
  1508. case B43_DEBUGIRQ_DUMP_REGS:
  1509. if (!B43_DEBUG)
  1510. break; /* Only with driver debugging enabled. */
  1511. b43info(dev->wl, "Microcode register dump:\n");
  1512. for (i = 0, cnt = 0; i < 64; i++) {
  1513. u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
  1514. if (cnt == 0)
  1515. printk(KERN_INFO);
  1516. printk("r%02u: 0x%04X ", i, tmp);
  1517. cnt++;
  1518. if (cnt == 6) {
  1519. printk("\n");
  1520. cnt = 0;
  1521. }
  1522. }
  1523. printk("\n");
  1524. break;
  1525. case B43_DEBUGIRQ_MARKER:
  1526. if (!B43_DEBUG)
  1527. break; /* Only with driver debugging enabled. */
  1528. marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1529. B43_MARKER_ID_REG);
  1530. marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1531. B43_MARKER_LINE_REG);
  1532. b43info(dev->wl, "The firmware just executed the MARKER(%u) "
  1533. "at line number %u\n",
  1534. marker_id, marker_line);
  1535. break;
  1536. default:
  1537. b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
  1538. reason);
  1539. }
  1540. out:
  1541. /* Acknowledge the debug-IRQ, so the firmware can continue. */
  1542. b43_shm_write16(dev, B43_SHM_SCRATCH,
  1543. B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
  1544. }
  1545. /* Interrupt handler bottom-half */
  1546. static void b43_interrupt_tasklet(struct b43_wldev *dev)
  1547. {
  1548. u32 reason;
  1549. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1550. u32 merged_dma_reason = 0;
  1551. int i;
  1552. unsigned long flags;
  1553. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1554. B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
  1555. reason = dev->irq_reason;
  1556. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1557. dma_reason[i] = dev->dma_reason[i];
  1558. merged_dma_reason |= dma_reason[i];
  1559. }
  1560. if (unlikely(reason & B43_IRQ_MAC_TXERR))
  1561. b43err(dev->wl, "MAC transmission error\n");
  1562. if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
  1563. b43err(dev->wl, "PHY transmission error\n");
  1564. rmb();
  1565. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1566. atomic_set(&dev->phy.txerr_cnt,
  1567. B43_PHY_TX_BADNESS_LIMIT);
  1568. b43err(dev->wl, "Too many PHY TX errors, "
  1569. "restarting the controller\n");
  1570. b43_controller_restart(dev, "PHY TX errors");
  1571. }
  1572. }
  1573. if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
  1574. B43_DMAIRQ_NONFATALMASK))) {
  1575. if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
  1576. b43err(dev->wl, "Fatal DMA error: "
  1577. "0x%08X, 0x%08X, 0x%08X, "
  1578. "0x%08X, 0x%08X, 0x%08X\n",
  1579. dma_reason[0], dma_reason[1],
  1580. dma_reason[2], dma_reason[3],
  1581. dma_reason[4], dma_reason[5]);
  1582. b43_controller_restart(dev, "DMA error");
  1583. mmiowb();
  1584. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1585. return;
  1586. }
  1587. if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
  1588. b43err(dev->wl, "DMA error: "
  1589. "0x%08X, 0x%08X, 0x%08X, "
  1590. "0x%08X, 0x%08X, 0x%08X\n",
  1591. dma_reason[0], dma_reason[1],
  1592. dma_reason[2], dma_reason[3],
  1593. dma_reason[4], dma_reason[5]);
  1594. }
  1595. }
  1596. if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
  1597. handle_irq_ucode_debug(dev);
  1598. if (reason & B43_IRQ_TBTT_INDI)
  1599. handle_irq_tbtt_indication(dev);
  1600. if (reason & B43_IRQ_ATIM_END)
  1601. handle_irq_atim_end(dev);
  1602. if (reason & B43_IRQ_BEACON)
  1603. handle_irq_beacon(dev);
  1604. if (reason & B43_IRQ_PMQ)
  1605. handle_irq_pmq(dev);
  1606. if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
  1607. ;/* TODO */
  1608. if (reason & B43_IRQ_NOISESAMPLE_OK)
  1609. handle_irq_noise(dev);
  1610. /* Check the DMA reason registers for received data. */
  1611. if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
  1612. if (b43_using_pio_transfers(dev))
  1613. b43_pio_rx(dev->pio.rx_queue);
  1614. else
  1615. b43_dma_rx(dev->dma.rx_ring);
  1616. }
  1617. B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
  1618. B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
  1619. B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
  1620. B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
  1621. B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
  1622. if (reason & B43_IRQ_TX_OK)
  1623. handle_irq_transmit_status(dev);
  1624. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  1625. mmiowb();
  1626. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1627. }
  1628. static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
  1629. {
  1630. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
  1631. b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
  1632. b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
  1633. b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
  1634. b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
  1635. b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
  1636. /* Unused ring
  1637. b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
  1638. */
  1639. }
  1640. /* Interrupt handler top-half */
  1641. static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
  1642. {
  1643. irqreturn_t ret = IRQ_NONE;
  1644. struct b43_wldev *dev = dev_id;
  1645. u32 reason;
  1646. B43_WARN_ON(!dev);
  1647. spin_lock(&dev->wl->irq_lock);
  1648. if (unlikely(b43_status(dev) < B43_STAT_STARTED)) {
  1649. /* This can only happen on shared IRQ lines. */
  1650. goto out;
  1651. }
  1652. reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1653. if (reason == 0xffffffff) /* shared IRQ */
  1654. goto out;
  1655. ret = IRQ_HANDLED;
  1656. reason &= dev->irq_mask;
  1657. if (!reason)
  1658. goto out;
  1659. dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
  1660. & 0x0001DC00;
  1661. dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
  1662. & 0x0000DC00;
  1663. dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
  1664. & 0x0000DC00;
  1665. dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
  1666. & 0x0001DC00;
  1667. dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
  1668. & 0x0000DC00;
  1669. /* Unused ring
  1670. dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
  1671. & 0x0000DC00;
  1672. */
  1673. b43_interrupt_ack(dev, reason);
  1674. /* disable all IRQs. They are enabled again in the bottom half. */
  1675. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
  1676. /* save the reason code and call our bottom half. */
  1677. dev->irq_reason = reason;
  1678. tasklet_schedule(&dev->isr_tasklet);
  1679. out:
  1680. mmiowb();
  1681. spin_unlock(&dev->wl->irq_lock);
  1682. return ret;
  1683. }
  1684. void b43_do_release_fw(struct b43_firmware_file *fw)
  1685. {
  1686. release_firmware(fw->data);
  1687. fw->data = NULL;
  1688. fw->filename = NULL;
  1689. }
  1690. static void b43_release_firmware(struct b43_wldev *dev)
  1691. {
  1692. b43_do_release_fw(&dev->fw.ucode);
  1693. b43_do_release_fw(&dev->fw.pcm);
  1694. b43_do_release_fw(&dev->fw.initvals);
  1695. b43_do_release_fw(&dev->fw.initvals_band);
  1696. }
  1697. static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
  1698. {
  1699. const char text[] =
  1700. "You must go to " \
  1701. "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
  1702. "and download the correct firmware for this driver version. " \
  1703. "Please carefully read all instructions on this website.\n";
  1704. if (error)
  1705. b43err(wl, text);
  1706. else
  1707. b43warn(wl, text);
  1708. }
  1709. int b43_do_request_fw(struct b43_request_fw_context *ctx,
  1710. const char *name,
  1711. struct b43_firmware_file *fw)
  1712. {
  1713. const struct firmware *blob;
  1714. struct b43_fw_header *hdr;
  1715. u32 size;
  1716. int err;
  1717. if (!name) {
  1718. /* Don't fetch anything. Free possibly cached firmware. */
  1719. /* FIXME: We should probably keep it anyway, to save some headache
  1720. * on suspend/resume with multiband devices. */
  1721. b43_do_release_fw(fw);
  1722. return 0;
  1723. }
  1724. if (fw->filename) {
  1725. if ((fw->type == ctx->req_type) &&
  1726. (strcmp(fw->filename, name) == 0))
  1727. return 0; /* Already have this fw. */
  1728. /* Free the cached firmware first. */
  1729. /* FIXME: We should probably do this later after we successfully
  1730. * got the new fw. This could reduce headache with multiband devices.
  1731. * We could also redesign this to cache the firmware for all possible
  1732. * bands all the time. */
  1733. b43_do_release_fw(fw);
  1734. }
  1735. switch (ctx->req_type) {
  1736. case B43_FWTYPE_PROPRIETARY:
  1737. snprintf(ctx->fwname, sizeof(ctx->fwname),
  1738. "b43%s/%s.fw",
  1739. modparam_fwpostfix, name);
  1740. break;
  1741. case B43_FWTYPE_OPENSOURCE:
  1742. snprintf(ctx->fwname, sizeof(ctx->fwname),
  1743. "b43-open%s/%s.fw",
  1744. modparam_fwpostfix, name);
  1745. break;
  1746. default:
  1747. B43_WARN_ON(1);
  1748. return -ENOSYS;
  1749. }
  1750. err = request_firmware(&blob, ctx->fwname, ctx->dev->dev->dev);
  1751. if (err == -ENOENT) {
  1752. snprintf(ctx->errors[ctx->req_type],
  1753. sizeof(ctx->errors[ctx->req_type]),
  1754. "Firmware file \"%s\" not found\n", ctx->fwname);
  1755. return err;
  1756. } else if (err) {
  1757. snprintf(ctx->errors[ctx->req_type],
  1758. sizeof(ctx->errors[ctx->req_type]),
  1759. "Firmware file \"%s\" request failed (err=%d)\n",
  1760. ctx->fwname, err);
  1761. return err;
  1762. }
  1763. if (blob->size < sizeof(struct b43_fw_header))
  1764. goto err_format;
  1765. hdr = (struct b43_fw_header *)(blob->data);
  1766. switch (hdr->type) {
  1767. case B43_FW_TYPE_UCODE:
  1768. case B43_FW_TYPE_PCM:
  1769. size = be32_to_cpu(hdr->size);
  1770. if (size != blob->size - sizeof(struct b43_fw_header))
  1771. goto err_format;
  1772. /* fallthrough */
  1773. case B43_FW_TYPE_IV:
  1774. if (hdr->ver != 1)
  1775. goto err_format;
  1776. break;
  1777. default:
  1778. goto err_format;
  1779. }
  1780. fw->data = blob;
  1781. fw->filename = name;
  1782. fw->type = ctx->req_type;
  1783. return 0;
  1784. err_format:
  1785. snprintf(ctx->errors[ctx->req_type],
  1786. sizeof(ctx->errors[ctx->req_type]),
  1787. "Firmware file \"%s\" format error.\n", ctx->fwname);
  1788. release_firmware(blob);
  1789. return -EPROTO;
  1790. }
  1791. static int b43_try_request_fw(struct b43_request_fw_context *ctx)
  1792. {
  1793. struct b43_wldev *dev = ctx->dev;
  1794. struct b43_firmware *fw = &ctx->dev->fw;
  1795. const u8 rev = ctx->dev->dev->id.revision;
  1796. const char *filename;
  1797. u32 tmshigh;
  1798. int err;
  1799. /* Get microcode */
  1800. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1801. if ((rev >= 5) && (rev <= 10))
  1802. filename = "ucode5";
  1803. else if ((rev >= 11) && (rev <= 12))
  1804. filename = "ucode11";
  1805. else if (rev >= 13)
  1806. filename = "ucode13";
  1807. else
  1808. goto err_no_ucode;
  1809. err = b43_do_request_fw(ctx, filename, &fw->ucode);
  1810. if (err)
  1811. goto err_load;
  1812. /* Get PCM code */
  1813. if ((rev >= 5) && (rev <= 10))
  1814. filename = "pcm5";
  1815. else if (rev >= 11)
  1816. filename = NULL;
  1817. else
  1818. goto err_no_pcm;
  1819. fw->pcm_request_failed = 0;
  1820. err = b43_do_request_fw(ctx, filename, &fw->pcm);
  1821. if (err == -ENOENT) {
  1822. /* We did not find a PCM file? Not fatal, but
  1823. * core rev <= 10 must do without hwcrypto then. */
  1824. fw->pcm_request_failed = 1;
  1825. } else if (err)
  1826. goto err_load;
  1827. /* Get initvals */
  1828. switch (dev->phy.type) {
  1829. case B43_PHYTYPE_A:
  1830. if ((rev >= 5) && (rev <= 10)) {
  1831. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1832. filename = "a0g1initvals5";
  1833. else
  1834. filename = "a0g0initvals5";
  1835. } else
  1836. goto err_no_initvals;
  1837. break;
  1838. case B43_PHYTYPE_G:
  1839. if ((rev >= 5) && (rev <= 10))
  1840. filename = "b0g0initvals5";
  1841. else if (rev >= 13)
  1842. filename = "b0g0initvals13";
  1843. else
  1844. goto err_no_initvals;
  1845. break;
  1846. case B43_PHYTYPE_N:
  1847. if ((rev >= 11) && (rev <= 12))
  1848. filename = "n0initvals11";
  1849. else
  1850. goto err_no_initvals;
  1851. break;
  1852. default:
  1853. goto err_no_initvals;
  1854. }
  1855. err = b43_do_request_fw(ctx, filename, &fw->initvals);
  1856. if (err)
  1857. goto err_load;
  1858. /* Get bandswitch initvals */
  1859. switch (dev->phy.type) {
  1860. case B43_PHYTYPE_A:
  1861. if ((rev >= 5) && (rev <= 10)) {
  1862. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1863. filename = "a0g1bsinitvals5";
  1864. else
  1865. filename = "a0g0bsinitvals5";
  1866. } else if (rev >= 11)
  1867. filename = NULL;
  1868. else
  1869. goto err_no_initvals;
  1870. break;
  1871. case B43_PHYTYPE_G:
  1872. if ((rev >= 5) && (rev <= 10))
  1873. filename = "b0g0bsinitvals5";
  1874. else if (rev >= 11)
  1875. filename = NULL;
  1876. else
  1877. goto err_no_initvals;
  1878. break;
  1879. case B43_PHYTYPE_N:
  1880. if ((rev >= 11) && (rev <= 12))
  1881. filename = "n0bsinitvals11";
  1882. else
  1883. goto err_no_initvals;
  1884. break;
  1885. default:
  1886. goto err_no_initvals;
  1887. }
  1888. err = b43_do_request_fw(ctx, filename, &fw->initvals_band);
  1889. if (err)
  1890. goto err_load;
  1891. return 0;
  1892. err_no_ucode:
  1893. err = ctx->fatal_failure = -EOPNOTSUPP;
  1894. b43err(dev->wl, "The driver does not know which firmware (ucode) "
  1895. "is required for your device (wl-core rev %u)\n", rev);
  1896. goto error;
  1897. err_no_pcm:
  1898. err = ctx->fatal_failure = -EOPNOTSUPP;
  1899. b43err(dev->wl, "The driver does not know which firmware (PCM) "
  1900. "is required for your device (wl-core rev %u)\n", rev);
  1901. goto error;
  1902. err_no_initvals:
  1903. err = ctx->fatal_failure = -EOPNOTSUPP;
  1904. b43err(dev->wl, "The driver does not know which firmware (initvals) "
  1905. "is required for your device (wl-core rev %u)\n", rev);
  1906. goto error;
  1907. err_load:
  1908. /* We failed to load this firmware image. The error message
  1909. * already is in ctx->errors. Return and let our caller decide
  1910. * what to do. */
  1911. goto error;
  1912. error:
  1913. b43_release_firmware(dev);
  1914. return err;
  1915. }
  1916. static int b43_request_firmware(struct b43_wldev *dev)
  1917. {
  1918. struct b43_request_fw_context *ctx;
  1919. unsigned int i;
  1920. int err;
  1921. const char *errmsg;
  1922. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1923. if (!ctx)
  1924. return -ENOMEM;
  1925. ctx->dev = dev;
  1926. ctx->req_type = B43_FWTYPE_PROPRIETARY;
  1927. err = b43_try_request_fw(ctx);
  1928. if (!err)
  1929. goto out; /* Successfully loaded it. */
  1930. err = ctx->fatal_failure;
  1931. if (err)
  1932. goto out;
  1933. ctx->req_type = B43_FWTYPE_OPENSOURCE;
  1934. err = b43_try_request_fw(ctx);
  1935. if (!err)
  1936. goto out; /* Successfully loaded it. */
  1937. err = ctx->fatal_failure;
  1938. if (err)
  1939. goto out;
  1940. /* Could not find a usable firmware. Print the errors. */
  1941. for (i = 0; i < B43_NR_FWTYPES; i++) {
  1942. errmsg = ctx->errors[i];
  1943. if (strlen(errmsg))
  1944. b43err(dev->wl, errmsg);
  1945. }
  1946. b43_print_fw_helptext(dev->wl, 1);
  1947. err = -ENOENT;
  1948. out:
  1949. kfree(ctx);
  1950. return err;
  1951. }
  1952. static int b43_upload_microcode(struct b43_wldev *dev)
  1953. {
  1954. const size_t hdr_len = sizeof(struct b43_fw_header);
  1955. const __be32 *data;
  1956. unsigned int i, len;
  1957. u16 fwrev, fwpatch, fwdate, fwtime;
  1958. u32 tmp, macctl;
  1959. int err = 0;
  1960. /* Jump the microcode PSM to offset 0 */
  1961. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1962. B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
  1963. macctl |= B43_MACCTL_PSM_JMP0;
  1964. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1965. /* Zero out all microcode PSM registers and shared memory. */
  1966. for (i = 0; i < 64; i++)
  1967. b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
  1968. for (i = 0; i < 4096; i += 2)
  1969. b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
  1970. /* Upload Microcode. */
  1971. data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
  1972. len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
  1973. b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
  1974. for (i = 0; i < len; i++) {
  1975. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1976. udelay(10);
  1977. }
  1978. if (dev->fw.pcm.data) {
  1979. /* Upload PCM data. */
  1980. data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
  1981. len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
  1982. b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
  1983. b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
  1984. /* No need for autoinc bit in SHM_HW */
  1985. b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
  1986. for (i = 0; i < len; i++) {
  1987. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1988. udelay(10);
  1989. }
  1990. }
  1991. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
  1992. /* Start the microcode PSM */
  1993. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1994. macctl &= ~B43_MACCTL_PSM_JMP0;
  1995. macctl |= B43_MACCTL_PSM_RUN;
  1996. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1997. /* Wait for the microcode to load and respond */
  1998. i = 0;
  1999. while (1) {
  2000. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2001. if (tmp == B43_IRQ_MAC_SUSPENDED)
  2002. break;
  2003. i++;
  2004. if (i >= 20) {
  2005. b43err(dev->wl, "Microcode not responding\n");
  2006. b43_print_fw_helptext(dev->wl, 1);
  2007. err = -ENODEV;
  2008. goto error;
  2009. }
  2010. msleep_interruptible(50);
  2011. if (signal_pending(current)) {
  2012. err = -EINTR;
  2013. goto error;
  2014. }
  2015. }
  2016. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
  2017. /* Get and check the revisions. */
  2018. fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
  2019. fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
  2020. fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
  2021. fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
  2022. if (fwrev <= 0x128) {
  2023. b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
  2024. "binary drivers older than version 4.x is unsupported. "
  2025. "You must upgrade your firmware files.\n");
  2026. b43_print_fw_helptext(dev->wl, 1);
  2027. err = -EOPNOTSUPP;
  2028. goto error;
  2029. }
  2030. dev->fw.rev = fwrev;
  2031. dev->fw.patch = fwpatch;
  2032. dev->fw.opensource = (fwdate == 0xFFFF);
  2033. /* Default to use-all-queues. */
  2034. dev->wl->hw->queues = dev->wl->mac80211_initially_registered_queues;
  2035. dev->qos_enabled = !!modparam_qos;
  2036. /* Default to firmware/hardware crypto acceleration. */
  2037. dev->hwcrypto_enabled = 1;
  2038. if (dev->fw.opensource) {
  2039. u16 fwcapa;
  2040. /* Patchlevel info is encoded in the "time" field. */
  2041. dev->fw.patch = fwtime;
  2042. b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
  2043. dev->fw.rev, dev->fw.patch);
  2044. fwcapa = b43_fwcapa_read(dev);
  2045. if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
  2046. b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
  2047. /* Disable hardware crypto and fall back to software crypto. */
  2048. dev->hwcrypto_enabled = 0;
  2049. }
  2050. if (!(fwcapa & B43_FWCAPA_QOS)) {
  2051. b43info(dev->wl, "QoS not supported by firmware\n");
  2052. /* Disable QoS. Tweak hw->queues to 1. It will be restored before
  2053. * ieee80211_unregister to make sure the networking core can
  2054. * properly free possible resources. */
  2055. dev->wl->hw->queues = 1;
  2056. dev->qos_enabled = 0;
  2057. }
  2058. } else {
  2059. b43info(dev->wl, "Loading firmware version %u.%u "
  2060. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
  2061. fwrev, fwpatch,
  2062. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  2063. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
  2064. if (dev->fw.pcm_request_failed) {
  2065. b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
  2066. "Hardware accelerated cryptography is disabled.\n");
  2067. b43_print_fw_helptext(dev->wl, 0);
  2068. }
  2069. }
  2070. if (b43_is_old_txhdr_format(dev)) {
  2071. /* We're over the deadline, but we keep support for old fw
  2072. * until it turns out to be in major conflict with something new. */
  2073. b43warn(dev->wl, "You are using an old firmware image. "
  2074. "Support for old firmware will be removed soon "
  2075. "(official deadline was July 2008).\n");
  2076. b43_print_fw_helptext(dev->wl, 0);
  2077. }
  2078. return 0;
  2079. error:
  2080. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  2081. macctl &= ~B43_MACCTL_PSM_RUN;
  2082. macctl |= B43_MACCTL_PSM_JMP0;
  2083. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2084. return err;
  2085. }
  2086. static int b43_write_initvals(struct b43_wldev *dev,
  2087. const struct b43_iv *ivals,
  2088. size_t count,
  2089. size_t array_size)
  2090. {
  2091. const struct b43_iv *iv;
  2092. u16 offset;
  2093. size_t i;
  2094. bool bit32;
  2095. BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
  2096. iv = ivals;
  2097. for (i = 0; i < count; i++) {
  2098. if (array_size < sizeof(iv->offset_size))
  2099. goto err_format;
  2100. array_size -= sizeof(iv->offset_size);
  2101. offset = be16_to_cpu(iv->offset_size);
  2102. bit32 = !!(offset & B43_IV_32BIT);
  2103. offset &= B43_IV_OFFSET_MASK;
  2104. if (offset >= 0x1000)
  2105. goto err_format;
  2106. if (bit32) {
  2107. u32 value;
  2108. if (array_size < sizeof(iv->data.d32))
  2109. goto err_format;
  2110. array_size -= sizeof(iv->data.d32);
  2111. value = get_unaligned_be32(&iv->data.d32);
  2112. b43_write32(dev, offset, value);
  2113. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2114. sizeof(__be16) +
  2115. sizeof(__be32));
  2116. } else {
  2117. u16 value;
  2118. if (array_size < sizeof(iv->data.d16))
  2119. goto err_format;
  2120. array_size -= sizeof(iv->data.d16);
  2121. value = be16_to_cpu(iv->data.d16);
  2122. b43_write16(dev, offset, value);
  2123. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2124. sizeof(__be16) +
  2125. sizeof(__be16));
  2126. }
  2127. }
  2128. if (array_size)
  2129. goto err_format;
  2130. return 0;
  2131. err_format:
  2132. b43err(dev->wl, "Initial Values Firmware file-format error.\n");
  2133. b43_print_fw_helptext(dev->wl, 1);
  2134. return -EPROTO;
  2135. }
  2136. static int b43_upload_initvals(struct b43_wldev *dev)
  2137. {
  2138. const size_t hdr_len = sizeof(struct b43_fw_header);
  2139. const struct b43_fw_header *hdr;
  2140. struct b43_firmware *fw = &dev->fw;
  2141. const struct b43_iv *ivals;
  2142. size_t count;
  2143. int err;
  2144. hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
  2145. ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
  2146. count = be32_to_cpu(hdr->size);
  2147. err = b43_write_initvals(dev, ivals, count,
  2148. fw->initvals.data->size - hdr_len);
  2149. if (err)
  2150. goto out;
  2151. if (fw->initvals_band.data) {
  2152. hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
  2153. ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
  2154. count = be32_to_cpu(hdr->size);
  2155. err = b43_write_initvals(dev, ivals, count,
  2156. fw->initvals_band.data->size - hdr_len);
  2157. if (err)
  2158. goto out;
  2159. }
  2160. out:
  2161. return err;
  2162. }
  2163. /* Initialize the GPIOs
  2164. * http://bcm-specs.sipsolutions.net/GPIO
  2165. */
  2166. static int b43_gpio_init(struct b43_wldev *dev)
  2167. {
  2168. struct ssb_bus *bus = dev->dev->bus;
  2169. struct ssb_device *gpiodev, *pcidev = NULL;
  2170. u32 mask, set;
  2171. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2172. & ~B43_MACCTL_GPOUTSMSK);
  2173. b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
  2174. | 0x000F);
  2175. mask = 0x0000001F;
  2176. set = 0x0000000F;
  2177. if (dev->dev->bus->chip_id == 0x4301) {
  2178. mask |= 0x0060;
  2179. set |= 0x0060;
  2180. }
  2181. if (0 /* FIXME: conditional unknown */ ) {
  2182. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2183. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2184. | 0x0100);
  2185. mask |= 0x0180;
  2186. set |= 0x0180;
  2187. }
  2188. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
  2189. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2190. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2191. | 0x0200);
  2192. mask |= 0x0200;
  2193. set |= 0x0200;
  2194. }
  2195. if (dev->dev->id.revision >= 2)
  2196. mask |= 0x0010; /* FIXME: This is redundant. */
  2197. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2198. pcidev = bus->pcicore.dev;
  2199. #endif
  2200. gpiodev = bus->chipco.dev ? : pcidev;
  2201. if (!gpiodev)
  2202. return 0;
  2203. ssb_write32(gpiodev, B43_GPIO_CONTROL,
  2204. (ssb_read32(gpiodev, B43_GPIO_CONTROL)
  2205. & mask) | set);
  2206. return 0;
  2207. }
  2208. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  2209. static void b43_gpio_cleanup(struct b43_wldev *dev)
  2210. {
  2211. struct ssb_bus *bus = dev->dev->bus;
  2212. struct ssb_device *gpiodev, *pcidev = NULL;
  2213. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2214. pcidev = bus->pcicore.dev;
  2215. #endif
  2216. gpiodev = bus->chipco.dev ? : pcidev;
  2217. if (!gpiodev)
  2218. return;
  2219. ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
  2220. }
  2221. /* http://bcm-specs.sipsolutions.net/EnableMac */
  2222. void b43_mac_enable(struct b43_wldev *dev)
  2223. {
  2224. if (b43_debug(dev, B43_DBG_FIRMWARE)) {
  2225. u16 fwstate;
  2226. fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
  2227. B43_SHM_SH_UCODESTAT);
  2228. if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
  2229. (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
  2230. b43err(dev->wl, "b43_mac_enable(): The firmware "
  2231. "should be suspended, but current state is %u\n",
  2232. fwstate);
  2233. }
  2234. }
  2235. dev->mac_suspended--;
  2236. B43_WARN_ON(dev->mac_suspended < 0);
  2237. if (dev->mac_suspended == 0) {
  2238. b43_write32(dev, B43_MMIO_MACCTL,
  2239. b43_read32(dev, B43_MMIO_MACCTL)
  2240. | B43_MACCTL_ENABLED);
  2241. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  2242. B43_IRQ_MAC_SUSPENDED);
  2243. /* Commit writes */
  2244. b43_read32(dev, B43_MMIO_MACCTL);
  2245. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2246. b43_power_saving_ctl_bits(dev, 0);
  2247. }
  2248. }
  2249. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  2250. void b43_mac_suspend(struct b43_wldev *dev)
  2251. {
  2252. int i;
  2253. u32 tmp;
  2254. might_sleep();
  2255. B43_WARN_ON(dev->mac_suspended < 0);
  2256. if (dev->mac_suspended == 0) {
  2257. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  2258. b43_write32(dev, B43_MMIO_MACCTL,
  2259. b43_read32(dev, B43_MMIO_MACCTL)
  2260. & ~B43_MACCTL_ENABLED);
  2261. /* force pci to flush the write */
  2262. b43_read32(dev, B43_MMIO_MACCTL);
  2263. for (i = 35; i; i--) {
  2264. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2265. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2266. goto out;
  2267. udelay(10);
  2268. }
  2269. /* Hm, it seems this will take some time. Use msleep(). */
  2270. for (i = 40; i; i--) {
  2271. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2272. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2273. goto out;
  2274. msleep(1);
  2275. }
  2276. b43err(dev->wl, "MAC suspend failed\n");
  2277. }
  2278. out:
  2279. dev->mac_suspended++;
  2280. }
  2281. static void b43_adjust_opmode(struct b43_wldev *dev)
  2282. {
  2283. struct b43_wl *wl = dev->wl;
  2284. u32 ctl;
  2285. u16 cfp_pretbtt;
  2286. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  2287. /* Reset status to STA infrastructure mode. */
  2288. ctl &= ~B43_MACCTL_AP;
  2289. ctl &= ~B43_MACCTL_KEEP_CTL;
  2290. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  2291. ctl &= ~B43_MACCTL_KEEP_BAD;
  2292. ctl &= ~B43_MACCTL_PROMISC;
  2293. ctl &= ~B43_MACCTL_BEACPROMISC;
  2294. ctl |= B43_MACCTL_INFRA;
  2295. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  2296. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  2297. ctl |= B43_MACCTL_AP;
  2298. else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
  2299. ctl &= ~B43_MACCTL_INFRA;
  2300. if (wl->filter_flags & FIF_CONTROL)
  2301. ctl |= B43_MACCTL_KEEP_CTL;
  2302. if (wl->filter_flags & FIF_FCSFAIL)
  2303. ctl |= B43_MACCTL_KEEP_BAD;
  2304. if (wl->filter_flags & FIF_PLCPFAIL)
  2305. ctl |= B43_MACCTL_KEEP_BADPLCP;
  2306. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  2307. ctl |= B43_MACCTL_PROMISC;
  2308. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  2309. ctl |= B43_MACCTL_BEACPROMISC;
  2310. /* Workaround: On old hardware the HW-MAC-address-filter
  2311. * doesn't work properly, so always run promisc in filter
  2312. * it in software. */
  2313. if (dev->dev->id.revision <= 4)
  2314. ctl |= B43_MACCTL_PROMISC;
  2315. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  2316. cfp_pretbtt = 2;
  2317. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  2318. if (dev->dev->bus->chip_id == 0x4306 &&
  2319. dev->dev->bus->chip_rev == 3)
  2320. cfp_pretbtt = 100;
  2321. else
  2322. cfp_pretbtt = 50;
  2323. }
  2324. b43_write16(dev, 0x612, cfp_pretbtt);
  2325. }
  2326. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  2327. {
  2328. u16 offset;
  2329. if (is_ofdm) {
  2330. offset = 0x480;
  2331. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  2332. } else {
  2333. offset = 0x4C0;
  2334. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  2335. }
  2336. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  2337. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  2338. }
  2339. static void b43_rate_memory_init(struct b43_wldev *dev)
  2340. {
  2341. switch (dev->phy.type) {
  2342. case B43_PHYTYPE_A:
  2343. case B43_PHYTYPE_G:
  2344. case B43_PHYTYPE_N:
  2345. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  2346. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  2347. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  2348. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  2349. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  2350. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  2351. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  2352. if (dev->phy.type == B43_PHYTYPE_A)
  2353. break;
  2354. /* fallthrough */
  2355. case B43_PHYTYPE_B:
  2356. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  2357. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  2358. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  2359. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  2360. break;
  2361. default:
  2362. B43_WARN_ON(1);
  2363. }
  2364. }
  2365. /* Set the default values for the PHY TX Control Words. */
  2366. static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
  2367. {
  2368. u16 ctl = 0;
  2369. ctl |= B43_TXH_PHY_ENC_CCK;
  2370. ctl |= B43_TXH_PHY_ANT01AUTO;
  2371. ctl |= B43_TXH_PHY_TXPWR;
  2372. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  2373. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
  2374. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
  2375. }
  2376. /* Set the TX-Antenna for management frames sent by firmware. */
  2377. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  2378. {
  2379. u16 ant;
  2380. u16 tmp;
  2381. ant = b43_antenna_to_phyctl(antenna);
  2382. /* For ACK/CTS */
  2383. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  2384. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2385. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  2386. /* For Probe Resposes */
  2387. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  2388. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2389. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  2390. }
  2391. /* This is the opposite of b43_chip_init() */
  2392. static void b43_chip_exit(struct b43_wldev *dev)
  2393. {
  2394. b43_phy_exit(dev);
  2395. b43_gpio_cleanup(dev);
  2396. /* firmware is released later */
  2397. }
  2398. /* Initialize the chip
  2399. * http://bcm-specs.sipsolutions.net/ChipInit
  2400. */
  2401. static int b43_chip_init(struct b43_wldev *dev)
  2402. {
  2403. struct b43_phy *phy = &dev->phy;
  2404. int err;
  2405. u32 value32, macctl;
  2406. u16 value16;
  2407. /* Initialize the MAC control */
  2408. macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
  2409. if (dev->phy.gmode)
  2410. macctl |= B43_MACCTL_GMODE;
  2411. macctl |= B43_MACCTL_INFRA;
  2412. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2413. err = b43_request_firmware(dev);
  2414. if (err)
  2415. goto out;
  2416. err = b43_upload_microcode(dev);
  2417. if (err)
  2418. goto out; /* firmware is released later */
  2419. err = b43_gpio_init(dev);
  2420. if (err)
  2421. goto out; /* firmware is released later */
  2422. err = b43_upload_initvals(dev);
  2423. if (err)
  2424. goto err_gpio_clean;
  2425. /* Turn the Analog on and initialize the PHY. */
  2426. phy->ops->switch_analog(dev, 1);
  2427. err = b43_phy_init(dev);
  2428. if (err)
  2429. goto err_gpio_clean;
  2430. /* Disable Interference Mitigation. */
  2431. if (phy->ops->interf_mitigation)
  2432. phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
  2433. /* Select the antennae */
  2434. if (phy->ops->set_rx_antenna)
  2435. phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  2436. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  2437. if (phy->type == B43_PHYTYPE_B) {
  2438. value16 = b43_read16(dev, 0x005E);
  2439. value16 |= 0x0004;
  2440. b43_write16(dev, 0x005E, value16);
  2441. }
  2442. b43_write32(dev, 0x0100, 0x01000000);
  2443. if (dev->dev->id.revision < 5)
  2444. b43_write32(dev, 0x010C, 0x01000000);
  2445. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2446. & ~B43_MACCTL_INFRA);
  2447. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2448. | B43_MACCTL_INFRA);
  2449. /* Probe Response Timeout value */
  2450. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  2451. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  2452. /* Initially set the wireless operation mode. */
  2453. b43_adjust_opmode(dev);
  2454. if (dev->dev->id.revision < 3) {
  2455. b43_write16(dev, 0x060E, 0x0000);
  2456. b43_write16(dev, 0x0610, 0x8000);
  2457. b43_write16(dev, 0x0604, 0x0000);
  2458. b43_write16(dev, 0x0606, 0x0200);
  2459. } else {
  2460. b43_write32(dev, 0x0188, 0x80000000);
  2461. b43_write32(dev, 0x018C, 0x02000000);
  2462. }
  2463. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  2464. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  2465. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  2466. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  2467. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  2468. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  2469. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  2470. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  2471. value32 |= 0x00100000;
  2472. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  2473. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  2474. dev->dev->bus->chipco.fast_pwrup_delay);
  2475. err = 0;
  2476. b43dbg(dev->wl, "Chip initialized\n");
  2477. out:
  2478. return err;
  2479. err_gpio_clean:
  2480. b43_gpio_cleanup(dev);
  2481. return err;
  2482. }
  2483. static void b43_periodic_every60sec(struct b43_wldev *dev)
  2484. {
  2485. const struct b43_phy_operations *ops = dev->phy.ops;
  2486. if (ops->pwork_60sec)
  2487. ops->pwork_60sec(dev);
  2488. /* Force check the TX power emission now. */
  2489. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
  2490. }
  2491. static void b43_periodic_every30sec(struct b43_wldev *dev)
  2492. {
  2493. /* Update device statistics. */
  2494. b43_calculate_link_quality(dev);
  2495. }
  2496. static void b43_periodic_every15sec(struct b43_wldev *dev)
  2497. {
  2498. struct b43_phy *phy = &dev->phy;
  2499. u16 wdr;
  2500. if (dev->fw.opensource) {
  2501. /* Check if the firmware is still alive.
  2502. * It will reset the watchdog counter to 0 in its idle loop. */
  2503. wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
  2504. if (unlikely(wdr)) {
  2505. b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
  2506. b43_controller_restart(dev, "Firmware watchdog");
  2507. return;
  2508. } else {
  2509. b43_shm_write16(dev, B43_SHM_SCRATCH,
  2510. B43_WATCHDOG_REG, 1);
  2511. }
  2512. }
  2513. if (phy->ops->pwork_15sec)
  2514. phy->ops->pwork_15sec(dev);
  2515. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2516. wmb();
  2517. }
  2518. static void do_periodic_work(struct b43_wldev *dev)
  2519. {
  2520. unsigned int state;
  2521. state = dev->periodic_state;
  2522. if (state % 4 == 0)
  2523. b43_periodic_every60sec(dev);
  2524. if (state % 2 == 0)
  2525. b43_periodic_every30sec(dev);
  2526. b43_periodic_every15sec(dev);
  2527. }
  2528. /* Periodic work locking policy:
  2529. * The whole periodic work handler is protected by
  2530. * wl->mutex. If another lock is needed somewhere in the
  2531. * pwork callchain, it's aquired in-place, where it's needed.
  2532. */
  2533. static void b43_periodic_work_handler(struct work_struct *work)
  2534. {
  2535. struct b43_wldev *dev = container_of(work, struct b43_wldev,
  2536. periodic_work.work);
  2537. struct b43_wl *wl = dev->wl;
  2538. unsigned long delay;
  2539. mutex_lock(&wl->mutex);
  2540. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2541. goto out;
  2542. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2543. goto out_requeue;
  2544. do_periodic_work(dev);
  2545. dev->periodic_state++;
  2546. out_requeue:
  2547. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2548. delay = msecs_to_jiffies(50);
  2549. else
  2550. delay = round_jiffies_relative(HZ * 15);
  2551. ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
  2552. out:
  2553. mutex_unlock(&wl->mutex);
  2554. }
  2555. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2556. {
  2557. struct delayed_work *work = &dev->periodic_work;
  2558. dev->periodic_state = 0;
  2559. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2560. ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
  2561. }
  2562. /* Check if communication with the device works correctly. */
  2563. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2564. {
  2565. u32 v, backup0, backup4;
  2566. backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2567. backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
  2568. /* Check for read/write and endianness problems. */
  2569. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2570. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2571. goto error;
  2572. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2573. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2574. goto error;
  2575. /* Check if unaligned 32bit SHM_SHARED access works properly.
  2576. * However, don't bail out on failure, because it's noncritical. */
  2577. b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
  2578. b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
  2579. b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
  2580. b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
  2581. if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
  2582. b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
  2583. b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
  2584. if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
  2585. b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
  2586. b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
  2587. b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
  2588. b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
  2589. b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
  2590. b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
  2591. if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
  2592. /* The 32bit register shadows the two 16bit registers
  2593. * with update sideeffects. Validate this. */
  2594. b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
  2595. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
  2596. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
  2597. goto error;
  2598. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
  2599. goto error;
  2600. }
  2601. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
  2602. v = b43_read32(dev, B43_MMIO_MACCTL);
  2603. v |= B43_MACCTL_GMODE;
  2604. if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2605. goto error;
  2606. return 0;
  2607. error:
  2608. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2609. return -ENODEV;
  2610. }
  2611. static void b43_security_init(struct b43_wldev *dev)
  2612. {
  2613. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2614. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2615. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2616. /* KTP is a word address, but we address SHM bytewise.
  2617. * So multiply by two.
  2618. */
  2619. dev->ktp *= 2;
  2620. if (dev->dev->id.revision >= 5) {
  2621. /* Number of RCMTA address slots */
  2622. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2623. }
  2624. b43_clear_keys(dev);
  2625. }
  2626. #ifdef CONFIG_B43_HWRNG
  2627. static int b43_rng_read(struct hwrng *rng, u32 *data)
  2628. {
  2629. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2630. unsigned long flags;
  2631. /* Don't take wl->mutex here, as it could deadlock with
  2632. * hwrng internal locking. It's not needed to take
  2633. * wl->mutex here, anyway. */
  2634. spin_lock_irqsave(&wl->irq_lock, flags);
  2635. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2636. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2637. return (sizeof(u16));
  2638. }
  2639. #endif /* CONFIG_B43_HWRNG */
  2640. static void b43_rng_exit(struct b43_wl *wl)
  2641. {
  2642. #ifdef CONFIG_B43_HWRNG
  2643. if (wl->rng_initialized)
  2644. hwrng_unregister(&wl->rng);
  2645. #endif /* CONFIG_B43_HWRNG */
  2646. }
  2647. static int b43_rng_init(struct b43_wl *wl)
  2648. {
  2649. int err = 0;
  2650. #ifdef CONFIG_B43_HWRNG
  2651. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2652. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2653. wl->rng.name = wl->rng_name;
  2654. wl->rng.data_read = b43_rng_read;
  2655. wl->rng.priv = (unsigned long)wl;
  2656. wl->rng_initialized = 1;
  2657. err = hwrng_register(&wl->rng);
  2658. if (err) {
  2659. wl->rng_initialized = 0;
  2660. b43err(wl, "Failed to register the random "
  2661. "number generator (%d)\n", err);
  2662. }
  2663. #endif /* CONFIG_B43_HWRNG */
  2664. return err;
  2665. }
  2666. static int b43_op_tx(struct ieee80211_hw *hw,
  2667. struct sk_buff *skb)
  2668. {
  2669. struct b43_wl *wl = hw_to_b43_wl(hw);
  2670. struct b43_wldev *dev = wl->current_dev;
  2671. unsigned long flags;
  2672. int err;
  2673. if (unlikely(skb->len < 2 + 2 + 6)) {
  2674. /* Too short, this can't be a valid frame. */
  2675. goto drop_packet;
  2676. }
  2677. B43_WARN_ON(skb_shinfo(skb)->nr_frags);
  2678. if (unlikely(!dev))
  2679. goto drop_packet;
  2680. /* Transmissions on seperate queues can run concurrently. */
  2681. read_lock_irqsave(&wl->tx_lock, flags);
  2682. err = -ENODEV;
  2683. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2684. if (b43_using_pio_transfers(dev))
  2685. err = b43_pio_tx(dev, skb);
  2686. else
  2687. err = b43_dma_tx(dev, skb);
  2688. }
  2689. read_unlock_irqrestore(&wl->tx_lock, flags);
  2690. if (unlikely(err))
  2691. goto drop_packet;
  2692. return NETDEV_TX_OK;
  2693. drop_packet:
  2694. /* We can not transmit this packet. Drop it. */
  2695. dev_kfree_skb_any(skb);
  2696. return NETDEV_TX_OK;
  2697. }
  2698. /* Locking: wl->irq_lock */
  2699. static void b43_qos_params_upload(struct b43_wldev *dev,
  2700. const struct ieee80211_tx_queue_params *p,
  2701. u16 shm_offset)
  2702. {
  2703. u16 params[B43_NR_QOSPARAMS];
  2704. int bslots, tmp;
  2705. unsigned int i;
  2706. bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
  2707. memset(&params, 0, sizeof(params));
  2708. params[B43_QOSPARAM_TXOP] = p->txop * 32;
  2709. params[B43_QOSPARAM_CWMIN] = p->cw_min;
  2710. params[B43_QOSPARAM_CWMAX] = p->cw_max;
  2711. params[B43_QOSPARAM_CWCUR] = p->cw_min;
  2712. params[B43_QOSPARAM_AIFS] = p->aifs;
  2713. params[B43_QOSPARAM_BSLOTS] = bslots;
  2714. params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
  2715. for (i = 0; i < ARRAY_SIZE(params); i++) {
  2716. if (i == B43_QOSPARAM_STATUS) {
  2717. tmp = b43_shm_read16(dev, B43_SHM_SHARED,
  2718. shm_offset + (i * 2));
  2719. /* Mark the parameters as updated. */
  2720. tmp |= 0x100;
  2721. b43_shm_write16(dev, B43_SHM_SHARED,
  2722. shm_offset + (i * 2),
  2723. tmp);
  2724. } else {
  2725. b43_shm_write16(dev, B43_SHM_SHARED,
  2726. shm_offset + (i * 2),
  2727. params[i]);
  2728. }
  2729. }
  2730. }
  2731. /* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
  2732. static const u16 b43_qos_shm_offsets[] = {
  2733. /* [mac80211-queue-nr] = SHM_OFFSET, */
  2734. [0] = B43_QOS_VOICE,
  2735. [1] = B43_QOS_VIDEO,
  2736. [2] = B43_QOS_BESTEFFORT,
  2737. [3] = B43_QOS_BACKGROUND,
  2738. };
  2739. /* Update all QOS parameters in hardware. */
  2740. static void b43_qos_upload_all(struct b43_wldev *dev)
  2741. {
  2742. struct b43_wl *wl = dev->wl;
  2743. struct b43_qos_params *params;
  2744. unsigned int i;
  2745. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2746. ARRAY_SIZE(wl->qos_params));
  2747. b43_mac_suspend(dev);
  2748. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2749. params = &(wl->qos_params[i]);
  2750. b43_qos_params_upload(dev, &(params->p),
  2751. b43_qos_shm_offsets[i]);
  2752. }
  2753. b43_mac_enable(dev);
  2754. }
  2755. static void b43_qos_clear(struct b43_wl *wl)
  2756. {
  2757. struct b43_qos_params *params;
  2758. unsigned int i;
  2759. /* Initialize QoS parameters to sane defaults. */
  2760. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2761. ARRAY_SIZE(wl->qos_params));
  2762. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2763. params = &(wl->qos_params[i]);
  2764. switch (b43_qos_shm_offsets[i]) {
  2765. case B43_QOS_VOICE:
  2766. params->p.txop = 0;
  2767. params->p.aifs = 2;
  2768. params->p.cw_min = 0x0001;
  2769. params->p.cw_max = 0x0001;
  2770. break;
  2771. case B43_QOS_VIDEO:
  2772. params->p.txop = 0;
  2773. params->p.aifs = 2;
  2774. params->p.cw_min = 0x0001;
  2775. params->p.cw_max = 0x0001;
  2776. break;
  2777. case B43_QOS_BESTEFFORT:
  2778. params->p.txop = 0;
  2779. params->p.aifs = 3;
  2780. params->p.cw_min = 0x0001;
  2781. params->p.cw_max = 0x03FF;
  2782. break;
  2783. case B43_QOS_BACKGROUND:
  2784. params->p.txop = 0;
  2785. params->p.aifs = 7;
  2786. params->p.cw_min = 0x0001;
  2787. params->p.cw_max = 0x03FF;
  2788. break;
  2789. default:
  2790. B43_WARN_ON(1);
  2791. }
  2792. }
  2793. }
  2794. /* Initialize the core's QOS capabilities */
  2795. static void b43_qos_init(struct b43_wldev *dev)
  2796. {
  2797. /* Upload the current QOS parameters. */
  2798. b43_qos_upload_all(dev);
  2799. /* Enable QOS support. */
  2800. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
  2801. b43_write16(dev, B43_MMIO_IFSCTL,
  2802. b43_read16(dev, B43_MMIO_IFSCTL)
  2803. | B43_MMIO_IFSCTL_USE_EDCF);
  2804. }
  2805. static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
  2806. const struct ieee80211_tx_queue_params *params)
  2807. {
  2808. struct b43_wl *wl = hw_to_b43_wl(hw);
  2809. struct b43_wldev *dev;
  2810. unsigned int queue = (unsigned int)_queue;
  2811. int err = -ENODEV;
  2812. if (queue >= ARRAY_SIZE(wl->qos_params)) {
  2813. /* Queue not available or don't support setting
  2814. * params on this queue. Return success to not
  2815. * confuse mac80211. */
  2816. return 0;
  2817. }
  2818. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2819. ARRAY_SIZE(wl->qos_params));
  2820. mutex_lock(&wl->mutex);
  2821. dev = wl->current_dev;
  2822. if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
  2823. goto out_unlock;
  2824. memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
  2825. b43_mac_suspend(dev);
  2826. b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
  2827. b43_qos_shm_offsets[queue]);
  2828. b43_mac_enable(dev);
  2829. err = 0;
  2830. out_unlock:
  2831. mutex_unlock(&wl->mutex);
  2832. return err;
  2833. }
  2834. static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
  2835. struct ieee80211_tx_queue_stats *stats)
  2836. {
  2837. struct b43_wl *wl = hw_to_b43_wl(hw);
  2838. struct b43_wldev *dev = wl->current_dev;
  2839. unsigned long flags;
  2840. int err = -ENODEV;
  2841. if (!dev)
  2842. goto out;
  2843. spin_lock_irqsave(&wl->irq_lock, flags);
  2844. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2845. if (b43_using_pio_transfers(dev))
  2846. b43_pio_get_tx_stats(dev, stats);
  2847. else
  2848. b43_dma_get_tx_stats(dev, stats);
  2849. err = 0;
  2850. }
  2851. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2852. out:
  2853. return err;
  2854. }
  2855. static int b43_op_get_stats(struct ieee80211_hw *hw,
  2856. struct ieee80211_low_level_stats *stats)
  2857. {
  2858. struct b43_wl *wl = hw_to_b43_wl(hw);
  2859. unsigned long flags;
  2860. spin_lock_irqsave(&wl->irq_lock, flags);
  2861. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2862. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2863. return 0;
  2864. }
  2865. static u64 b43_op_get_tsf(struct ieee80211_hw *hw)
  2866. {
  2867. struct b43_wl *wl = hw_to_b43_wl(hw);
  2868. struct b43_wldev *dev;
  2869. u64 tsf;
  2870. mutex_lock(&wl->mutex);
  2871. spin_lock_irq(&wl->irq_lock);
  2872. dev = wl->current_dev;
  2873. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
  2874. b43_tsf_read(dev, &tsf);
  2875. else
  2876. tsf = 0;
  2877. spin_unlock_irq(&wl->irq_lock);
  2878. mutex_unlock(&wl->mutex);
  2879. return tsf;
  2880. }
  2881. static void b43_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2882. {
  2883. struct b43_wl *wl = hw_to_b43_wl(hw);
  2884. struct b43_wldev *dev;
  2885. mutex_lock(&wl->mutex);
  2886. spin_lock_irq(&wl->irq_lock);
  2887. dev = wl->current_dev;
  2888. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
  2889. b43_tsf_write(dev, tsf);
  2890. spin_unlock_irq(&wl->irq_lock);
  2891. mutex_unlock(&wl->mutex);
  2892. }
  2893. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2894. {
  2895. struct ssb_device *sdev = dev->dev;
  2896. u32 tmslow;
  2897. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2898. tmslow &= ~B43_TMSLOW_GMODE;
  2899. tmslow |= B43_TMSLOW_PHYRESET;
  2900. tmslow |= SSB_TMSLOW_FGC;
  2901. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2902. msleep(1);
  2903. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2904. tmslow &= ~SSB_TMSLOW_FGC;
  2905. tmslow |= B43_TMSLOW_PHYRESET;
  2906. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2907. msleep(1);
  2908. }
  2909. static const char *band_to_string(enum ieee80211_band band)
  2910. {
  2911. switch (band) {
  2912. case IEEE80211_BAND_5GHZ:
  2913. return "5";
  2914. case IEEE80211_BAND_2GHZ:
  2915. return "2.4";
  2916. default:
  2917. break;
  2918. }
  2919. B43_WARN_ON(1);
  2920. return "";
  2921. }
  2922. /* Expects wl->mutex locked */
  2923. static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
  2924. {
  2925. struct b43_wldev *up_dev = NULL;
  2926. struct b43_wldev *down_dev;
  2927. struct b43_wldev *d;
  2928. int err;
  2929. bool uninitialized_var(gmode);
  2930. int prev_status;
  2931. /* Find a device and PHY which supports the band. */
  2932. list_for_each_entry(d, &wl->devlist, list) {
  2933. switch (chan->band) {
  2934. case IEEE80211_BAND_5GHZ:
  2935. if (d->phy.supports_5ghz) {
  2936. up_dev = d;
  2937. gmode = 0;
  2938. }
  2939. break;
  2940. case IEEE80211_BAND_2GHZ:
  2941. if (d->phy.supports_2ghz) {
  2942. up_dev = d;
  2943. gmode = 1;
  2944. }
  2945. break;
  2946. default:
  2947. B43_WARN_ON(1);
  2948. return -EINVAL;
  2949. }
  2950. if (up_dev)
  2951. break;
  2952. }
  2953. if (!up_dev) {
  2954. b43err(wl, "Could not find a device for %s-GHz band operation\n",
  2955. band_to_string(chan->band));
  2956. return -ENODEV;
  2957. }
  2958. if ((up_dev == wl->current_dev) &&
  2959. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2960. /* This device is already running. */
  2961. return 0;
  2962. }
  2963. b43dbg(wl, "Switching to %s-GHz band\n",
  2964. band_to_string(chan->band));
  2965. down_dev = wl->current_dev;
  2966. prev_status = b43_status(down_dev);
  2967. /* Shutdown the currently running core. */
  2968. if (prev_status >= B43_STAT_STARTED)
  2969. b43_wireless_core_stop(down_dev);
  2970. if (prev_status >= B43_STAT_INITIALIZED)
  2971. b43_wireless_core_exit(down_dev);
  2972. if (down_dev != up_dev) {
  2973. /* We switch to a different core, so we put PHY into
  2974. * RESET on the old core. */
  2975. b43_put_phy_into_reset(down_dev);
  2976. }
  2977. /* Now start the new core. */
  2978. up_dev->phy.gmode = gmode;
  2979. if (prev_status >= B43_STAT_INITIALIZED) {
  2980. err = b43_wireless_core_init(up_dev);
  2981. if (err) {
  2982. b43err(wl, "Fatal: Could not initialize device for "
  2983. "selected %s-GHz band\n",
  2984. band_to_string(chan->band));
  2985. goto init_failure;
  2986. }
  2987. }
  2988. if (prev_status >= B43_STAT_STARTED) {
  2989. err = b43_wireless_core_start(up_dev);
  2990. if (err) {
  2991. b43err(wl, "Fatal: Coult not start device for "
  2992. "selected %s-GHz band\n",
  2993. band_to_string(chan->band));
  2994. b43_wireless_core_exit(up_dev);
  2995. goto init_failure;
  2996. }
  2997. }
  2998. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2999. wl->current_dev = up_dev;
  3000. return 0;
  3001. init_failure:
  3002. /* Whoops, failed to init the new core. No core is operating now. */
  3003. wl->current_dev = NULL;
  3004. return err;
  3005. }
  3006. /* Write the short and long frame retry limit values. */
  3007. static void b43_set_retry_limits(struct b43_wldev *dev,
  3008. unsigned int short_retry,
  3009. unsigned int long_retry)
  3010. {
  3011. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  3012. * the chip-internal counter. */
  3013. short_retry = min(short_retry, (unsigned int)0xF);
  3014. long_retry = min(long_retry, (unsigned int)0xF);
  3015. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
  3016. short_retry);
  3017. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
  3018. long_retry);
  3019. }
  3020. static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
  3021. {
  3022. struct b43_wl *wl = hw_to_b43_wl(hw);
  3023. struct b43_wldev *dev;
  3024. struct b43_phy *phy;
  3025. struct ieee80211_conf *conf = &hw->conf;
  3026. unsigned long flags;
  3027. int antenna;
  3028. int err = 0;
  3029. mutex_lock(&wl->mutex);
  3030. /* Switch the band (if necessary). This might change the active core. */
  3031. err = b43_switch_band(wl, conf->channel);
  3032. if (err)
  3033. goto out_unlock_mutex;
  3034. dev = wl->current_dev;
  3035. phy = &dev->phy;
  3036. b43_mac_suspend(dev);
  3037. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  3038. b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
  3039. conf->long_frame_max_tx_count);
  3040. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  3041. if (!changed)
  3042. goto out_mac_enable;
  3043. /* Switch to the requested channel.
  3044. * The firmware takes care of races with the TX handler. */
  3045. if (conf->channel->hw_value != phy->channel)
  3046. b43_switch_channel(dev, conf->channel->hw_value);
  3047. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  3048. /* Adjust the desired TX power level. */
  3049. if (conf->power_level != 0) {
  3050. spin_lock_irqsave(&wl->irq_lock, flags);
  3051. if (conf->power_level != phy->desired_txpower) {
  3052. phy->desired_txpower = conf->power_level;
  3053. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
  3054. B43_TXPWR_IGNORE_TSSI);
  3055. }
  3056. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3057. }
  3058. /* Antennas for RX and management frame TX. */
  3059. antenna = B43_ANTENNA_DEFAULT;
  3060. b43_mgmtframe_txantenna(dev, antenna);
  3061. antenna = B43_ANTENNA_DEFAULT;
  3062. if (phy->ops->set_rx_antenna)
  3063. phy->ops->set_rx_antenna(dev, antenna);
  3064. if (wl->radio_enabled != phy->radio_on) {
  3065. if (wl->radio_enabled) {
  3066. b43_software_rfkill(dev, false);
  3067. b43info(dev->wl, "Radio turned on by software\n");
  3068. if (!dev->radio_hw_enable) {
  3069. b43info(dev->wl, "The hardware RF-kill button "
  3070. "still turns the radio physically off. "
  3071. "Press the button to turn it on.\n");
  3072. }
  3073. } else {
  3074. b43_software_rfkill(dev, true);
  3075. b43info(dev->wl, "Radio turned off by software\n");
  3076. }
  3077. }
  3078. out_mac_enable:
  3079. b43_mac_enable(dev);
  3080. out_unlock_mutex:
  3081. mutex_unlock(&wl->mutex);
  3082. return err;
  3083. }
  3084. static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
  3085. {
  3086. struct ieee80211_supported_band *sband =
  3087. dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
  3088. struct ieee80211_rate *rate;
  3089. int i;
  3090. u16 basic, direct, offset, basic_offset, rateptr;
  3091. for (i = 0; i < sband->n_bitrates; i++) {
  3092. rate = &sband->bitrates[i];
  3093. if (b43_is_cck_rate(rate->hw_value)) {
  3094. direct = B43_SHM_SH_CCKDIRECT;
  3095. basic = B43_SHM_SH_CCKBASIC;
  3096. offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  3097. offset &= 0xF;
  3098. } else {
  3099. direct = B43_SHM_SH_OFDMDIRECT;
  3100. basic = B43_SHM_SH_OFDMBASIC;
  3101. offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  3102. offset &= 0xF;
  3103. }
  3104. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  3105. if (b43_is_cck_rate(rate->hw_value)) {
  3106. basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  3107. basic_offset &= 0xF;
  3108. } else {
  3109. basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  3110. basic_offset &= 0xF;
  3111. }
  3112. /*
  3113. * Get the pointer that we need to point to
  3114. * from the direct map
  3115. */
  3116. rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
  3117. direct + 2 * basic_offset);
  3118. /* and write it to the basic map */
  3119. b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
  3120. rateptr);
  3121. }
  3122. }
  3123. static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
  3124. struct ieee80211_vif *vif,
  3125. struct ieee80211_bss_conf *conf,
  3126. u32 changed)
  3127. {
  3128. struct b43_wl *wl = hw_to_b43_wl(hw);
  3129. struct b43_wldev *dev;
  3130. unsigned long flags;
  3131. mutex_lock(&wl->mutex);
  3132. dev = wl->current_dev;
  3133. if (!dev || b43_status(dev) < B43_STAT_STARTED)
  3134. goto out_unlock_mutex;
  3135. B43_WARN_ON(wl->vif != vif);
  3136. spin_lock_irqsave(&wl->irq_lock, flags);
  3137. if (changed & BSS_CHANGED_BSSID) {
  3138. if (conf->bssid)
  3139. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  3140. else
  3141. memset(wl->bssid, 0, ETH_ALEN);
  3142. }
  3143. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  3144. if (changed & BSS_CHANGED_BEACON &&
  3145. (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  3146. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
  3147. b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
  3148. b43_update_templates(wl);
  3149. if (changed & BSS_CHANGED_BSSID)
  3150. b43_write_mac_bssid_templates(dev);
  3151. }
  3152. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3153. b43_mac_suspend(dev);
  3154. /* Update templates for AP/mesh mode. */
  3155. if (changed & BSS_CHANGED_BEACON_INT &&
  3156. (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  3157. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
  3158. b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
  3159. b43_set_beacon_int(dev, conf->beacon_int);
  3160. if (changed & BSS_CHANGED_BASIC_RATES)
  3161. b43_update_basic_rates(dev, conf->basic_rates);
  3162. if (changed & BSS_CHANGED_ERP_SLOT) {
  3163. if (conf->use_short_slot)
  3164. b43_short_slot_timing_enable(dev);
  3165. else
  3166. b43_short_slot_timing_disable(dev);
  3167. }
  3168. b43_mac_enable(dev);
  3169. out_unlock_mutex:
  3170. mutex_unlock(&wl->mutex);
  3171. }
  3172. static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3173. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  3174. struct ieee80211_key_conf *key)
  3175. {
  3176. struct b43_wl *wl = hw_to_b43_wl(hw);
  3177. struct b43_wldev *dev;
  3178. u8 algorithm;
  3179. u8 index;
  3180. int err;
  3181. static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  3182. if (modparam_nohwcrypt)
  3183. return -ENOSPC; /* User disabled HW-crypto */
  3184. mutex_lock(&wl->mutex);
  3185. spin_lock_irq(&wl->irq_lock);
  3186. write_lock(&wl->tx_lock);
  3187. /* Why do we need all this locking here?
  3188. * mutex -> Every config operation must take it.
  3189. * irq_lock -> We modify the dev->key array, which is accessed
  3190. * in the IRQ handlers.
  3191. * tx_lock -> We modify the dev->key array, which is accessed
  3192. * in the TX handler.
  3193. */
  3194. dev = wl->current_dev;
  3195. err = -ENODEV;
  3196. if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
  3197. goto out_unlock;
  3198. if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
  3199. /* We don't have firmware for the crypto engine.
  3200. * Must use software-crypto. */
  3201. err = -EOPNOTSUPP;
  3202. goto out_unlock;
  3203. }
  3204. err = -EINVAL;
  3205. switch (key->alg) {
  3206. case ALG_WEP:
  3207. if (key->keylen == WLAN_KEY_LEN_WEP40)
  3208. algorithm = B43_SEC_ALGO_WEP40;
  3209. else
  3210. algorithm = B43_SEC_ALGO_WEP104;
  3211. break;
  3212. case ALG_TKIP:
  3213. algorithm = B43_SEC_ALGO_TKIP;
  3214. break;
  3215. case ALG_CCMP:
  3216. algorithm = B43_SEC_ALGO_AES;
  3217. break;
  3218. default:
  3219. B43_WARN_ON(1);
  3220. goto out_unlock;
  3221. }
  3222. index = (u8) (key->keyidx);
  3223. if (index > 3)
  3224. goto out_unlock;
  3225. switch (cmd) {
  3226. case SET_KEY:
  3227. if (algorithm == B43_SEC_ALGO_TKIP) {
  3228. /* FIXME: No TKIP hardware encryption for now. */
  3229. err = -EOPNOTSUPP;
  3230. goto out_unlock;
  3231. }
  3232. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  3233. if (WARN_ON(!sta)) {
  3234. err = -EOPNOTSUPP;
  3235. goto out_unlock;
  3236. }
  3237. /* Pairwise key with an assigned MAC address. */
  3238. err = b43_key_write(dev, -1, algorithm,
  3239. key->key, key->keylen,
  3240. sta->addr, key);
  3241. } else {
  3242. /* Group key */
  3243. err = b43_key_write(dev, index, algorithm,
  3244. key->key, key->keylen, NULL, key);
  3245. }
  3246. if (err)
  3247. goto out_unlock;
  3248. if (algorithm == B43_SEC_ALGO_WEP40 ||
  3249. algorithm == B43_SEC_ALGO_WEP104) {
  3250. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  3251. } else {
  3252. b43_hf_write(dev,
  3253. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  3254. }
  3255. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  3256. break;
  3257. case DISABLE_KEY: {
  3258. err = b43_key_clear(dev, key->hw_key_idx);
  3259. if (err)
  3260. goto out_unlock;
  3261. break;
  3262. }
  3263. default:
  3264. B43_WARN_ON(1);
  3265. }
  3266. out_unlock:
  3267. if (!err) {
  3268. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  3269. "mac: %pM\n",
  3270. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  3271. sta ? sta->addr : bcast_addr);
  3272. b43_dump_keymemory(dev);
  3273. }
  3274. write_unlock(&wl->tx_lock);
  3275. spin_unlock_irq(&wl->irq_lock);
  3276. mutex_unlock(&wl->mutex);
  3277. return err;
  3278. }
  3279. static void b43_op_configure_filter(struct ieee80211_hw *hw,
  3280. unsigned int changed, unsigned int *fflags,
  3281. int mc_count, struct dev_addr_list *mc_list)
  3282. {
  3283. struct b43_wl *wl = hw_to_b43_wl(hw);
  3284. struct b43_wldev *dev = wl->current_dev;
  3285. unsigned long flags;
  3286. if (!dev) {
  3287. *fflags = 0;
  3288. return;
  3289. }
  3290. spin_lock_irqsave(&wl->irq_lock, flags);
  3291. *fflags &= FIF_PROMISC_IN_BSS |
  3292. FIF_ALLMULTI |
  3293. FIF_FCSFAIL |
  3294. FIF_PLCPFAIL |
  3295. FIF_CONTROL |
  3296. FIF_OTHER_BSS |
  3297. FIF_BCN_PRBRESP_PROMISC;
  3298. changed &= FIF_PROMISC_IN_BSS |
  3299. FIF_ALLMULTI |
  3300. FIF_FCSFAIL |
  3301. FIF_PLCPFAIL |
  3302. FIF_CONTROL |
  3303. FIF_OTHER_BSS |
  3304. FIF_BCN_PRBRESP_PROMISC;
  3305. wl->filter_flags = *fflags;
  3306. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  3307. b43_adjust_opmode(dev);
  3308. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3309. }
  3310. /* Locking: wl->mutex */
  3311. static void b43_wireless_core_stop(struct b43_wldev *dev)
  3312. {
  3313. struct b43_wl *wl = dev->wl;
  3314. unsigned long flags;
  3315. if (b43_status(dev) < B43_STAT_STARTED)
  3316. return;
  3317. /* Disable and sync interrupts. We must do this before than
  3318. * setting the status to INITIALIZED, as the interrupt handler
  3319. * won't care about IRQs then. */
  3320. spin_lock_irqsave(&wl->irq_lock, flags);
  3321. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
  3322. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  3323. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3324. b43_synchronize_irq(dev);
  3325. write_lock_irqsave(&wl->tx_lock, flags);
  3326. b43_set_status(dev, B43_STAT_INITIALIZED);
  3327. write_unlock_irqrestore(&wl->tx_lock, flags);
  3328. b43_pio_stop(dev);
  3329. mutex_unlock(&wl->mutex);
  3330. /* Must unlock as it would otherwise deadlock. No races here.
  3331. * Cancel the possibly running self-rearming periodic work. */
  3332. cancel_delayed_work_sync(&dev->periodic_work);
  3333. mutex_lock(&wl->mutex);
  3334. b43_mac_suspend(dev);
  3335. free_irq(dev->dev->irq, dev);
  3336. b43dbg(wl, "Wireless interface stopped\n");
  3337. }
  3338. /* Locking: wl->mutex */
  3339. static int b43_wireless_core_start(struct b43_wldev *dev)
  3340. {
  3341. int err;
  3342. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  3343. drain_txstatus_queue(dev);
  3344. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  3345. IRQF_SHARED, KBUILD_MODNAME, dev);
  3346. if (err) {
  3347. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  3348. goto out;
  3349. }
  3350. /* We are ready to run. */
  3351. b43_set_status(dev, B43_STAT_STARTED);
  3352. /* Start data flow (TX/RX). */
  3353. b43_mac_enable(dev);
  3354. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  3355. /* Start maintainance work */
  3356. b43_periodic_tasks_setup(dev);
  3357. b43dbg(dev->wl, "Wireless interface started\n");
  3358. out:
  3359. return err;
  3360. }
  3361. /* Get PHY and RADIO versioning numbers */
  3362. static int b43_phy_versioning(struct b43_wldev *dev)
  3363. {
  3364. struct b43_phy *phy = &dev->phy;
  3365. u32 tmp;
  3366. u8 analog_type;
  3367. u8 phy_type;
  3368. u8 phy_rev;
  3369. u16 radio_manuf;
  3370. u16 radio_ver;
  3371. u16 radio_rev;
  3372. int unsupported = 0;
  3373. /* Get PHY versioning */
  3374. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  3375. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  3376. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  3377. phy_rev = (tmp & B43_PHYVER_VERSION);
  3378. switch (phy_type) {
  3379. case B43_PHYTYPE_A:
  3380. if (phy_rev >= 4)
  3381. unsupported = 1;
  3382. break;
  3383. case B43_PHYTYPE_B:
  3384. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  3385. && phy_rev != 7)
  3386. unsupported = 1;
  3387. break;
  3388. case B43_PHYTYPE_G:
  3389. if (phy_rev > 9)
  3390. unsupported = 1;
  3391. break;
  3392. #ifdef CONFIG_B43_NPHY
  3393. case B43_PHYTYPE_N:
  3394. if (phy_rev > 4)
  3395. unsupported = 1;
  3396. break;
  3397. #endif
  3398. #ifdef CONFIG_B43_PHY_LP
  3399. case B43_PHYTYPE_LP:
  3400. if (phy_rev > 1)
  3401. unsupported = 1;
  3402. break;
  3403. #endif
  3404. default:
  3405. unsupported = 1;
  3406. };
  3407. if (unsupported) {
  3408. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  3409. "(Analog %u, Type %u, Revision %u)\n",
  3410. analog_type, phy_type, phy_rev);
  3411. return -EOPNOTSUPP;
  3412. }
  3413. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  3414. analog_type, phy_type, phy_rev);
  3415. /* Get RADIO versioning */
  3416. if (dev->dev->bus->chip_id == 0x4317) {
  3417. if (dev->dev->bus->chip_rev == 0)
  3418. tmp = 0x3205017F;
  3419. else if (dev->dev->bus->chip_rev == 1)
  3420. tmp = 0x4205017F;
  3421. else
  3422. tmp = 0x5205017F;
  3423. } else {
  3424. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3425. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  3426. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3427. tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
  3428. }
  3429. radio_manuf = (tmp & 0x00000FFF);
  3430. radio_ver = (tmp & 0x0FFFF000) >> 12;
  3431. radio_rev = (tmp & 0xF0000000) >> 28;
  3432. if (radio_manuf != 0x17F /* Broadcom */)
  3433. unsupported = 1;
  3434. switch (phy_type) {
  3435. case B43_PHYTYPE_A:
  3436. if (radio_ver != 0x2060)
  3437. unsupported = 1;
  3438. if (radio_rev != 1)
  3439. unsupported = 1;
  3440. if (radio_manuf != 0x17F)
  3441. unsupported = 1;
  3442. break;
  3443. case B43_PHYTYPE_B:
  3444. if ((radio_ver & 0xFFF0) != 0x2050)
  3445. unsupported = 1;
  3446. break;
  3447. case B43_PHYTYPE_G:
  3448. if (radio_ver != 0x2050)
  3449. unsupported = 1;
  3450. break;
  3451. case B43_PHYTYPE_N:
  3452. if (radio_ver != 0x2055 && radio_ver != 0x2056)
  3453. unsupported = 1;
  3454. break;
  3455. case B43_PHYTYPE_LP:
  3456. if (radio_ver != 0x2062)
  3457. unsupported = 1;
  3458. break;
  3459. default:
  3460. B43_WARN_ON(1);
  3461. }
  3462. if (unsupported) {
  3463. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  3464. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  3465. radio_manuf, radio_ver, radio_rev);
  3466. return -EOPNOTSUPP;
  3467. }
  3468. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  3469. radio_manuf, radio_ver, radio_rev);
  3470. phy->radio_manuf = radio_manuf;
  3471. phy->radio_ver = radio_ver;
  3472. phy->radio_rev = radio_rev;
  3473. phy->analog = analog_type;
  3474. phy->type = phy_type;
  3475. phy->rev = phy_rev;
  3476. return 0;
  3477. }
  3478. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  3479. struct b43_phy *phy)
  3480. {
  3481. phy->hardware_power_control = !!modparam_hwpctl;
  3482. phy->next_txpwr_check_time = jiffies;
  3483. /* PHY TX errors counter. */
  3484. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  3485. #if B43_DEBUG
  3486. phy->phy_locked = 0;
  3487. phy->radio_locked = 0;
  3488. #endif
  3489. }
  3490. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  3491. {
  3492. dev->dfq_valid = 0;
  3493. /* Assume the radio is enabled. If it's not enabled, the state will
  3494. * immediately get fixed on the first periodic work run. */
  3495. dev->radio_hw_enable = 1;
  3496. /* Stats */
  3497. memset(&dev->stats, 0, sizeof(dev->stats));
  3498. setup_struct_phy_for_init(dev, &dev->phy);
  3499. /* IRQ related flags */
  3500. dev->irq_reason = 0;
  3501. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  3502. dev->irq_mask = B43_IRQ_MASKTEMPLATE;
  3503. if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
  3504. dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
  3505. dev->mac_suspended = 1;
  3506. /* Noise calculation context */
  3507. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  3508. }
  3509. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  3510. {
  3511. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  3512. u64 hf;
  3513. if (!modparam_btcoex)
  3514. return;
  3515. if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
  3516. return;
  3517. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  3518. return;
  3519. hf = b43_hf_read(dev);
  3520. if (sprom->boardflags_lo & B43_BFL_BTCMOD)
  3521. hf |= B43_HF_BTCOEXALT;
  3522. else
  3523. hf |= B43_HF_BTCOEX;
  3524. b43_hf_write(dev, hf);
  3525. }
  3526. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  3527. {
  3528. if (!modparam_btcoex)
  3529. return;
  3530. //TODO
  3531. }
  3532. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  3533. {
  3534. #ifdef CONFIG_SSB_DRIVER_PCICORE
  3535. struct ssb_bus *bus = dev->dev->bus;
  3536. u32 tmp;
  3537. if (bus->pcicore.dev &&
  3538. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  3539. bus->pcicore.dev->id.revision <= 5) {
  3540. /* IMCFGLO timeouts workaround. */
  3541. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  3542. tmp &= ~SSB_IMCFGLO_REQTO;
  3543. tmp &= ~SSB_IMCFGLO_SERTO;
  3544. switch (bus->bustype) {
  3545. case SSB_BUSTYPE_PCI:
  3546. case SSB_BUSTYPE_PCMCIA:
  3547. tmp |= 0x32;
  3548. break;
  3549. case SSB_BUSTYPE_SSB:
  3550. tmp |= 0x53;
  3551. break;
  3552. }
  3553. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  3554. }
  3555. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  3556. }
  3557. static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
  3558. {
  3559. u16 pu_delay;
  3560. /* The time value is in microseconds. */
  3561. if (dev->phy.type == B43_PHYTYPE_A)
  3562. pu_delay = 3700;
  3563. else
  3564. pu_delay = 1050;
  3565. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  3566. pu_delay = 500;
  3567. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  3568. pu_delay = max(pu_delay, (u16)2400);
  3569. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
  3570. }
  3571. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  3572. static void b43_set_pretbtt(struct b43_wldev *dev)
  3573. {
  3574. u16 pretbtt;
  3575. /* The time value is in microseconds. */
  3576. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
  3577. pretbtt = 2;
  3578. } else {
  3579. if (dev->phy.type == B43_PHYTYPE_A)
  3580. pretbtt = 120;
  3581. else
  3582. pretbtt = 250;
  3583. }
  3584. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
  3585. b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
  3586. }
  3587. /* Shutdown a wireless core */
  3588. /* Locking: wl->mutex */
  3589. static void b43_wireless_core_exit(struct b43_wldev *dev)
  3590. {
  3591. u32 macctl;
  3592. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  3593. if (b43_status(dev) != B43_STAT_INITIALIZED)
  3594. return;
  3595. b43_set_status(dev, B43_STAT_UNINIT);
  3596. /* Stop the microcode PSM. */
  3597. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  3598. macctl &= ~B43_MACCTL_PSM_RUN;
  3599. macctl |= B43_MACCTL_PSM_JMP0;
  3600. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  3601. if (!dev->suspend_in_progress) {
  3602. b43_leds_exit(dev);
  3603. b43_rng_exit(dev->wl);
  3604. }
  3605. b43_dma_free(dev);
  3606. b43_pio_free(dev);
  3607. b43_chip_exit(dev);
  3608. dev->phy.ops->switch_analog(dev, 0);
  3609. if (dev->wl->current_beacon) {
  3610. dev_kfree_skb_any(dev->wl->current_beacon);
  3611. dev->wl->current_beacon = NULL;
  3612. }
  3613. ssb_device_disable(dev->dev, 0);
  3614. ssb_bus_may_powerdown(dev->dev->bus);
  3615. }
  3616. /* Initialize a wireless core */
  3617. static int b43_wireless_core_init(struct b43_wldev *dev)
  3618. {
  3619. struct b43_wl *wl = dev->wl;
  3620. struct ssb_bus *bus = dev->dev->bus;
  3621. struct ssb_sprom *sprom = &bus->sprom;
  3622. struct b43_phy *phy = &dev->phy;
  3623. int err;
  3624. u64 hf;
  3625. u32 tmp;
  3626. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3627. err = ssb_bus_powerup(bus, 0);
  3628. if (err)
  3629. goto out;
  3630. if (!ssb_device_is_enabled(dev->dev)) {
  3631. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  3632. b43_wireless_core_reset(dev, tmp);
  3633. }
  3634. /* Reset all data structures. */
  3635. setup_struct_wldev_for_init(dev);
  3636. phy->ops->prepare_structs(dev);
  3637. /* Enable IRQ routing to this device. */
  3638. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  3639. b43_imcfglo_timeouts_workaround(dev);
  3640. b43_bluetooth_coext_disable(dev);
  3641. if (phy->ops->prepare_hardware) {
  3642. err = phy->ops->prepare_hardware(dev);
  3643. if (err)
  3644. goto err_busdown;
  3645. }
  3646. err = b43_chip_init(dev);
  3647. if (err)
  3648. goto err_busdown;
  3649. b43_shm_write16(dev, B43_SHM_SHARED,
  3650. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  3651. hf = b43_hf_read(dev);
  3652. if (phy->type == B43_PHYTYPE_G) {
  3653. hf |= B43_HF_SYMW;
  3654. if (phy->rev == 1)
  3655. hf |= B43_HF_GDCW;
  3656. if (sprom->boardflags_lo & B43_BFL_PACTRL)
  3657. hf |= B43_HF_OFDMPABOOST;
  3658. }
  3659. if (phy->radio_ver == 0x2050) {
  3660. if (phy->radio_rev == 6)
  3661. hf |= B43_HF_4318TSSI;
  3662. if (phy->radio_rev < 6)
  3663. hf |= B43_HF_VCORECALC;
  3664. }
  3665. if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
  3666. hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
  3667. #ifdef CONFIG_SSB_DRIVER_PCICORE
  3668. if ((bus->bustype == SSB_BUSTYPE_PCI) &&
  3669. (bus->pcicore.dev->id.revision <= 10))
  3670. hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
  3671. #endif
  3672. hf &= ~B43_HF_SKCFPUP;
  3673. b43_hf_write(dev, hf);
  3674. b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
  3675. B43_DEFAULT_LONG_RETRY_LIMIT);
  3676. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  3677. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  3678. /* Disable sending probe responses from firmware.
  3679. * Setting the MaxTime to one usec will always trigger
  3680. * a timeout, so we never send any probe resp.
  3681. * A timeout of zero is infinite. */
  3682. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  3683. b43_rate_memory_init(dev);
  3684. b43_set_phytxctl_defaults(dev);
  3685. /* Minimum Contention Window */
  3686. if (phy->type == B43_PHYTYPE_B) {
  3687. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  3688. } else {
  3689. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  3690. }
  3691. /* Maximum Contention Window */
  3692. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  3693. if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
  3694. dev->__using_pio_transfers = 1;
  3695. err = b43_pio_init(dev);
  3696. } else {
  3697. dev->__using_pio_transfers = 0;
  3698. err = b43_dma_init(dev);
  3699. }
  3700. if (err)
  3701. goto err_chip_exit;
  3702. b43_qos_init(dev);
  3703. b43_set_synth_pu_delay(dev, 1);
  3704. b43_bluetooth_coext_enable(dev);
  3705. ssb_bus_powerup(bus, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
  3706. b43_upload_card_macaddress(dev);
  3707. b43_security_init(dev);
  3708. if (!dev->suspend_in_progress)
  3709. b43_rng_init(wl);
  3710. b43_set_status(dev, B43_STAT_INITIALIZED);
  3711. if (!dev->suspend_in_progress)
  3712. b43_leds_init(dev);
  3713. out:
  3714. return err;
  3715. err_chip_exit:
  3716. b43_chip_exit(dev);
  3717. err_busdown:
  3718. ssb_bus_may_powerdown(bus);
  3719. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3720. return err;
  3721. }
  3722. static int b43_op_add_interface(struct ieee80211_hw *hw,
  3723. struct ieee80211_if_init_conf *conf)
  3724. {
  3725. struct b43_wl *wl = hw_to_b43_wl(hw);
  3726. struct b43_wldev *dev;
  3727. unsigned long flags;
  3728. int err = -EOPNOTSUPP;
  3729. /* TODO: allow WDS/AP devices to coexist */
  3730. if (conf->type != NL80211_IFTYPE_AP &&
  3731. conf->type != NL80211_IFTYPE_MESH_POINT &&
  3732. conf->type != NL80211_IFTYPE_STATION &&
  3733. conf->type != NL80211_IFTYPE_WDS &&
  3734. conf->type != NL80211_IFTYPE_ADHOC)
  3735. return -EOPNOTSUPP;
  3736. mutex_lock(&wl->mutex);
  3737. if (wl->operating)
  3738. goto out_mutex_unlock;
  3739. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  3740. dev = wl->current_dev;
  3741. wl->operating = 1;
  3742. wl->vif = conf->vif;
  3743. wl->if_type = conf->type;
  3744. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  3745. spin_lock_irqsave(&wl->irq_lock, flags);
  3746. b43_adjust_opmode(dev);
  3747. b43_set_pretbtt(dev);
  3748. b43_set_synth_pu_delay(dev, 0);
  3749. b43_upload_card_macaddress(dev);
  3750. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3751. err = 0;
  3752. out_mutex_unlock:
  3753. mutex_unlock(&wl->mutex);
  3754. return err;
  3755. }
  3756. static void b43_op_remove_interface(struct ieee80211_hw *hw,
  3757. struct ieee80211_if_init_conf *conf)
  3758. {
  3759. struct b43_wl *wl = hw_to_b43_wl(hw);
  3760. struct b43_wldev *dev = wl->current_dev;
  3761. unsigned long flags;
  3762. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3763. mutex_lock(&wl->mutex);
  3764. B43_WARN_ON(!wl->operating);
  3765. B43_WARN_ON(wl->vif != conf->vif);
  3766. wl->vif = NULL;
  3767. wl->operating = 0;
  3768. spin_lock_irqsave(&wl->irq_lock, flags);
  3769. b43_adjust_opmode(dev);
  3770. memset(wl->mac_addr, 0, ETH_ALEN);
  3771. b43_upload_card_macaddress(dev);
  3772. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3773. mutex_unlock(&wl->mutex);
  3774. }
  3775. static int b43_op_start(struct ieee80211_hw *hw)
  3776. {
  3777. struct b43_wl *wl = hw_to_b43_wl(hw);
  3778. struct b43_wldev *dev = wl->current_dev;
  3779. int did_init = 0;
  3780. int err = 0;
  3781. /* Kill all old instance specific information to make sure
  3782. * the card won't use it in the short timeframe between start
  3783. * and mac80211 reconfiguring it. */
  3784. memset(wl->bssid, 0, ETH_ALEN);
  3785. memset(wl->mac_addr, 0, ETH_ALEN);
  3786. wl->filter_flags = 0;
  3787. wl->radiotap_enabled = 0;
  3788. b43_qos_clear(wl);
  3789. wl->beacon0_uploaded = 0;
  3790. wl->beacon1_uploaded = 0;
  3791. wl->beacon_templates_virgin = 1;
  3792. wl->radio_enabled = 1;
  3793. mutex_lock(&wl->mutex);
  3794. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3795. err = b43_wireless_core_init(dev);
  3796. if (err)
  3797. goto out_mutex_unlock;
  3798. did_init = 1;
  3799. }
  3800. if (b43_status(dev) < B43_STAT_STARTED) {
  3801. err = b43_wireless_core_start(dev);
  3802. if (err) {
  3803. if (did_init)
  3804. b43_wireless_core_exit(dev);
  3805. goto out_mutex_unlock;
  3806. }
  3807. }
  3808. /* XXX: only do if device doesn't support rfkill irq */
  3809. wiphy_rfkill_start_polling(hw->wiphy);
  3810. out_mutex_unlock:
  3811. mutex_unlock(&wl->mutex);
  3812. return err;
  3813. }
  3814. static void b43_op_stop(struct ieee80211_hw *hw)
  3815. {
  3816. struct b43_wl *wl = hw_to_b43_wl(hw);
  3817. struct b43_wldev *dev = wl->current_dev;
  3818. cancel_work_sync(&(wl->beacon_update_trigger));
  3819. mutex_lock(&wl->mutex);
  3820. if (b43_status(dev) >= B43_STAT_STARTED)
  3821. b43_wireless_core_stop(dev);
  3822. b43_wireless_core_exit(dev);
  3823. wl->radio_enabled = 0;
  3824. mutex_unlock(&wl->mutex);
  3825. cancel_work_sync(&(wl->txpower_adjust_work));
  3826. }
  3827. static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
  3828. struct ieee80211_sta *sta, bool set)
  3829. {
  3830. struct b43_wl *wl = hw_to_b43_wl(hw);
  3831. unsigned long flags;
  3832. spin_lock_irqsave(&wl->irq_lock, flags);
  3833. b43_update_templates(wl);
  3834. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3835. return 0;
  3836. }
  3837. static void b43_op_sta_notify(struct ieee80211_hw *hw,
  3838. struct ieee80211_vif *vif,
  3839. enum sta_notify_cmd notify_cmd,
  3840. struct ieee80211_sta *sta)
  3841. {
  3842. struct b43_wl *wl = hw_to_b43_wl(hw);
  3843. B43_WARN_ON(!vif || wl->vif != vif);
  3844. }
  3845. static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
  3846. {
  3847. struct b43_wl *wl = hw_to_b43_wl(hw);
  3848. struct b43_wldev *dev;
  3849. mutex_lock(&wl->mutex);
  3850. dev = wl->current_dev;
  3851. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
  3852. /* Disable CFP update during scan on other channels. */
  3853. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
  3854. }
  3855. mutex_unlock(&wl->mutex);
  3856. }
  3857. static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
  3858. {
  3859. struct b43_wl *wl = hw_to_b43_wl(hw);
  3860. struct b43_wldev *dev;
  3861. mutex_lock(&wl->mutex);
  3862. dev = wl->current_dev;
  3863. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
  3864. /* Re-enable CFP update. */
  3865. b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
  3866. }
  3867. mutex_unlock(&wl->mutex);
  3868. }
  3869. static const struct ieee80211_ops b43_hw_ops = {
  3870. .tx = b43_op_tx,
  3871. .conf_tx = b43_op_conf_tx,
  3872. .add_interface = b43_op_add_interface,
  3873. .remove_interface = b43_op_remove_interface,
  3874. .config = b43_op_config,
  3875. .bss_info_changed = b43_op_bss_info_changed,
  3876. .configure_filter = b43_op_configure_filter,
  3877. .set_key = b43_op_set_key,
  3878. .get_stats = b43_op_get_stats,
  3879. .get_tx_stats = b43_op_get_tx_stats,
  3880. .get_tsf = b43_op_get_tsf,
  3881. .set_tsf = b43_op_set_tsf,
  3882. .start = b43_op_start,
  3883. .stop = b43_op_stop,
  3884. .set_tim = b43_op_beacon_set_tim,
  3885. .sta_notify = b43_op_sta_notify,
  3886. .sw_scan_start = b43_op_sw_scan_start_notifier,
  3887. .sw_scan_complete = b43_op_sw_scan_complete_notifier,
  3888. .rfkill_poll = b43_rfkill_poll,
  3889. };
  3890. /* Hard-reset the chip. Do not call this directly.
  3891. * Use b43_controller_restart()
  3892. */
  3893. static void b43_chip_reset(struct work_struct *work)
  3894. {
  3895. struct b43_wldev *dev =
  3896. container_of(work, struct b43_wldev, restart_work);
  3897. struct b43_wl *wl = dev->wl;
  3898. int err = 0;
  3899. int prev_status;
  3900. mutex_lock(&wl->mutex);
  3901. prev_status = b43_status(dev);
  3902. /* Bring the device down... */
  3903. if (prev_status >= B43_STAT_STARTED)
  3904. b43_wireless_core_stop(dev);
  3905. if (prev_status >= B43_STAT_INITIALIZED)
  3906. b43_wireless_core_exit(dev);
  3907. /* ...and up again. */
  3908. if (prev_status >= B43_STAT_INITIALIZED) {
  3909. err = b43_wireless_core_init(dev);
  3910. if (err)
  3911. goto out;
  3912. }
  3913. if (prev_status >= B43_STAT_STARTED) {
  3914. err = b43_wireless_core_start(dev);
  3915. if (err) {
  3916. b43_wireless_core_exit(dev);
  3917. goto out;
  3918. }
  3919. }
  3920. out:
  3921. if (err)
  3922. wl->current_dev = NULL; /* Failed to init the dev. */
  3923. mutex_unlock(&wl->mutex);
  3924. if (err)
  3925. b43err(wl, "Controller restart FAILED\n");
  3926. else
  3927. b43info(wl, "Controller restarted\n");
  3928. }
  3929. static int b43_setup_bands(struct b43_wldev *dev,
  3930. bool have_2ghz_phy, bool have_5ghz_phy)
  3931. {
  3932. struct ieee80211_hw *hw = dev->wl->hw;
  3933. if (have_2ghz_phy)
  3934. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
  3935. if (dev->phy.type == B43_PHYTYPE_N) {
  3936. if (have_5ghz_phy)
  3937. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
  3938. } else {
  3939. if (have_5ghz_phy)
  3940. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
  3941. }
  3942. dev->phy.supports_2ghz = have_2ghz_phy;
  3943. dev->phy.supports_5ghz = have_5ghz_phy;
  3944. return 0;
  3945. }
  3946. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3947. {
  3948. /* We release firmware that late to not be required to re-request
  3949. * is all the time when we reinit the core. */
  3950. b43_release_firmware(dev);
  3951. b43_phy_free(dev);
  3952. }
  3953. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3954. {
  3955. struct b43_wl *wl = dev->wl;
  3956. struct ssb_bus *bus = dev->dev->bus;
  3957. struct pci_dev *pdev = bus->host_pci;
  3958. int err;
  3959. bool have_2ghz_phy = 0, have_5ghz_phy = 0;
  3960. u32 tmp;
  3961. /* Do NOT do any device initialization here.
  3962. * Do it in wireless_core_init() instead.
  3963. * This function is for gathering basic information about the HW, only.
  3964. * Also some structs may be set up here. But most likely you want to have
  3965. * that in core_init(), too.
  3966. */
  3967. err = ssb_bus_powerup(bus, 0);
  3968. if (err) {
  3969. b43err(wl, "Bus powerup failed\n");
  3970. goto out;
  3971. }
  3972. /* Get the PHY type. */
  3973. if (dev->dev->id.revision >= 5) {
  3974. u32 tmshigh;
  3975. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3976. have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
  3977. have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
  3978. } else
  3979. B43_WARN_ON(1);
  3980. dev->phy.gmode = have_2ghz_phy;
  3981. dev->phy.radio_on = 1;
  3982. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3983. b43_wireless_core_reset(dev, tmp);
  3984. err = b43_phy_versioning(dev);
  3985. if (err)
  3986. goto err_powerdown;
  3987. /* Check if this device supports multiband. */
  3988. if (!pdev ||
  3989. (pdev->device != 0x4312 &&
  3990. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3991. /* No multiband support. */
  3992. have_2ghz_phy = 0;
  3993. have_5ghz_phy = 0;
  3994. switch (dev->phy.type) {
  3995. case B43_PHYTYPE_A:
  3996. have_5ghz_phy = 1;
  3997. break;
  3998. case B43_PHYTYPE_G:
  3999. case B43_PHYTYPE_N:
  4000. case B43_PHYTYPE_LP:
  4001. have_2ghz_phy = 1;
  4002. break;
  4003. default:
  4004. B43_WARN_ON(1);
  4005. }
  4006. }
  4007. if (dev->phy.type == B43_PHYTYPE_A) {
  4008. /* FIXME */
  4009. b43err(wl, "IEEE 802.11a devices are unsupported\n");
  4010. err = -EOPNOTSUPP;
  4011. goto err_powerdown;
  4012. }
  4013. if (1 /* disable A-PHY */) {
  4014. /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
  4015. if (dev->phy.type != B43_PHYTYPE_N) {
  4016. have_2ghz_phy = 1;
  4017. have_5ghz_phy = 0;
  4018. }
  4019. }
  4020. err = b43_phy_allocate(dev);
  4021. if (err)
  4022. goto err_powerdown;
  4023. dev->phy.gmode = have_2ghz_phy;
  4024. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  4025. b43_wireless_core_reset(dev, tmp);
  4026. err = b43_validate_chipaccess(dev);
  4027. if (err)
  4028. goto err_phy_free;
  4029. err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
  4030. if (err)
  4031. goto err_phy_free;
  4032. /* Now set some default "current_dev" */
  4033. if (!wl->current_dev)
  4034. wl->current_dev = dev;
  4035. INIT_WORK(&dev->restart_work, b43_chip_reset);
  4036. dev->phy.ops->switch_analog(dev, 0);
  4037. ssb_device_disable(dev->dev, 0);
  4038. ssb_bus_may_powerdown(bus);
  4039. out:
  4040. return err;
  4041. err_phy_free:
  4042. b43_phy_free(dev);
  4043. err_powerdown:
  4044. ssb_bus_may_powerdown(bus);
  4045. return err;
  4046. }
  4047. static void b43_one_core_detach(struct ssb_device *dev)
  4048. {
  4049. struct b43_wldev *wldev;
  4050. struct b43_wl *wl;
  4051. /* Do not cancel ieee80211-workqueue based work here.
  4052. * See comment in b43_remove(). */
  4053. wldev = ssb_get_drvdata(dev);
  4054. wl = wldev->wl;
  4055. b43_debugfs_remove_device(wldev);
  4056. b43_wireless_core_detach(wldev);
  4057. list_del(&wldev->list);
  4058. wl->nr_devs--;
  4059. ssb_set_drvdata(dev, NULL);
  4060. kfree(wldev);
  4061. }
  4062. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  4063. {
  4064. struct b43_wldev *wldev;
  4065. struct pci_dev *pdev;
  4066. int err = -ENOMEM;
  4067. if (!list_empty(&wl->devlist)) {
  4068. /* We are not the first core on this chip. */
  4069. pdev = dev->bus->host_pci;
  4070. /* Only special chips support more than one wireless
  4071. * core, although some of the other chips have more than
  4072. * one wireless core as well. Check for this and
  4073. * bail out early.
  4074. */
  4075. if (!pdev ||
  4076. ((pdev->device != 0x4321) &&
  4077. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  4078. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  4079. return -ENODEV;
  4080. }
  4081. }
  4082. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  4083. if (!wldev)
  4084. goto out;
  4085. wldev->dev = dev;
  4086. wldev->wl = wl;
  4087. b43_set_status(wldev, B43_STAT_UNINIT);
  4088. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  4089. tasklet_init(&wldev->isr_tasklet,
  4090. (void (*)(unsigned long))b43_interrupt_tasklet,
  4091. (unsigned long)wldev);
  4092. INIT_LIST_HEAD(&wldev->list);
  4093. err = b43_wireless_core_attach(wldev);
  4094. if (err)
  4095. goto err_kfree_wldev;
  4096. list_add(&wldev->list, &wl->devlist);
  4097. wl->nr_devs++;
  4098. ssb_set_drvdata(dev, wldev);
  4099. b43_debugfs_add_device(wldev);
  4100. out:
  4101. return err;
  4102. err_kfree_wldev:
  4103. kfree(wldev);
  4104. return err;
  4105. }
  4106. #define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
  4107. (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
  4108. (pdev->device == _device) && \
  4109. (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
  4110. (pdev->subsystem_device == _subdevice) )
  4111. static void b43_sprom_fixup(struct ssb_bus *bus)
  4112. {
  4113. struct pci_dev *pdev;
  4114. /* boardflags workarounds */
  4115. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  4116. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  4117. bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
  4118. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  4119. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  4120. bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
  4121. if (bus->bustype == SSB_BUSTYPE_PCI) {
  4122. pdev = bus->host_pci;
  4123. if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
  4124. IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
  4125. IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
  4126. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
  4127. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
  4128. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
  4129. IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
  4130. bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
  4131. }
  4132. }
  4133. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  4134. {
  4135. struct ieee80211_hw *hw = wl->hw;
  4136. ssb_set_devtypedata(dev, NULL);
  4137. ieee80211_free_hw(hw);
  4138. }
  4139. static int b43_wireless_init(struct ssb_device *dev)
  4140. {
  4141. struct ssb_sprom *sprom = &dev->bus->sprom;
  4142. struct ieee80211_hw *hw;
  4143. struct b43_wl *wl;
  4144. int err = -ENOMEM;
  4145. b43_sprom_fixup(dev->bus);
  4146. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  4147. if (!hw) {
  4148. b43err(NULL, "Could not allocate ieee80211 device\n");
  4149. goto out;
  4150. }
  4151. wl = hw_to_b43_wl(hw);
  4152. /* fill hw info */
  4153. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  4154. IEEE80211_HW_SIGNAL_DBM |
  4155. IEEE80211_HW_NOISE_DBM;
  4156. hw->wiphy->interface_modes =
  4157. BIT(NL80211_IFTYPE_AP) |
  4158. BIT(NL80211_IFTYPE_MESH_POINT) |
  4159. BIT(NL80211_IFTYPE_STATION) |
  4160. BIT(NL80211_IFTYPE_WDS) |
  4161. BIT(NL80211_IFTYPE_ADHOC);
  4162. hw->queues = modparam_qos ? 4 : 1;
  4163. wl->mac80211_initially_registered_queues = hw->queues;
  4164. hw->max_rates = 2;
  4165. SET_IEEE80211_DEV(hw, dev->dev);
  4166. if (is_valid_ether_addr(sprom->et1mac))
  4167. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  4168. else
  4169. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  4170. /* Initialize struct b43_wl */
  4171. wl->hw = hw;
  4172. spin_lock_init(&wl->irq_lock);
  4173. rwlock_init(&wl->tx_lock);
  4174. spin_lock_init(&wl->leds_lock);
  4175. spin_lock_init(&wl->shm_lock);
  4176. mutex_init(&wl->mutex);
  4177. INIT_LIST_HEAD(&wl->devlist);
  4178. INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
  4179. INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
  4180. ssb_set_devtypedata(dev, wl);
  4181. b43info(wl, "Broadcom %04X WLAN found (core revision %u)\n",
  4182. dev->bus->chip_id, dev->id.revision);
  4183. err = 0;
  4184. out:
  4185. return err;
  4186. }
  4187. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  4188. {
  4189. struct b43_wl *wl;
  4190. int err;
  4191. int first = 0;
  4192. wl = ssb_get_devtypedata(dev);
  4193. if (!wl) {
  4194. /* Probing the first core. Must setup common struct b43_wl */
  4195. first = 1;
  4196. err = b43_wireless_init(dev);
  4197. if (err)
  4198. goto out;
  4199. wl = ssb_get_devtypedata(dev);
  4200. B43_WARN_ON(!wl);
  4201. }
  4202. err = b43_one_core_attach(dev, wl);
  4203. if (err)
  4204. goto err_wireless_exit;
  4205. if (first) {
  4206. err = ieee80211_register_hw(wl->hw);
  4207. if (err)
  4208. goto err_one_core_detach;
  4209. }
  4210. out:
  4211. return err;
  4212. err_one_core_detach:
  4213. b43_one_core_detach(dev);
  4214. err_wireless_exit:
  4215. if (first)
  4216. b43_wireless_exit(dev, wl);
  4217. return err;
  4218. }
  4219. static void b43_remove(struct ssb_device *dev)
  4220. {
  4221. struct b43_wl *wl = ssb_get_devtypedata(dev);
  4222. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4223. /* We must cancel any work here before unregistering from ieee80211,
  4224. * as the ieee80211 unreg will destroy the workqueue. */
  4225. cancel_work_sync(&wldev->restart_work);
  4226. B43_WARN_ON(!wl);
  4227. if (wl->current_dev == wldev) {
  4228. /* Restore the queues count before unregistering, because firmware detect
  4229. * might have modified it. Restoring is important, so the networking
  4230. * stack can properly free resources. */
  4231. wl->hw->queues = wl->mac80211_initially_registered_queues;
  4232. ieee80211_unregister_hw(wl->hw);
  4233. }
  4234. b43_one_core_detach(dev);
  4235. if (list_empty(&wl->devlist)) {
  4236. /* Last core on the chip unregistered.
  4237. * We can destroy common struct b43_wl.
  4238. */
  4239. b43_wireless_exit(dev, wl);
  4240. }
  4241. }
  4242. /* Perform a hardware reset. This can be called from any context. */
  4243. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  4244. {
  4245. /* Must avoid requeueing, if we are in shutdown. */
  4246. if (b43_status(dev) < B43_STAT_INITIALIZED)
  4247. return;
  4248. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  4249. ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
  4250. }
  4251. #ifdef CONFIG_PM
  4252. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  4253. {
  4254. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4255. struct b43_wl *wl = wldev->wl;
  4256. b43dbg(wl, "Suspending...\n");
  4257. mutex_lock(&wl->mutex);
  4258. wldev->suspend_in_progress = true;
  4259. wldev->suspend_init_status = b43_status(wldev);
  4260. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  4261. b43_wireless_core_stop(wldev);
  4262. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  4263. b43_wireless_core_exit(wldev);
  4264. mutex_unlock(&wl->mutex);
  4265. b43dbg(wl, "Device suspended.\n");
  4266. return 0;
  4267. }
  4268. static int b43_resume(struct ssb_device *dev)
  4269. {
  4270. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4271. struct b43_wl *wl = wldev->wl;
  4272. int err = 0;
  4273. b43dbg(wl, "Resuming...\n");
  4274. mutex_lock(&wl->mutex);
  4275. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  4276. err = b43_wireless_core_init(wldev);
  4277. if (err) {
  4278. b43err(wl, "Resume failed at core init\n");
  4279. goto out;
  4280. }
  4281. }
  4282. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  4283. err = b43_wireless_core_start(wldev);
  4284. if (err) {
  4285. b43_leds_exit(wldev);
  4286. b43_rng_exit(wldev->wl);
  4287. b43_wireless_core_exit(wldev);
  4288. b43err(wl, "Resume failed at core start\n");
  4289. goto out;
  4290. }
  4291. }
  4292. b43dbg(wl, "Device resumed.\n");
  4293. out:
  4294. wldev->suspend_in_progress = false;
  4295. mutex_unlock(&wl->mutex);
  4296. return err;
  4297. }
  4298. #else /* CONFIG_PM */
  4299. # define b43_suspend NULL
  4300. # define b43_resume NULL
  4301. #endif /* CONFIG_PM */
  4302. static struct ssb_driver b43_ssb_driver = {
  4303. .name = KBUILD_MODNAME,
  4304. .id_table = b43_ssb_tbl,
  4305. .probe = b43_probe,
  4306. .remove = b43_remove,
  4307. .suspend = b43_suspend,
  4308. .resume = b43_resume,
  4309. };
  4310. static void b43_print_driverinfo(void)
  4311. {
  4312. const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
  4313. *feat_leds = "";
  4314. #ifdef CONFIG_B43_PCI_AUTOSELECT
  4315. feat_pci = "P";
  4316. #endif
  4317. #ifdef CONFIG_B43_PCMCIA
  4318. feat_pcmcia = "M";
  4319. #endif
  4320. #ifdef CONFIG_B43_NPHY
  4321. feat_nphy = "N";
  4322. #endif
  4323. #ifdef CONFIG_B43_LEDS
  4324. feat_leds = "L";
  4325. #endif
  4326. printk(KERN_INFO "Broadcom 43xx driver loaded "
  4327. "[ Features: %s%s%s%s, Firmware-ID: "
  4328. B43_SUPPORTED_FIRMWARE_ID " ]\n",
  4329. feat_pci, feat_pcmcia, feat_nphy,
  4330. feat_leds);
  4331. }
  4332. static int __init b43_init(void)
  4333. {
  4334. int err;
  4335. b43_debugfs_init();
  4336. err = b43_pcmcia_init();
  4337. if (err)
  4338. goto err_dfs_exit;
  4339. err = ssb_driver_register(&b43_ssb_driver);
  4340. if (err)
  4341. goto err_pcmcia_exit;
  4342. b43_print_driverinfo();
  4343. return err;
  4344. err_pcmcia_exit:
  4345. b43_pcmcia_exit();
  4346. err_dfs_exit:
  4347. b43_debugfs_exit();
  4348. return err;
  4349. }
  4350. static void __exit b43_exit(void)
  4351. {
  4352. ssb_driver_unregister(&b43_ssb_driver);
  4353. b43_pcmcia_exit();
  4354. b43_debugfs_exit();
  4355. }
  4356. module_init(b43_init)
  4357. module_exit(b43_exit)