main.c 103 KB

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