main.c 129 KB

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