main.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053
  1. /*
  2. *
  3. * Broadcom B43legacy wireless driver
  4. *
  5. * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
  6. * Copyright (c) 2005-2008 Stefano Brivio <stefano.brivio@polimi.it>
  7. * Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  8. * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  9. * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  10. * Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
  11. *
  12. * Some parts of the code in this file are derived from the ipw2200
  13. * driver Copyright(c) 2003 - 2004 Intel Corporation.
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; see the file COPYING. If not, write to
  26. * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  27. * Boston, MA 02110-1301, USA.
  28. *
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/firmware.h>
  36. #include <linux/wireless.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/dma-mapping.h>
  40. #include <net/dst.h>
  41. #include <asm/unaligned.h>
  42. #include "b43legacy.h"
  43. #include "main.h"
  44. #include "debugfs.h"
  45. #include "phy.h"
  46. #include "dma.h"
  47. #include "pio.h"
  48. #include "sysfs.h"
  49. #include "xmit.h"
  50. #include "radio.h"
  51. MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
  52. MODULE_AUTHOR("Martin Langer");
  53. MODULE_AUTHOR("Stefano Brivio");
  54. MODULE_AUTHOR("Michael Buesch");
  55. MODULE_LICENSE("GPL");
  56. MODULE_FIRMWARE(B43legacy_SUPPORTED_FIRMWARE_ID);
  57. #if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
  58. static int modparam_pio;
  59. module_param_named(pio, modparam_pio, int, 0444);
  60. MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
  61. #elif defined(CONFIG_B43LEGACY_DMA)
  62. # define modparam_pio 0
  63. #elif defined(CONFIG_B43LEGACY_PIO)
  64. # define modparam_pio 1
  65. #endif
  66. static int modparam_bad_frames_preempt;
  67. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  68. MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
  69. " Preemption");
  70. static char modparam_fwpostfix[16];
  71. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  72. MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
  73. /* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
  74. static const struct ssb_device_id b43legacy_ssb_tbl[] = {
  75. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
  76. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
  77. SSB_DEVTABLE_END
  78. };
  79. MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
  80. /* Channel and ratetables are shared for all devices.
  81. * They can't be const, because ieee80211 puts some precalculated
  82. * data in there. This data is the same for all devices, so we don't
  83. * get concurrency issues */
  84. #define RATETAB_ENT(_rateid, _flags) \
  85. { \
  86. .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
  87. .hw_value = (_rateid), \
  88. .flags = (_flags), \
  89. }
  90. /*
  91. * NOTE: When changing this, sync with xmit.c's
  92. * b43legacy_plcp_get_bitrate_idx_* functions!
  93. */
  94. static struct ieee80211_rate __b43legacy_ratetable[] = {
  95. RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
  96. RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
  97. RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
  98. RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
  99. RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
  100. RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
  101. RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
  102. RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
  103. RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
  104. RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
  105. RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
  106. RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
  107. };
  108. #define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
  109. #define b43legacy_b_ratetable_size 4
  110. #define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
  111. #define b43legacy_g_ratetable_size 12
  112. #define CHANTAB_ENT(_chanid, _freq) \
  113. { \
  114. .center_freq = (_freq), \
  115. .hw_value = (_chanid), \
  116. }
  117. static struct ieee80211_channel b43legacy_bg_chantable[] = {
  118. CHANTAB_ENT(1, 2412),
  119. CHANTAB_ENT(2, 2417),
  120. CHANTAB_ENT(3, 2422),
  121. CHANTAB_ENT(4, 2427),
  122. CHANTAB_ENT(5, 2432),
  123. CHANTAB_ENT(6, 2437),
  124. CHANTAB_ENT(7, 2442),
  125. CHANTAB_ENT(8, 2447),
  126. CHANTAB_ENT(9, 2452),
  127. CHANTAB_ENT(10, 2457),
  128. CHANTAB_ENT(11, 2462),
  129. CHANTAB_ENT(12, 2467),
  130. CHANTAB_ENT(13, 2472),
  131. CHANTAB_ENT(14, 2484),
  132. };
  133. static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
  134. .channels = b43legacy_bg_chantable,
  135. .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
  136. .bitrates = b43legacy_b_ratetable,
  137. .n_bitrates = b43legacy_b_ratetable_size,
  138. };
  139. static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
  140. .channels = b43legacy_bg_chantable,
  141. .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
  142. .bitrates = b43legacy_g_ratetable,
  143. .n_bitrates = b43legacy_g_ratetable_size,
  144. };
  145. static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
  146. static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
  147. static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
  148. static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
  149. static int b43legacy_ratelimit(struct b43legacy_wl *wl)
  150. {
  151. if (!wl || !wl->current_dev)
  152. return 1;
  153. if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
  154. return 1;
  155. /* We are up and running.
  156. * Ratelimit the messages to avoid DoS over the net. */
  157. return net_ratelimit();
  158. }
  159. void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
  160. {
  161. va_list args;
  162. if (!b43legacy_ratelimit(wl))
  163. return;
  164. va_start(args, fmt);
  165. printk(KERN_INFO "b43legacy-%s: ",
  166. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  167. vprintk(fmt, args);
  168. va_end(args);
  169. }
  170. void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
  171. {
  172. va_list args;
  173. if (!b43legacy_ratelimit(wl))
  174. return;
  175. va_start(args, fmt);
  176. printk(KERN_ERR "b43legacy-%s ERROR: ",
  177. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  178. vprintk(fmt, args);
  179. va_end(args);
  180. }
  181. void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
  182. {
  183. va_list args;
  184. if (!b43legacy_ratelimit(wl))
  185. return;
  186. va_start(args, fmt);
  187. printk(KERN_WARNING "b43legacy-%s warning: ",
  188. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  189. vprintk(fmt, args);
  190. va_end(args);
  191. }
  192. #if B43legacy_DEBUG
  193. void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
  194. {
  195. va_list args;
  196. va_start(args, fmt);
  197. printk(KERN_DEBUG "b43legacy-%s debug: ",
  198. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  199. vprintk(fmt, args);
  200. va_end(args);
  201. }
  202. #endif /* DEBUG */
  203. static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
  204. u32 val)
  205. {
  206. u32 status;
  207. B43legacy_WARN_ON(offset % 4 != 0);
  208. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  209. if (status & B43legacy_MACCTL_BE)
  210. val = swab32(val);
  211. b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
  212. mmiowb();
  213. b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
  214. }
  215. static inline
  216. void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
  217. u16 routing, u16 offset)
  218. {
  219. u32 control;
  220. /* "offset" is the WORD offset. */
  221. control = routing;
  222. control <<= 16;
  223. control |= offset;
  224. b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
  225. }
  226. u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
  227. u16 routing, u16 offset)
  228. {
  229. u32 ret;
  230. if (routing == B43legacy_SHM_SHARED) {
  231. B43legacy_WARN_ON((offset & 0x0001) != 0);
  232. if (offset & 0x0003) {
  233. /* Unaligned access */
  234. b43legacy_shm_control_word(dev, routing, offset >> 2);
  235. ret = b43legacy_read16(dev,
  236. B43legacy_MMIO_SHM_DATA_UNALIGNED);
  237. ret <<= 16;
  238. b43legacy_shm_control_word(dev, routing,
  239. (offset >> 2) + 1);
  240. ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
  241. return ret;
  242. }
  243. offset >>= 2;
  244. }
  245. b43legacy_shm_control_word(dev, routing, offset);
  246. ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
  247. return ret;
  248. }
  249. u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
  250. u16 routing, u16 offset)
  251. {
  252. u16 ret;
  253. if (routing == B43legacy_SHM_SHARED) {
  254. B43legacy_WARN_ON((offset & 0x0001) != 0);
  255. if (offset & 0x0003) {
  256. /* Unaligned access */
  257. b43legacy_shm_control_word(dev, routing, offset >> 2);
  258. ret = b43legacy_read16(dev,
  259. B43legacy_MMIO_SHM_DATA_UNALIGNED);
  260. return ret;
  261. }
  262. offset >>= 2;
  263. }
  264. b43legacy_shm_control_word(dev, routing, offset);
  265. ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
  266. return ret;
  267. }
  268. void b43legacy_shm_write32(struct b43legacy_wldev *dev,
  269. u16 routing, u16 offset,
  270. u32 value)
  271. {
  272. if (routing == B43legacy_SHM_SHARED) {
  273. B43legacy_WARN_ON((offset & 0x0001) != 0);
  274. if (offset & 0x0003) {
  275. /* Unaligned access */
  276. b43legacy_shm_control_word(dev, routing, offset >> 2);
  277. mmiowb();
  278. b43legacy_write16(dev,
  279. B43legacy_MMIO_SHM_DATA_UNALIGNED,
  280. (value >> 16) & 0xffff);
  281. mmiowb();
  282. b43legacy_shm_control_word(dev, routing,
  283. (offset >> 2) + 1);
  284. mmiowb();
  285. b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
  286. value & 0xffff);
  287. return;
  288. }
  289. offset >>= 2;
  290. }
  291. b43legacy_shm_control_word(dev, routing, offset);
  292. mmiowb();
  293. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
  294. }
  295. void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
  296. u16 value)
  297. {
  298. if (routing == B43legacy_SHM_SHARED) {
  299. B43legacy_WARN_ON((offset & 0x0001) != 0);
  300. if (offset & 0x0003) {
  301. /* Unaligned access */
  302. b43legacy_shm_control_word(dev, routing, offset >> 2);
  303. mmiowb();
  304. b43legacy_write16(dev,
  305. B43legacy_MMIO_SHM_DATA_UNALIGNED,
  306. value);
  307. return;
  308. }
  309. offset >>= 2;
  310. }
  311. b43legacy_shm_control_word(dev, routing, offset);
  312. mmiowb();
  313. b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
  314. }
  315. /* Read HostFlags */
  316. u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
  317. {
  318. u32 ret;
  319. ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  320. B43legacy_SHM_SH_HOSTFHI);
  321. ret <<= 16;
  322. ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  323. B43legacy_SHM_SH_HOSTFLO);
  324. return ret;
  325. }
  326. /* Write HostFlags */
  327. void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
  328. {
  329. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  330. B43legacy_SHM_SH_HOSTFLO,
  331. (value & 0x0000FFFF));
  332. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  333. B43legacy_SHM_SH_HOSTFHI,
  334. ((value & 0xFFFF0000) >> 16));
  335. }
  336. void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
  337. {
  338. /* We need to be careful. As we read the TSF from multiple
  339. * registers, we should take care of register overflows.
  340. * In theory, the whole tsf read process should be atomic.
  341. * We try to be atomic here, by restaring the read process,
  342. * if any of the high registers changed (overflew).
  343. */
  344. if (dev->dev->id.revision >= 3) {
  345. u32 low;
  346. u32 high;
  347. u32 high2;
  348. do {
  349. high = b43legacy_read32(dev,
  350. B43legacy_MMIO_REV3PLUS_TSF_HIGH);
  351. low = b43legacy_read32(dev,
  352. B43legacy_MMIO_REV3PLUS_TSF_LOW);
  353. high2 = b43legacy_read32(dev,
  354. B43legacy_MMIO_REV3PLUS_TSF_HIGH);
  355. } while (unlikely(high != high2));
  356. *tsf = high;
  357. *tsf <<= 32;
  358. *tsf |= low;
  359. } else {
  360. u64 tmp;
  361. u16 v0;
  362. u16 v1;
  363. u16 v2;
  364. u16 v3;
  365. u16 test1;
  366. u16 test2;
  367. u16 test3;
  368. do {
  369. v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
  370. v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
  371. v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
  372. v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
  373. test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
  374. test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
  375. test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
  376. } while (v3 != test3 || v2 != test2 || v1 != test1);
  377. *tsf = v3;
  378. *tsf <<= 48;
  379. tmp = v2;
  380. tmp <<= 32;
  381. *tsf |= tmp;
  382. tmp = v1;
  383. tmp <<= 16;
  384. *tsf |= tmp;
  385. *tsf |= v0;
  386. }
  387. }
  388. static void b43legacy_time_lock(struct b43legacy_wldev *dev)
  389. {
  390. u32 status;
  391. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  392. status |= B43legacy_MACCTL_TBTTHOLD;
  393. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
  394. mmiowb();
  395. }
  396. static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
  397. {
  398. u32 status;
  399. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  400. status &= ~B43legacy_MACCTL_TBTTHOLD;
  401. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
  402. }
  403. static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
  404. {
  405. /* Be careful with the in-progress timer.
  406. * First zero out the low register, so we have a full
  407. * register-overflow duration to complete the operation.
  408. */
  409. if (dev->dev->id.revision >= 3) {
  410. u32 lo = (tsf & 0x00000000FFFFFFFFULL);
  411. u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
  412. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
  413. mmiowb();
  414. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
  415. hi);
  416. mmiowb();
  417. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
  418. lo);
  419. } else {
  420. u16 v0 = (tsf & 0x000000000000FFFFULL);
  421. u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
  422. u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
  423. u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
  424. b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
  425. mmiowb();
  426. b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
  427. mmiowb();
  428. b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
  429. mmiowb();
  430. b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
  431. mmiowb();
  432. b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
  433. }
  434. }
  435. void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
  436. {
  437. b43legacy_time_lock(dev);
  438. b43legacy_tsf_write_locked(dev, tsf);
  439. b43legacy_time_unlock(dev);
  440. }
  441. static
  442. void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
  443. u16 offset, const u8 *mac)
  444. {
  445. static const u8 zero_addr[ETH_ALEN] = { 0 };
  446. u16 data;
  447. if (!mac)
  448. mac = zero_addr;
  449. offset |= 0x0020;
  450. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
  451. data = mac[0];
  452. data |= mac[1] << 8;
  453. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  454. data = mac[2];
  455. data |= mac[3] << 8;
  456. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  457. data = mac[4];
  458. data |= mac[5] << 8;
  459. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  460. }
  461. static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
  462. {
  463. static const u8 zero_addr[ETH_ALEN] = { 0 };
  464. const u8 *mac = dev->wl->mac_addr;
  465. const u8 *bssid = dev->wl->bssid;
  466. u8 mac_bssid[ETH_ALEN * 2];
  467. int i;
  468. u32 tmp;
  469. if (!bssid)
  470. bssid = zero_addr;
  471. if (!mac)
  472. mac = zero_addr;
  473. b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
  474. memcpy(mac_bssid, mac, ETH_ALEN);
  475. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  476. /* Write our MAC address and BSSID to template ram */
  477. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  478. tmp = (u32)(mac_bssid[i + 0]);
  479. tmp |= (u32)(mac_bssid[i + 1]) << 8;
  480. tmp |= (u32)(mac_bssid[i + 2]) << 16;
  481. tmp |= (u32)(mac_bssid[i + 3]) << 24;
  482. b43legacy_ram_write(dev, 0x20 + i, tmp);
  483. b43legacy_ram_write(dev, 0x78 + i, tmp);
  484. b43legacy_ram_write(dev, 0x478 + i, tmp);
  485. }
  486. }
  487. static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
  488. {
  489. b43legacy_write_mac_bssid_templates(dev);
  490. b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
  491. dev->wl->mac_addr);
  492. }
  493. static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
  494. u16 slot_time)
  495. {
  496. /* slot_time is in usec. */
  497. if (dev->phy.type != B43legacy_PHYTYPE_G)
  498. return;
  499. b43legacy_write16(dev, 0x684, 510 + slot_time);
  500. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
  501. slot_time);
  502. }
  503. static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
  504. {
  505. b43legacy_set_slot_time(dev, 9);
  506. }
  507. static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
  508. {
  509. b43legacy_set_slot_time(dev, 20);
  510. }
  511. /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
  512. * Returns the _previously_ enabled IRQ mask.
  513. */
  514. static inline u32 b43legacy_interrupt_enable(struct b43legacy_wldev *dev,
  515. u32 mask)
  516. {
  517. u32 old_mask;
  518. old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
  519. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask |
  520. mask);
  521. return old_mask;
  522. }
  523. /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
  524. * Returns the _previously_ enabled IRQ mask.
  525. */
  526. static inline u32 b43legacy_interrupt_disable(struct b43legacy_wldev *dev,
  527. u32 mask)
  528. {
  529. u32 old_mask;
  530. old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
  531. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
  532. return old_mask;
  533. }
  534. /* Synchronize IRQ top- and bottom-half.
  535. * IRQs must be masked before calling this.
  536. * This must not be called with the irq_lock held.
  537. */
  538. static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
  539. {
  540. synchronize_irq(dev->dev->irq);
  541. tasklet_kill(&dev->isr_tasklet);
  542. }
  543. /* DummyTransmission function, as documented on
  544. * http://bcm-specs.sipsolutions.net/DummyTransmission
  545. */
  546. void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
  547. {
  548. struct b43legacy_phy *phy = &dev->phy;
  549. unsigned int i;
  550. unsigned int max_loop;
  551. u16 value;
  552. u32 buffer[5] = {
  553. 0x00000000,
  554. 0x00D40000,
  555. 0x00000000,
  556. 0x01000000,
  557. 0x00000000,
  558. };
  559. switch (phy->type) {
  560. case B43legacy_PHYTYPE_B:
  561. case B43legacy_PHYTYPE_G:
  562. max_loop = 0xFA;
  563. buffer[0] = 0x000B846E;
  564. break;
  565. default:
  566. B43legacy_BUG_ON(1);
  567. return;
  568. }
  569. for (i = 0; i < 5; i++)
  570. b43legacy_ram_write(dev, i * 4, buffer[i]);
  571. /* dummy read follows */
  572. b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  573. b43legacy_write16(dev, 0x0568, 0x0000);
  574. b43legacy_write16(dev, 0x07C0, 0x0000);
  575. b43legacy_write16(dev, 0x050C, 0x0000);
  576. b43legacy_write16(dev, 0x0508, 0x0000);
  577. b43legacy_write16(dev, 0x050A, 0x0000);
  578. b43legacy_write16(dev, 0x054C, 0x0000);
  579. b43legacy_write16(dev, 0x056A, 0x0014);
  580. b43legacy_write16(dev, 0x0568, 0x0826);
  581. b43legacy_write16(dev, 0x0500, 0x0000);
  582. b43legacy_write16(dev, 0x0502, 0x0030);
  583. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  584. b43legacy_radio_write16(dev, 0x0051, 0x0017);
  585. for (i = 0x00; i < max_loop; i++) {
  586. value = b43legacy_read16(dev, 0x050E);
  587. if (value & 0x0080)
  588. break;
  589. udelay(10);
  590. }
  591. for (i = 0x00; i < 0x0A; i++) {
  592. value = b43legacy_read16(dev, 0x050E);
  593. if (value & 0x0400)
  594. break;
  595. udelay(10);
  596. }
  597. for (i = 0x00; i < 0x0A; i++) {
  598. value = b43legacy_read16(dev, 0x0690);
  599. if (!(value & 0x0100))
  600. break;
  601. udelay(10);
  602. }
  603. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  604. b43legacy_radio_write16(dev, 0x0051, 0x0037);
  605. }
  606. /* Turn the Analog ON/OFF */
  607. static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
  608. {
  609. b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
  610. }
  611. void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
  612. {
  613. u32 tmslow;
  614. u32 macctl;
  615. flags |= B43legacy_TMSLOW_PHYCLKEN;
  616. flags |= B43legacy_TMSLOW_PHYRESET;
  617. ssb_device_enable(dev->dev, flags);
  618. msleep(2); /* Wait for the PLL to turn on. */
  619. /* Now take the PHY out of Reset again */
  620. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  621. tmslow |= SSB_TMSLOW_FGC;
  622. tmslow &= ~B43legacy_TMSLOW_PHYRESET;
  623. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  624. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  625. msleep(1);
  626. tmslow &= ~SSB_TMSLOW_FGC;
  627. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  628. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  629. msleep(1);
  630. /* Turn Analog ON */
  631. b43legacy_switch_analog(dev, 1);
  632. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  633. macctl &= ~B43legacy_MACCTL_GMODE;
  634. if (flags & B43legacy_TMSLOW_GMODE) {
  635. macctl |= B43legacy_MACCTL_GMODE;
  636. dev->phy.gmode = 1;
  637. } else
  638. dev->phy.gmode = 0;
  639. macctl |= B43legacy_MACCTL_IHR_ENABLED;
  640. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  641. }
  642. static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
  643. {
  644. u32 v0;
  645. u32 v1;
  646. u16 tmp;
  647. struct b43legacy_txstatus stat;
  648. while (1) {
  649. v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
  650. if (!(v0 & 0x00000001))
  651. break;
  652. v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
  653. stat.cookie = (v0 >> 16);
  654. stat.seq = (v1 & 0x0000FFFF);
  655. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  656. tmp = (v0 & 0x0000FFFF);
  657. stat.frame_count = ((tmp & 0xF000) >> 12);
  658. stat.rts_count = ((tmp & 0x0F00) >> 8);
  659. stat.supp_reason = ((tmp & 0x001C) >> 2);
  660. stat.pm_indicated = !!(tmp & 0x0080);
  661. stat.intermediate = !!(tmp & 0x0040);
  662. stat.for_ampdu = !!(tmp & 0x0020);
  663. stat.acked = !!(tmp & 0x0002);
  664. b43legacy_handle_txstatus(dev, &stat);
  665. }
  666. }
  667. static void drain_txstatus_queue(struct b43legacy_wldev *dev)
  668. {
  669. u32 dummy;
  670. if (dev->dev->id.revision < 5)
  671. return;
  672. /* Read all entries from the microcode TXstatus FIFO
  673. * and throw them away.
  674. */
  675. while (1) {
  676. dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
  677. if (!(dummy & 0x00000001))
  678. break;
  679. dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
  680. }
  681. }
  682. static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
  683. {
  684. u32 val = 0;
  685. val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
  686. val <<= 16;
  687. val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
  688. return val;
  689. }
  690. static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
  691. {
  692. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
  693. (jssi & 0x0000FFFF));
  694. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
  695. (jssi & 0xFFFF0000) >> 16);
  696. }
  697. static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
  698. {
  699. b43legacy_jssi_write(dev, 0x7F7F7F7F);
  700. b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
  701. b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
  702. | B43legacy_MACCMD_BGNOISE);
  703. B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
  704. dev->phy.channel);
  705. }
  706. static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
  707. {
  708. /* Top half of Link Quality calculation. */
  709. if (dev->noisecalc.calculation_running)
  710. return;
  711. dev->noisecalc.channel_at_start = dev->phy.channel;
  712. dev->noisecalc.calculation_running = 1;
  713. dev->noisecalc.nr_samples = 0;
  714. b43legacy_generate_noise_sample(dev);
  715. }
  716. static void handle_irq_noise(struct b43legacy_wldev *dev)
  717. {
  718. struct b43legacy_phy *phy = &dev->phy;
  719. u16 tmp;
  720. u8 noise[4];
  721. u8 i;
  722. u8 j;
  723. s32 average;
  724. /* Bottom half of Link Quality calculation. */
  725. B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
  726. if (dev->noisecalc.channel_at_start != phy->channel)
  727. goto drop_calculation;
  728. *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
  729. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  730. noise[2] == 0x7F || noise[3] == 0x7F)
  731. goto generate_new;
  732. /* Get the noise samples. */
  733. B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
  734. i = dev->noisecalc.nr_samples;
  735. noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  736. noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  737. noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  738. noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  739. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  740. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  741. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  742. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  743. dev->noisecalc.nr_samples++;
  744. if (dev->noisecalc.nr_samples == 8) {
  745. /* Calculate the Link Quality by the noise samples. */
  746. average = 0;
  747. for (i = 0; i < 8; i++) {
  748. for (j = 0; j < 4; j++)
  749. average += dev->noisecalc.samples[i][j];
  750. }
  751. average /= (8 * 4);
  752. average *= 125;
  753. average += 64;
  754. average /= 128;
  755. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  756. 0x40C);
  757. tmp = (tmp / 128) & 0x1F;
  758. if (tmp >= 8)
  759. average += 2;
  760. else
  761. average -= 25;
  762. if (tmp == 8)
  763. average -= 72;
  764. else
  765. average -= 48;
  766. dev->stats.link_noise = average;
  767. drop_calculation:
  768. dev->noisecalc.calculation_running = 0;
  769. return;
  770. }
  771. generate_new:
  772. b43legacy_generate_noise_sample(dev);
  773. }
  774. static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
  775. {
  776. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
  777. /* TODO: PS TBTT */
  778. } else {
  779. if (1/*FIXME: the last PSpoll frame was sent successfully */)
  780. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  781. }
  782. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  783. dev->dfq_valid = 1;
  784. }
  785. static void handle_irq_atim_end(struct b43legacy_wldev *dev)
  786. {
  787. if (dev->dfq_valid) {
  788. b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
  789. b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
  790. | B43legacy_MACCMD_DFQ_VALID);
  791. dev->dfq_valid = 0;
  792. }
  793. }
  794. static void handle_irq_pmq(struct b43legacy_wldev *dev)
  795. {
  796. u32 tmp;
  797. /* TODO: AP mode. */
  798. while (1) {
  799. tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
  800. if (!(tmp & 0x00000008))
  801. break;
  802. }
  803. /* 16bit write is odd, but correct. */
  804. b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
  805. }
  806. static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
  807. const u8 *data, u16 size,
  808. u16 ram_offset,
  809. u16 shm_size_offset, u8 rate)
  810. {
  811. u32 i;
  812. u32 tmp;
  813. struct b43legacy_plcp_hdr4 plcp;
  814. plcp.data = 0;
  815. b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  816. b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  817. ram_offset += sizeof(u32);
  818. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  819. * So leave the first two bytes of the next write blank.
  820. */
  821. tmp = (u32)(data[0]) << 16;
  822. tmp |= (u32)(data[1]) << 24;
  823. b43legacy_ram_write(dev, ram_offset, tmp);
  824. ram_offset += sizeof(u32);
  825. for (i = 2; i < size; i += sizeof(u32)) {
  826. tmp = (u32)(data[i + 0]);
  827. if (i + 1 < size)
  828. tmp |= (u32)(data[i + 1]) << 8;
  829. if (i + 2 < size)
  830. tmp |= (u32)(data[i + 2]) << 16;
  831. if (i + 3 < size)
  832. tmp |= (u32)(data[i + 3]) << 24;
  833. b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
  834. }
  835. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
  836. size + sizeof(struct b43legacy_plcp_hdr6));
  837. }
  838. /* Convert a b43legacy antenna number value to the PHY TX control value. */
  839. static u16 b43legacy_antenna_to_phyctl(int antenna)
  840. {
  841. switch (antenna) {
  842. case B43legacy_ANTENNA0:
  843. return B43legacy_TX4_PHY_ANT0;
  844. case B43legacy_ANTENNA1:
  845. return B43legacy_TX4_PHY_ANT1;
  846. }
  847. return B43legacy_TX4_PHY_ANTLAST;
  848. }
  849. static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
  850. u16 ram_offset,
  851. u16 shm_size_offset)
  852. {
  853. unsigned int i, len, variable_len;
  854. const struct ieee80211_mgmt *bcn;
  855. const u8 *ie;
  856. bool tim_found = 0;
  857. unsigned int rate;
  858. u16 ctl;
  859. int antenna;
  860. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
  861. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  862. len = min((size_t)dev->wl->current_beacon->len,
  863. 0x200 - sizeof(struct b43legacy_plcp_hdr6));
  864. rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
  865. b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
  866. shm_size_offset, rate);
  867. /* Write the PHY TX control parameters. */
  868. antenna = B43legacy_ANTENNA_DEFAULT;
  869. antenna = b43legacy_antenna_to_phyctl(antenna);
  870. ctl = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  871. B43legacy_SHM_SH_BEACPHYCTL);
  872. /* We can't send beacons with short preamble. Would get PHY errors. */
  873. ctl &= ~B43legacy_TX4_PHY_SHORTPRMBL;
  874. ctl &= ~B43legacy_TX4_PHY_ANT;
  875. ctl &= ~B43legacy_TX4_PHY_ENC;
  876. ctl |= antenna;
  877. ctl |= B43legacy_TX4_PHY_ENC_CCK;
  878. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  879. B43legacy_SHM_SH_BEACPHYCTL, ctl);
  880. /* Find the position of the TIM and the DTIM_period value
  881. * and write them to SHM. */
  882. ie = bcn->u.beacon.variable;
  883. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  884. for (i = 0; i < variable_len - 2; ) {
  885. uint8_t ie_id, ie_len;
  886. ie_id = ie[i];
  887. ie_len = ie[i + 1];
  888. if (ie_id == 5) {
  889. u16 tim_position;
  890. u16 dtim_period;
  891. /* This is the TIM Information Element */
  892. /* Check whether the ie_len is in the beacon data range. */
  893. if (variable_len < ie_len + 2 + i)
  894. break;
  895. /* A valid TIM is at least 4 bytes long. */
  896. if (ie_len < 4)
  897. break;
  898. tim_found = 1;
  899. tim_position = sizeof(struct b43legacy_plcp_hdr6);
  900. tim_position += offsetof(struct ieee80211_mgmt,
  901. u.beacon.variable);
  902. tim_position += i;
  903. dtim_period = ie[i + 3];
  904. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  905. B43legacy_SHM_SH_TIMPOS, tim_position);
  906. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  907. B43legacy_SHM_SH_DTIMP, dtim_period);
  908. break;
  909. }
  910. i += ie_len + 2;
  911. }
  912. if (!tim_found) {
  913. b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
  914. "beacon template packet. AP or IBSS operation "
  915. "may be broken.\n");
  916. } else
  917. b43legacydbg(dev->wl, "Updated beacon template\n");
  918. }
  919. static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
  920. u16 shm_offset, u16 size,
  921. struct ieee80211_rate *rate)
  922. {
  923. struct b43legacy_plcp_hdr4 plcp;
  924. u32 tmp;
  925. __le16 dur;
  926. plcp.data = 0;
  927. b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
  928. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  929. dev->wl->vif,
  930. size,
  931. rate);
  932. /* Write PLCP in two parts and timing for packet transfer */
  933. tmp = le32_to_cpu(plcp.data);
  934. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
  935. tmp & 0xFFFF);
  936. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
  937. tmp >> 16);
  938. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
  939. le16_to_cpu(dur));
  940. }
  941. /* Instead of using custom probe response template, this function
  942. * just patches custom beacon template by:
  943. * 1) Changing packet type
  944. * 2) Patching duration field
  945. * 3) Stripping TIM
  946. */
  947. static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
  948. u16 *dest_size,
  949. struct ieee80211_rate *rate)
  950. {
  951. const u8 *src_data;
  952. u8 *dest_data;
  953. u16 src_size, elem_size, src_pos, dest_pos;
  954. __le16 dur;
  955. struct ieee80211_hdr *hdr;
  956. size_t ie_start;
  957. src_size = dev->wl->current_beacon->len;
  958. src_data = (const u8 *)dev->wl->current_beacon->data;
  959. /* Get the start offset of the variable IEs in the packet. */
  960. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  961. B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
  962. u.beacon.variable));
  963. if (B43legacy_WARN_ON(src_size < ie_start))
  964. return NULL;
  965. dest_data = kmalloc(src_size, GFP_ATOMIC);
  966. if (unlikely(!dest_data))
  967. return NULL;
  968. /* Copy the static data and all Information Elements, except the TIM. */
  969. memcpy(dest_data, src_data, ie_start);
  970. src_pos = ie_start;
  971. dest_pos = ie_start;
  972. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  973. elem_size = src_data[src_pos + 1] + 2;
  974. if (src_data[src_pos] == 5) {
  975. /* This is the TIM. */
  976. continue;
  977. }
  978. memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
  979. dest_pos += elem_size;
  980. }
  981. *dest_size = dest_pos;
  982. hdr = (struct ieee80211_hdr *)dest_data;
  983. /* Set the frame control. */
  984. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  985. IEEE80211_STYPE_PROBE_RESP);
  986. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  987. dev->wl->vif,
  988. *dest_size,
  989. rate);
  990. hdr->duration_id = dur;
  991. return dest_data;
  992. }
  993. static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
  994. u16 ram_offset,
  995. u16 shm_size_offset,
  996. struct ieee80211_rate *rate)
  997. {
  998. const u8 *probe_resp_data;
  999. u16 size;
  1000. size = dev->wl->current_beacon->len;
  1001. probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
  1002. if (unlikely(!probe_resp_data))
  1003. return;
  1004. /* Looks like PLCP headers plus packet timings are stored for
  1005. * all possible basic rates
  1006. */
  1007. b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
  1008. &b43legacy_b_ratetable[0]);
  1009. b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
  1010. &b43legacy_b_ratetable[1]);
  1011. b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
  1012. &b43legacy_b_ratetable[2]);
  1013. b43legacy_write_probe_resp_plcp(dev, 0x350, size,
  1014. &b43legacy_b_ratetable[3]);
  1015. size = min((size_t)size,
  1016. 0x200 - sizeof(struct b43legacy_plcp_hdr6));
  1017. b43legacy_write_template_common(dev, probe_resp_data,
  1018. size, ram_offset,
  1019. shm_size_offset, rate->hw_value);
  1020. kfree(probe_resp_data);
  1021. }
  1022. static void b43legacy_upload_beacon0(struct b43legacy_wldev *dev)
  1023. {
  1024. struct b43legacy_wl *wl = dev->wl;
  1025. if (wl->beacon0_uploaded)
  1026. return;
  1027. b43legacy_write_beacon_template(dev, 0x68, 0x18);
  1028. /* FIXME: Probe resp upload doesn't really belong here,
  1029. * but we don't use that feature anyway. */
  1030. b43legacy_write_probe_resp_template(dev, 0x268, 0x4A,
  1031. &__b43legacy_ratetable[3]);
  1032. wl->beacon0_uploaded = 1;
  1033. }
  1034. static void b43legacy_upload_beacon1(struct b43legacy_wldev *dev)
  1035. {
  1036. struct b43legacy_wl *wl = dev->wl;
  1037. if (wl->beacon1_uploaded)
  1038. return;
  1039. b43legacy_write_beacon_template(dev, 0x468, 0x1A);
  1040. wl->beacon1_uploaded = 1;
  1041. }
  1042. static void handle_irq_beacon(struct b43legacy_wldev *dev)
  1043. {
  1044. struct b43legacy_wl *wl = dev->wl;
  1045. u32 cmd, beacon0_valid, beacon1_valid;
  1046. if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1047. return;
  1048. /* This is the bottom half of the asynchronous beacon update. */
  1049. /* Ignore interrupt in the future. */
  1050. dev->irq_savedstate &= ~B43legacy_IRQ_BEACON;
  1051. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1052. beacon0_valid = (cmd & B43legacy_MACCMD_BEACON0_VALID);
  1053. beacon1_valid = (cmd & B43legacy_MACCMD_BEACON1_VALID);
  1054. /* Schedule interrupt manually, if busy. */
  1055. if (beacon0_valid && beacon1_valid) {
  1056. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, B43legacy_IRQ_BEACON);
  1057. dev->irq_savedstate |= B43legacy_IRQ_BEACON;
  1058. return;
  1059. }
  1060. if (unlikely(wl->beacon_templates_virgin)) {
  1061. /* We never uploaded a beacon before.
  1062. * Upload both templates now, but only mark one valid. */
  1063. wl->beacon_templates_virgin = 0;
  1064. b43legacy_upload_beacon0(dev);
  1065. b43legacy_upload_beacon1(dev);
  1066. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1067. cmd |= B43legacy_MACCMD_BEACON0_VALID;
  1068. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1069. } else {
  1070. if (!beacon0_valid) {
  1071. b43legacy_upload_beacon0(dev);
  1072. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1073. cmd |= B43legacy_MACCMD_BEACON0_VALID;
  1074. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1075. } else if (!beacon1_valid) {
  1076. b43legacy_upload_beacon1(dev);
  1077. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1078. cmd |= B43legacy_MACCMD_BEACON1_VALID;
  1079. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1080. }
  1081. }
  1082. }
  1083. static void b43legacy_beacon_update_trigger_work(struct work_struct *work)
  1084. {
  1085. struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
  1086. beacon_update_trigger);
  1087. struct b43legacy_wldev *dev;
  1088. mutex_lock(&wl->mutex);
  1089. dev = wl->current_dev;
  1090. if (likely(dev && (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED))) {
  1091. spin_lock_irq(&wl->irq_lock);
  1092. /* update beacon right away or defer to irq */
  1093. dev->irq_savedstate = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
  1094. handle_irq_beacon(dev);
  1095. /* The handler might have updated the IRQ mask. */
  1096. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
  1097. dev->irq_savedstate);
  1098. mmiowb();
  1099. spin_unlock_irq(&wl->irq_lock);
  1100. }
  1101. mutex_unlock(&wl->mutex);
  1102. }
  1103. /* Asynchronously update the packet templates in template RAM.
  1104. * Locking: Requires wl->irq_lock to be locked. */
  1105. static void b43legacy_update_templates(struct b43legacy_wl *wl)
  1106. {
  1107. struct sk_buff *beacon;
  1108. /* This is the top half of the ansynchronous beacon update. The bottom
  1109. * half is the beacon IRQ. Beacon update must be asynchronous to avoid
  1110. * sending an invalid beacon. This can happen for example, if the
  1111. * firmware transmits a beacon while we are updating it. */
  1112. /* We could modify the existing beacon and set the aid bit in the TIM
  1113. * field, but that would probably require resizing and moving of data
  1114. * within the beacon template. Simply request a new beacon and let
  1115. * mac80211 do the hard work. */
  1116. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1117. if (unlikely(!beacon))
  1118. return;
  1119. if (wl->current_beacon)
  1120. dev_kfree_skb_any(wl->current_beacon);
  1121. wl->current_beacon = beacon;
  1122. wl->beacon0_uploaded = 0;
  1123. wl->beacon1_uploaded = 0;
  1124. queue_work(wl->hw->workqueue, &wl->beacon_update_trigger);
  1125. }
  1126. static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
  1127. u16 beacon_int)
  1128. {
  1129. b43legacy_time_lock(dev);
  1130. if (dev->dev->id.revision >= 3) {
  1131. b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_REP,
  1132. (beacon_int << 16));
  1133. b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_START,
  1134. (beacon_int << 10));
  1135. } else {
  1136. b43legacy_write16(dev, 0x606, (beacon_int >> 6));
  1137. b43legacy_write16(dev, 0x610, beacon_int);
  1138. }
  1139. b43legacy_time_unlock(dev);
  1140. b43legacydbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
  1141. }
  1142. static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
  1143. {
  1144. }
  1145. /* Interrupt handler bottom-half */
  1146. static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
  1147. {
  1148. u32 reason;
  1149. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1150. u32 merged_dma_reason = 0;
  1151. int i;
  1152. unsigned long flags;
  1153. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1154. B43legacy_WARN_ON(b43legacy_status(dev) <
  1155. B43legacy_STAT_INITIALIZED);
  1156. reason = dev->irq_reason;
  1157. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1158. dma_reason[i] = dev->dma_reason[i];
  1159. merged_dma_reason |= dma_reason[i];
  1160. }
  1161. if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
  1162. b43legacyerr(dev->wl, "MAC transmission error\n");
  1163. if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
  1164. b43legacyerr(dev->wl, "PHY transmission error\n");
  1165. rmb();
  1166. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1167. b43legacyerr(dev->wl, "Too many PHY TX errors, "
  1168. "restarting the controller\n");
  1169. b43legacy_controller_restart(dev, "PHY TX errors");
  1170. }
  1171. }
  1172. if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
  1173. B43legacy_DMAIRQ_NONFATALMASK))) {
  1174. if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
  1175. b43legacyerr(dev->wl, "Fatal DMA error: "
  1176. "0x%08X, 0x%08X, 0x%08X, "
  1177. "0x%08X, 0x%08X, 0x%08X\n",
  1178. dma_reason[0], dma_reason[1],
  1179. dma_reason[2], dma_reason[3],
  1180. dma_reason[4], dma_reason[5]);
  1181. b43legacy_controller_restart(dev, "DMA error");
  1182. mmiowb();
  1183. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1184. return;
  1185. }
  1186. if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
  1187. b43legacyerr(dev->wl, "DMA error: "
  1188. "0x%08X, 0x%08X, 0x%08X, "
  1189. "0x%08X, 0x%08X, 0x%08X\n",
  1190. dma_reason[0], dma_reason[1],
  1191. dma_reason[2], dma_reason[3],
  1192. dma_reason[4], dma_reason[5]);
  1193. }
  1194. if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
  1195. handle_irq_ucode_debug(dev);
  1196. if (reason & B43legacy_IRQ_TBTT_INDI)
  1197. handle_irq_tbtt_indication(dev);
  1198. if (reason & B43legacy_IRQ_ATIM_END)
  1199. handle_irq_atim_end(dev);
  1200. if (reason & B43legacy_IRQ_BEACON)
  1201. handle_irq_beacon(dev);
  1202. if (reason & B43legacy_IRQ_PMQ)
  1203. handle_irq_pmq(dev);
  1204. if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
  1205. ;/*TODO*/
  1206. if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
  1207. handle_irq_noise(dev);
  1208. /* Check the DMA reason registers for received data. */
  1209. if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
  1210. if (b43legacy_using_pio(dev))
  1211. b43legacy_pio_rx(dev->pio.queue0);
  1212. else
  1213. b43legacy_dma_rx(dev->dma.rx_ring0);
  1214. }
  1215. B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
  1216. B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
  1217. if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
  1218. if (b43legacy_using_pio(dev))
  1219. b43legacy_pio_rx(dev->pio.queue3);
  1220. else
  1221. b43legacy_dma_rx(dev->dma.rx_ring3);
  1222. }
  1223. B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
  1224. B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
  1225. if (reason & B43legacy_IRQ_TX_OK)
  1226. handle_irq_transmit_status(dev);
  1227. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  1228. mmiowb();
  1229. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1230. }
  1231. static void pio_irq_workaround(struct b43legacy_wldev *dev,
  1232. u16 base, int queueidx)
  1233. {
  1234. u16 rxctl;
  1235. rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
  1236. if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
  1237. dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
  1238. else
  1239. dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
  1240. }
  1241. static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
  1242. {
  1243. if (b43legacy_using_pio(dev) &&
  1244. (dev->dev->id.revision < 3) &&
  1245. (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
  1246. /* Apply a PIO specific workaround to the dma_reasons */
  1247. pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
  1248. pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
  1249. pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
  1250. pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
  1251. }
  1252. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
  1253. b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
  1254. dev->dma_reason[0]);
  1255. b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
  1256. dev->dma_reason[1]);
  1257. b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
  1258. dev->dma_reason[2]);
  1259. b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
  1260. dev->dma_reason[3]);
  1261. b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
  1262. dev->dma_reason[4]);
  1263. b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
  1264. dev->dma_reason[5]);
  1265. }
  1266. /* Interrupt handler top-half */
  1267. static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
  1268. {
  1269. irqreturn_t ret = IRQ_NONE;
  1270. struct b43legacy_wldev *dev = dev_id;
  1271. u32 reason;
  1272. if (!dev)
  1273. return IRQ_NONE;
  1274. spin_lock(&dev->wl->irq_lock);
  1275. if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
  1276. goto out;
  1277. reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1278. if (reason == 0xffffffff) /* shared IRQ */
  1279. goto out;
  1280. ret = IRQ_HANDLED;
  1281. reason &= b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
  1282. if (!reason)
  1283. goto out;
  1284. dev->dma_reason[0] = b43legacy_read32(dev,
  1285. B43legacy_MMIO_DMA0_REASON)
  1286. & 0x0001DC00;
  1287. dev->dma_reason[1] = b43legacy_read32(dev,
  1288. B43legacy_MMIO_DMA1_REASON)
  1289. & 0x0000DC00;
  1290. dev->dma_reason[2] = b43legacy_read32(dev,
  1291. B43legacy_MMIO_DMA2_REASON)
  1292. & 0x0000DC00;
  1293. dev->dma_reason[3] = b43legacy_read32(dev,
  1294. B43legacy_MMIO_DMA3_REASON)
  1295. & 0x0001DC00;
  1296. dev->dma_reason[4] = b43legacy_read32(dev,
  1297. B43legacy_MMIO_DMA4_REASON)
  1298. & 0x0000DC00;
  1299. dev->dma_reason[5] = b43legacy_read32(dev,
  1300. B43legacy_MMIO_DMA5_REASON)
  1301. & 0x0000DC00;
  1302. b43legacy_interrupt_ack(dev, reason);
  1303. /* disable all IRQs. They are enabled again in the bottom half. */
  1304. dev->irq_savedstate = b43legacy_interrupt_disable(dev,
  1305. B43legacy_IRQ_ALL);
  1306. /* save the reason code and call our bottom half. */
  1307. dev->irq_reason = reason;
  1308. tasklet_schedule(&dev->isr_tasklet);
  1309. out:
  1310. mmiowb();
  1311. spin_unlock(&dev->wl->irq_lock);
  1312. return ret;
  1313. }
  1314. static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
  1315. {
  1316. release_firmware(dev->fw.ucode);
  1317. dev->fw.ucode = NULL;
  1318. release_firmware(dev->fw.pcm);
  1319. dev->fw.pcm = NULL;
  1320. release_firmware(dev->fw.initvals);
  1321. dev->fw.initvals = NULL;
  1322. release_firmware(dev->fw.initvals_band);
  1323. dev->fw.initvals_band = NULL;
  1324. }
  1325. static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
  1326. {
  1327. b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
  1328. "Drivers/b43#devicefirmware "
  1329. "and download the correct firmware (version 3).\n");
  1330. }
  1331. static int do_request_fw(struct b43legacy_wldev *dev,
  1332. const char *name,
  1333. const struct firmware **fw)
  1334. {
  1335. char path[sizeof(modparam_fwpostfix) + 32];
  1336. struct b43legacy_fw_header *hdr;
  1337. u32 size;
  1338. int err;
  1339. if (!name)
  1340. return 0;
  1341. snprintf(path, ARRAY_SIZE(path),
  1342. "b43legacy%s/%s.fw",
  1343. modparam_fwpostfix, name);
  1344. err = request_firmware(fw, path, dev->dev->dev);
  1345. if (err) {
  1346. b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
  1347. "or load failed.\n", path);
  1348. return err;
  1349. }
  1350. if ((*fw)->size < sizeof(struct b43legacy_fw_header))
  1351. goto err_format;
  1352. hdr = (struct b43legacy_fw_header *)((*fw)->data);
  1353. switch (hdr->type) {
  1354. case B43legacy_FW_TYPE_UCODE:
  1355. case B43legacy_FW_TYPE_PCM:
  1356. size = be32_to_cpu(hdr->size);
  1357. if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
  1358. goto err_format;
  1359. /* fallthrough */
  1360. case B43legacy_FW_TYPE_IV:
  1361. if (hdr->ver != 1)
  1362. goto err_format;
  1363. break;
  1364. default:
  1365. goto err_format;
  1366. }
  1367. return err;
  1368. err_format:
  1369. b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1370. return -EPROTO;
  1371. }
  1372. static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
  1373. {
  1374. struct b43legacy_firmware *fw = &dev->fw;
  1375. const u8 rev = dev->dev->id.revision;
  1376. const char *filename;
  1377. u32 tmshigh;
  1378. int err;
  1379. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1380. if (!fw->ucode) {
  1381. if (rev == 2)
  1382. filename = "ucode2";
  1383. else if (rev == 4)
  1384. filename = "ucode4";
  1385. else
  1386. filename = "ucode5";
  1387. err = do_request_fw(dev, filename, &fw->ucode);
  1388. if (err)
  1389. goto err_load;
  1390. }
  1391. if (!fw->pcm) {
  1392. if (rev < 5)
  1393. filename = "pcm4";
  1394. else
  1395. filename = "pcm5";
  1396. err = do_request_fw(dev, filename, &fw->pcm);
  1397. if (err)
  1398. goto err_load;
  1399. }
  1400. if (!fw->initvals) {
  1401. switch (dev->phy.type) {
  1402. case B43legacy_PHYTYPE_B:
  1403. case B43legacy_PHYTYPE_G:
  1404. if ((rev >= 5) && (rev <= 10))
  1405. filename = "b0g0initvals5";
  1406. else if (rev == 2 || rev == 4)
  1407. filename = "b0g0initvals2";
  1408. else
  1409. goto err_no_initvals;
  1410. break;
  1411. default:
  1412. goto err_no_initvals;
  1413. }
  1414. err = do_request_fw(dev, filename, &fw->initvals);
  1415. if (err)
  1416. goto err_load;
  1417. }
  1418. if (!fw->initvals_band) {
  1419. switch (dev->phy.type) {
  1420. case B43legacy_PHYTYPE_B:
  1421. case B43legacy_PHYTYPE_G:
  1422. if ((rev >= 5) && (rev <= 10))
  1423. filename = "b0g0bsinitvals5";
  1424. else if (rev >= 11)
  1425. filename = NULL;
  1426. else if (rev == 2 || rev == 4)
  1427. filename = NULL;
  1428. else
  1429. goto err_no_initvals;
  1430. break;
  1431. default:
  1432. goto err_no_initvals;
  1433. }
  1434. err = do_request_fw(dev, filename, &fw->initvals_band);
  1435. if (err)
  1436. goto err_load;
  1437. }
  1438. return 0;
  1439. err_load:
  1440. b43legacy_print_fw_helptext(dev->wl);
  1441. goto error;
  1442. err_no_initvals:
  1443. err = -ENODEV;
  1444. b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
  1445. "core rev %u\n", dev->phy.type, rev);
  1446. goto error;
  1447. error:
  1448. b43legacy_release_firmware(dev);
  1449. return err;
  1450. }
  1451. static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
  1452. {
  1453. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1454. const __be32 *data;
  1455. unsigned int i;
  1456. unsigned int len;
  1457. u16 fwrev;
  1458. u16 fwpatch;
  1459. u16 fwdate;
  1460. u16 fwtime;
  1461. u32 tmp, macctl;
  1462. int err = 0;
  1463. /* Jump the microcode PSM to offset 0 */
  1464. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1465. B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
  1466. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1467. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1468. /* Zero out all microcode PSM registers and shared memory. */
  1469. for (i = 0; i < 64; i++)
  1470. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
  1471. for (i = 0; i < 4096; i += 2)
  1472. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
  1473. /* Upload Microcode. */
  1474. data = (__be32 *) (dev->fw.ucode->data + hdr_len);
  1475. len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
  1476. b43legacy_shm_control_word(dev,
  1477. B43legacy_SHM_UCODE |
  1478. B43legacy_SHM_AUTOINC_W,
  1479. 0x0000);
  1480. for (i = 0; i < len; i++) {
  1481. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1482. be32_to_cpu(data[i]));
  1483. udelay(10);
  1484. }
  1485. if (dev->fw.pcm) {
  1486. /* Upload PCM data. */
  1487. data = (__be32 *) (dev->fw.pcm->data + hdr_len);
  1488. len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
  1489. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
  1490. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
  1491. /* No need for autoinc bit in SHM_HW */
  1492. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
  1493. for (i = 0; i < len; i++) {
  1494. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1495. be32_to_cpu(data[i]));
  1496. udelay(10);
  1497. }
  1498. }
  1499. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1500. B43legacy_IRQ_ALL);
  1501. /* Start the microcode PSM */
  1502. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1503. macctl &= ~B43legacy_MACCTL_PSM_JMP0;
  1504. macctl |= B43legacy_MACCTL_PSM_RUN;
  1505. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1506. /* Wait for the microcode to load and respond */
  1507. i = 0;
  1508. while (1) {
  1509. tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1510. if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
  1511. break;
  1512. i++;
  1513. if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
  1514. b43legacyerr(dev->wl, "Microcode not responding\n");
  1515. b43legacy_print_fw_helptext(dev->wl);
  1516. err = -ENODEV;
  1517. goto error;
  1518. }
  1519. msleep_interruptible(50);
  1520. if (signal_pending(current)) {
  1521. err = -EINTR;
  1522. goto error;
  1523. }
  1524. }
  1525. /* dummy read follows */
  1526. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1527. /* Get and check the revisions. */
  1528. fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1529. B43legacy_SHM_SH_UCODEREV);
  1530. fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1531. B43legacy_SHM_SH_UCODEPATCH);
  1532. fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1533. B43legacy_SHM_SH_UCODEDATE);
  1534. fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1535. B43legacy_SHM_SH_UCODETIME);
  1536. if (fwrev > 0x128) {
  1537. b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
  1538. " Only firmware from binary drivers version 3.x"
  1539. " is supported. You must change your firmware"
  1540. " files.\n");
  1541. b43legacy_print_fw_helptext(dev->wl);
  1542. err = -EOPNOTSUPP;
  1543. goto error;
  1544. }
  1545. b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
  1546. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
  1547. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1548. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
  1549. fwtime & 0x1F);
  1550. dev->fw.rev = fwrev;
  1551. dev->fw.patch = fwpatch;
  1552. return 0;
  1553. error:
  1554. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1555. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  1556. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1557. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1558. return err;
  1559. }
  1560. static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
  1561. const struct b43legacy_iv *ivals,
  1562. size_t count,
  1563. size_t array_size)
  1564. {
  1565. const struct b43legacy_iv *iv;
  1566. u16 offset;
  1567. size_t i;
  1568. bool bit32;
  1569. BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
  1570. iv = ivals;
  1571. for (i = 0; i < count; i++) {
  1572. if (array_size < sizeof(iv->offset_size))
  1573. goto err_format;
  1574. array_size -= sizeof(iv->offset_size);
  1575. offset = be16_to_cpu(iv->offset_size);
  1576. bit32 = !!(offset & B43legacy_IV_32BIT);
  1577. offset &= B43legacy_IV_OFFSET_MASK;
  1578. if (offset >= 0x1000)
  1579. goto err_format;
  1580. if (bit32) {
  1581. u32 value;
  1582. if (array_size < sizeof(iv->data.d32))
  1583. goto err_format;
  1584. array_size -= sizeof(iv->data.d32);
  1585. value = get_unaligned_be32(&iv->data.d32);
  1586. b43legacy_write32(dev, offset, value);
  1587. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1588. sizeof(__be16) +
  1589. sizeof(__be32));
  1590. } else {
  1591. u16 value;
  1592. if (array_size < sizeof(iv->data.d16))
  1593. goto err_format;
  1594. array_size -= sizeof(iv->data.d16);
  1595. value = be16_to_cpu(iv->data.d16);
  1596. b43legacy_write16(dev, offset, value);
  1597. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1598. sizeof(__be16) +
  1599. sizeof(__be16));
  1600. }
  1601. }
  1602. if (array_size)
  1603. goto err_format;
  1604. return 0;
  1605. err_format:
  1606. b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
  1607. b43legacy_print_fw_helptext(dev->wl);
  1608. return -EPROTO;
  1609. }
  1610. static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
  1611. {
  1612. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1613. const struct b43legacy_fw_header *hdr;
  1614. struct b43legacy_firmware *fw = &dev->fw;
  1615. const struct b43legacy_iv *ivals;
  1616. size_t count;
  1617. int err;
  1618. hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
  1619. ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
  1620. count = be32_to_cpu(hdr->size);
  1621. err = b43legacy_write_initvals(dev, ivals, count,
  1622. fw->initvals->size - hdr_len);
  1623. if (err)
  1624. goto out;
  1625. if (fw->initvals_band) {
  1626. hdr = (const struct b43legacy_fw_header *)
  1627. (fw->initvals_band->data);
  1628. ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
  1629. + hdr_len);
  1630. count = be32_to_cpu(hdr->size);
  1631. err = b43legacy_write_initvals(dev, ivals, count,
  1632. fw->initvals_band->size - hdr_len);
  1633. if (err)
  1634. goto out;
  1635. }
  1636. out:
  1637. return err;
  1638. }
  1639. /* Initialize the GPIOs
  1640. * http://bcm-specs.sipsolutions.net/GPIO
  1641. */
  1642. static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
  1643. {
  1644. struct ssb_bus *bus = dev->dev->bus;
  1645. struct ssb_device *gpiodev, *pcidev = NULL;
  1646. u32 mask;
  1647. u32 set;
  1648. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1649. b43legacy_read32(dev,
  1650. B43legacy_MMIO_MACCTL)
  1651. & 0xFFFF3FFF);
  1652. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1653. b43legacy_read16(dev,
  1654. B43legacy_MMIO_GPIO_MASK)
  1655. | 0x000F);
  1656. mask = 0x0000001F;
  1657. set = 0x0000000F;
  1658. if (dev->dev->bus->chip_id == 0x4301) {
  1659. mask |= 0x0060;
  1660. set |= 0x0060;
  1661. }
  1662. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
  1663. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1664. b43legacy_read16(dev,
  1665. B43legacy_MMIO_GPIO_MASK)
  1666. | 0x0200);
  1667. mask |= 0x0200;
  1668. set |= 0x0200;
  1669. }
  1670. if (dev->dev->id.revision >= 2)
  1671. mask |= 0x0010; /* FIXME: This is redundant. */
  1672. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1673. pcidev = bus->pcicore.dev;
  1674. #endif
  1675. gpiodev = bus->chipco.dev ? : pcidev;
  1676. if (!gpiodev)
  1677. return 0;
  1678. ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
  1679. (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
  1680. & mask) | set);
  1681. return 0;
  1682. }
  1683. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  1684. static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
  1685. {
  1686. struct ssb_bus *bus = dev->dev->bus;
  1687. struct ssb_device *gpiodev, *pcidev = NULL;
  1688. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1689. pcidev = bus->pcicore.dev;
  1690. #endif
  1691. gpiodev = bus->chipco.dev ? : pcidev;
  1692. if (!gpiodev)
  1693. return;
  1694. ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
  1695. }
  1696. /* http://bcm-specs.sipsolutions.net/EnableMac */
  1697. void b43legacy_mac_enable(struct b43legacy_wldev *dev)
  1698. {
  1699. dev->mac_suspended--;
  1700. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1701. B43legacy_WARN_ON(irqs_disabled());
  1702. if (dev->mac_suspended == 0) {
  1703. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1704. b43legacy_read32(dev,
  1705. B43legacy_MMIO_MACCTL)
  1706. | B43legacy_MACCTL_ENABLED);
  1707. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1708. B43legacy_IRQ_MAC_SUSPENDED);
  1709. /* the next two are dummy reads */
  1710. b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1711. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1712. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1713. /* Re-enable IRQs. */
  1714. spin_lock_irq(&dev->wl->irq_lock);
  1715. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  1716. spin_unlock_irq(&dev->wl->irq_lock);
  1717. }
  1718. }
  1719. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1720. void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
  1721. {
  1722. int i;
  1723. u32 tmp;
  1724. might_sleep();
  1725. B43legacy_WARN_ON(irqs_disabled());
  1726. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1727. if (dev->mac_suspended == 0) {
  1728. /* Mask IRQs before suspending MAC. Otherwise
  1729. * the MAC stays busy and won't suspend. */
  1730. spin_lock_irq(&dev->wl->irq_lock);
  1731. tmp = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  1732. spin_unlock_irq(&dev->wl->irq_lock);
  1733. b43legacy_synchronize_irq(dev);
  1734. dev->irq_savedstate = tmp;
  1735. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1736. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1737. b43legacy_read32(dev,
  1738. B43legacy_MMIO_MACCTL)
  1739. & ~B43legacy_MACCTL_ENABLED);
  1740. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1741. for (i = 40; i; i--) {
  1742. tmp = b43legacy_read32(dev,
  1743. B43legacy_MMIO_GEN_IRQ_REASON);
  1744. if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
  1745. goto out;
  1746. msleep(1);
  1747. }
  1748. b43legacyerr(dev->wl, "MAC suspend failed\n");
  1749. }
  1750. out:
  1751. dev->mac_suspended++;
  1752. }
  1753. static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
  1754. {
  1755. struct b43legacy_wl *wl = dev->wl;
  1756. u32 ctl;
  1757. u16 cfp_pretbtt;
  1758. ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1759. /* Reset status to STA infrastructure mode. */
  1760. ctl &= ~B43legacy_MACCTL_AP;
  1761. ctl &= ~B43legacy_MACCTL_KEEP_CTL;
  1762. ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
  1763. ctl &= ~B43legacy_MACCTL_KEEP_BAD;
  1764. ctl &= ~B43legacy_MACCTL_PROMISC;
  1765. ctl &= ~B43legacy_MACCTL_BEACPROMISC;
  1766. ctl |= B43legacy_MACCTL_INFRA;
  1767. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1768. ctl |= B43legacy_MACCTL_AP;
  1769. else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
  1770. ctl &= ~B43legacy_MACCTL_INFRA;
  1771. if (wl->filter_flags & FIF_CONTROL)
  1772. ctl |= B43legacy_MACCTL_KEEP_CTL;
  1773. if (wl->filter_flags & FIF_FCSFAIL)
  1774. ctl |= B43legacy_MACCTL_KEEP_BAD;
  1775. if (wl->filter_flags & FIF_PLCPFAIL)
  1776. ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
  1777. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1778. ctl |= B43legacy_MACCTL_PROMISC;
  1779. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1780. ctl |= B43legacy_MACCTL_BEACPROMISC;
  1781. /* Workaround: On old hardware the HW-MAC-address-filter
  1782. * doesn't work properly, so always run promisc in filter
  1783. * it in software. */
  1784. if (dev->dev->id.revision <= 4)
  1785. ctl |= B43legacy_MACCTL_PROMISC;
  1786. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
  1787. cfp_pretbtt = 2;
  1788. if ((ctl & B43legacy_MACCTL_INFRA) &&
  1789. !(ctl & B43legacy_MACCTL_AP)) {
  1790. if (dev->dev->bus->chip_id == 0x4306 &&
  1791. dev->dev->bus->chip_rev == 3)
  1792. cfp_pretbtt = 100;
  1793. else
  1794. cfp_pretbtt = 50;
  1795. }
  1796. b43legacy_write16(dev, 0x612, cfp_pretbtt);
  1797. }
  1798. static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
  1799. u16 rate,
  1800. int is_ofdm)
  1801. {
  1802. u16 offset;
  1803. if (is_ofdm) {
  1804. offset = 0x480;
  1805. offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1806. } else {
  1807. offset = 0x4C0;
  1808. offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1809. }
  1810. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
  1811. b43legacy_shm_read16(dev,
  1812. B43legacy_SHM_SHARED, offset));
  1813. }
  1814. static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
  1815. {
  1816. switch (dev->phy.type) {
  1817. case B43legacy_PHYTYPE_G:
  1818. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
  1819. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
  1820. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
  1821. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
  1822. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
  1823. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
  1824. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
  1825. /* fallthrough */
  1826. case B43legacy_PHYTYPE_B:
  1827. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
  1828. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
  1829. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
  1830. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
  1831. break;
  1832. default:
  1833. B43legacy_BUG_ON(1);
  1834. }
  1835. }
  1836. /* Set the TX-Antenna for management frames sent by firmware. */
  1837. static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
  1838. int antenna)
  1839. {
  1840. u16 ant = 0;
  1841. u16 tmp;
  1842. switch (antenna) {
  1843. case B43legacy_ANTENNA0:
  1844. ant |= B43legacy_TX4_PHY_ANT0;
  1845. break;
  1846. case B43legacy_ANTENNA1:
  1847. ant |= B43legacy_TX4_PHY_ANT1;
  1848. break;
  1849. case B43legacy_ANTENNA_AUTO:
  1850. ant |= B43legacy_TX4_PHY_ANTLAST;
  1851. break;
  1852. default:
  1853. B43legacy_BUG_ON(1);
  1854. }
  1855. /* FIXME We also need to set the other flags of the PHY control
  1856. * field somewhere. */
  1857. /* For Beacons */
  1858. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1859. B43legacy_SHM_SH_BEACPHYCTL);
  1860. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1861. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1862. B43legacy_SHM_SH_BEACPHYCTL, tmp);
  1863. /* For ACK/CTS */
  1864. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1865. B43legacy_SHM_SH_ACKCTSPHYCTL);
  1866. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1867. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1868. B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
  1869. /* For Probe Resposes */
  1870. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1871. B43legacy_SHM_SH_PRPHYCTL);
  1872. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1873. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1874. B43legacy_SHM_SH_PRPHYCTL, tmp);
  1875. }
  1876. /* This is the opposite of b43legacy_chip_init() */
  1877. static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
  1878. {
  1879. b43legacy_radio_turn_off(dev, 1);
  1880. b43legacy_gpio_cleanup(dev);
  1881. /* firmware is released later */
  1882. }
  1883. /* Initialize the chip
  1884. * http://bcm-specs.sipsolutions.net/ChipInit
  1885. */
  1886. static int b43legacy_chip_init(struct b43legacy_wldev *dev)
  1887. {
  1888. struct b43legacy_phy *phy = &dev->phy;
  1889. int err;
  1890. int tmp;
  1891. u32 value32, macctl;
  1892. u16 value16;
  1893. /* Initialize the MAC control */
  1894. macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
  1895. if (dev->phy.gmode)
  1896. macctl |= B43legacy_MACCTL_GMODE;
  1897. macctl |= B43legacy_MACCTL_INFRA;
  1898. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1899. err = b43legacy_request_firmware(dev);
  1900. if (err)
  1901. goto out;
  1902. err = b43legacy_upload_microcode(dev);
  1903. if (err)
  1904. goto out; /* firmware is released later */
  1905. err = b43legacy_gpio_init(dev);
  1906. if (err)
  1907. goto out; /* firmware is released later */
  1908. err = b43legacy_upload_initvals(dev);
  1909. if (err)
  1910. goto err_gpio_clean;
  1911. b43legacy_radio_turn_on(dev);
  1912. b43legacy_write16(dev, 0x03E6, 0x0000);
  1913. err = b43legacy_phy_init(dev);
  1914. if (err)
  1915. goto err_radio_off;
  1916. /* Select initial Interference Mitigation. */
  1917. tmp = phy->interfmode;
  1918. phy->interfmode = B43legacy_INTERFMODE_NONE;
  1919. b43legacy_radio_set_interference_mitigation(dev, tmp);
  1920. b43legacy_phy_set_antenna_diversity(dev);
  1921. b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
  1922. if (phy->type == B43legacy_PHYTYPE_B) {
  1923. value16 = b43legacy_read16(dev, 0x005E);
  1924. value16 |= 0x0004;
  1925. b43legacy_write16(dev, 0x005E, value16);
  1926. }
  1927. b43legacy_write32(dev, 0x0100, 0x01000000);
  1928. if (dev->dev->id.revision < 5)
  1929. b43legacy_write32(dev, 0x010C, 0x01000000);
  1930. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1931. value32 &= ~B43legacy_MACCTL_INFRA;
  1932. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1933. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1934. value32 |= B43legacy_MACCTL_INFRA;
  1935. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1936. if (b43legacy_using_pio(dev)) {
  1937. b43legacy_write32(dev, 0x0210, 0x00000100);
  1938. b43legacy_write32(dev, 0x0230, 0x00000100);
  1939. b43legacy_write32(dev, 0x0250, 0x00000100);
  1940. b43legacy_write32(dev, 0x0270, 0x00000100);
  1941. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
  1942. 0x0000);
  1943. }
  1944. /* Probe Response Timeout value */
  1945. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  1946. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
  1947. /* Initially set the wireless operation mode. */
  1948. b43legacy_adjust_opmode(dev);
  1949. if (dev->dev->id.revision < 3) {
  1950. b43legacy_write16(dev, 0x060E, 0x0000);
  1951. b43legacy_write16(dev, 0x0610, 0x8000);
  1952. b43legacy_write16(dev, 0x0604, 0x0000);
  1953. b43legacy_write16(dev, 0x0606, 0x0200);
  1954. } else {
  1955. b43legacy_write32(dev, 0x0188, 0x80000000);
  1956. b43legacy_write32(dev, 0x018C, 0x02000000);
  1957. }
  1958. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
  1959. b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  1960. b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  1961. b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  1962. b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  1963. b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  1964. b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  1965. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  1966. value32 |= 0x00100000;
  1967. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  1968. b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
  1969. dev->dev->bus->chipco.fast_pwrup_delay);
  1970. /* PHY TX errors counter. */
  1971. atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  1972. B43legacy_WARN_ON(err != 0);
  1973. b43legacydbg(dev->wl, "Chip initialized\n");
  1974. out:
  1975. return err;
  1976. err_radio_off:
  1977. b43legacy_radio_turn_off(dev, 1);
  1978. err_gpio_clean:
  1979. b43legacy_gpio_cleanup(dev);
  1980. goto out;
  1981. }
  1982. static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
  1983. {
  1984. struct b43legacy_phy *phy = &dev->phy;
  1985. if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
  1986. return;
  1987. b43legacy_mac_suspend(dev);
  1988. b43legacy_phy_lo_g_measure(dev);
  1989. b43legacy_mac_enable(dev);
  1990. }
  1991. static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
  1992. {
  1993. b43legacy_phy_lo_mark_all_unused(dev);
  1994. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
  1995. b43legacy_mac_suspend(dev);
  1996. b43legacy_calc_nrssi_slope(dev);
  1997. b43legacy_mac_enable(dev);
  1998. }
  1999. }
  2000. static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
  2001. {
  2002. /* Update device statistics. */
  2003. b43legacy_calculate_link_quality(dev);
  2004. }
  2005. static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
  2006. {
  2007. b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
  2008. atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  2009. wmb();
  2010. }
  2011. static void do_periodic_work(struct b43legacy_wldev *dev)
  2012. {
  2013. unsigned int state;
  2014. state = dev->periodic_state;
  2015. if (state % 8 == 0)
  2016. b43legacy_periodic_every120sec(dev);
  2017. if (state % 4 == 0)
  2018. b43legacy_periodic_every60sec(dev);
  2019. if (state % 2 == 0)
  2020. b43legacy_periodic_every30sec(dev);
  2021. b43legacy_periodic_every15sec(dev);
  2022. }
  2023. /* Periodic work locking policy:
  2024. * The whole periodic work handler is protected by
  2025. * wl->mutex. If another lock is needed somewhere in the
  2026. * pwork callchain, it's aquired in-place, where it's needed.
  2027. */
  2028. static void b43legacy_periodic_work_handler(struct work_struct *work)
  2029. {
  2030. struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
  2031. periodic_work.work);
  2032. struct b43legacy_wl *wl = dev->wl;
  2033. unsigned long delay;
  2034. mutex_lock(&wl->mutex);
  2035. if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
  2036. goto out;
  2037. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
  2038. goto out_requeue;
  2039. do_periodic_work(dev);
  2040. dev->periodic_state++;
  2041. out_requeue:
  2042. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
  2043. delay = msecs_to_jiffies(50);
  2044. else
  2045. delay = round_jiffies_relative(HZ * 15);
  2046. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  2047. out:
  2048. mutex_unlock(&wl->mutex);
  2049. }
  2050. static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
  2051. {
  2052. struct delayed_work *work = &dev->periodic_work;
  2053. dev->periodic_state = 0;
  2054. INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
  2055. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2056. }
  2057. /* Validate access to the chip (SHM) */
  2058. static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
  2059. {
  2060. u32 value;
  2061. u32 shm_backup;
  2062. shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
  2063. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
  2064. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  2065. 0xAA5555AA)
  2066. goto error;
  2067. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
  2068. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  2069. 0x55AAAA55)
  2070. goto error;
  2071. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
  2072. value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2073. if ((value | B43legacy_MACCTL_GMODE) !=
  2074. (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
  2075. goto error;
  2076. value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  2077. if (value)
  2078. goto error;
  2079. return 0;
  2080. error:
  2081. b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
  2082. return -ENODEV;
  2083. }
  2084. static void b43legacy_security_init(struct b43legacy_wldev *dev)
  2085. {
  2086. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2087. B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2088. dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2089. 0x0056);
  2090. /* KTP is a word address, but we address SHM bytewise.
  2091. * So multiply by two.
  2092. */
  2093. dev->ktp *= 2;
  2094. if (dev->dev->id.revision >= 5)
  2095. /* Number of RCMTA address slots */
  2096. b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
  2097. dev->max_nr_keys - 8);
  2098. }
  2099. #ifdef CONFIG_B43LEGACY_HWRNG
  2100. static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
  2101. {
  2102. struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
  2103. unsigned long flags;
  2104. /* Don't take wl->mutex here, as it could deadlock with
  2105. * hwrng internal locking. It's not needed to take
  2106. * wl->mutex here, anyway. */
  2107. spin_lock_irqsave(&wl->irq_lock, flags);
  2108. *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
  2109. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2110. return (sizeof(u16));
  2111. }
  2112. #endif
  2113. static void b43legacy_rng_exit(struct b43legacy_wl *wl)
  2114. {
  2115. #ifdef CONFIG_B43LEGACY_HWRNG
  2116. if (wl->rng_initialized)
  2117. hwrng_unregister(&wl->rng);
  2118. #endif
  2119. }
  2120. static int b43legacy_rng_init(struct b43legacy_wl *wl)
  2121. {
  2122. int err = 0;
  2123. #ifdef CONFIG_B43LEGACY_HWRNG
  2124. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2125. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2126. wl->rng.name = wl->rng_name;
  2127. wl->rng.data_read = b43legacy_rng_read;
  2128. wl->rng.priv = (unsigned long)wl;
  2129. wl->rng_initialized = 1;
  2130. err = hwrng_register(&wl->rng);
  2131. if (err) {
  2132. wl->rng_initialized = 0;
  2133. b43legacyerr(wl, "Failed to register the random "
  2134. "number generator (%d)\n", err);
  2135. }
  2136. #endif
  2137. return err;
  2138. }
  2139. static int b43legacy_op_tx(struct ieee80211_hw *hw,
  2140. struct sk_buff *skb)
  2141. {
  2142. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2143. struct b43legacy_wldev *dev = wl->current_dev;
  2144. int err = -ENODEV;
  2145. unsigned long flags;
  2146. if (unlikely(!dev))
  2147. goto out;
  2148. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
  2149. goto out;
  2150. /* DMA-TX is done without a global lock. */
  2151. if (b43legacy_using_pio(dev)) {
  2152. spin_lock_irqsave(&wl->irq_lock, flags);
  2153. err = b43legacy_pio_tx(dev, skb);
  2154. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2155. } else
  2156. err = b43legacy_dma_tx(dev, skb);
  2157. out:
  2158. if (unlikely(err)) {
  2159. /* Drop the packet. */
  2160. dev_kfree_skb_any(skb);
  2161. }
  2162. return NETDEV_TX_OK;
  2163. }
  2164. static int b43legacy_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2165. const struct ieee80211_tx_queue_params *params)
  2166. {
  2167. return 0;
  2168. }
  2169. static int b43legacy_op_get_tx_stats(struct ieee80211_hw *hw,
  2170. struct ieee80211_tx_queue_stats *stats)
  2171. {
  2172. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2173. struct b43legacy_wldev *dev = wl->current_dev;
  2174. unsigned long flags;
  2175. int err = -ENODEV;
  2176. if (!dev)
  2177. goto out;
  2178. spin_lock_irqsave(&wl->irq_lock, flags);
  2179. if (likely(b43legacy_status(dev) >= B43legacy_STAT_STARTED)) {
  2180. if (b43legacy_using_pio(dev))
  2181. b43legacy_pio_get_tx_stats(dev, stats);
  2182. else
  2183. b43legacy_dma_get_tx_stats(dev, stats);
  2184. err = 0;
  2185. }
  2186. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2187. out:
  2188. return err;
  2189. }
  2190. static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
  2191. struct ieee80211_low_level_stats *stats)
  2192. {
  2193. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2194. unsigned long flags;
  2195. spin_lock_irqsave(&wl->irq_lock, flags);
  2196. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2197. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2198. return 0;
  2199. }
  2200. static const char *phymode_to_string(unsigned int phymode)
  2201. {
  2202. switch (phymode) {
  2203. case B43legacy_PHYMODE_B:
  2204. return "B";
  2205. case B43legacy_PHYMODE_G:
  2206. return "G";
  2207. default:
  2208. B43legacy_BUG_ON(1);
  2209. }
  2210. return "";
  2211. }
  2212. static int find_wldev_for_phymode(struct b43legacy_wl *wl,
  2213. unsigned int phymode,
  2214. struct b43legacy_wldev **dev,
  2215. bool *gmode)
  2216. {
  2217. struct b43legacy_wldev *d;
  2218. list_for_each_entry(d, &wl->devlist, list) {
  2219. if (d->phy.possible_phymodes & phymode) {
  2220. /* Ok, this device supports the PHY-mode.
  2221. * Set the gmode bit. */
  2222. *gmode = 1;
  2223. *dev = d;
  2224. return 0;
  2225. }
  2226. }
  2227. return -ESRCH;
  2228. }
  2229. static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
  2230. {
  2231. struct ssb_device *sdev = dev->dev;
  2232. u32 tmslow;
  2233. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2234. tmslow &= ~B43legacy_TMSLOW_GMODE;
  2235. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2236. tmslow |= SSB_TMSLOW_FGC;
  2237. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2238. msleep(1);
  2239. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2240. tmslow &= ~SSB_TMSLOW_FGC;
  2241. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2242. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2243. msleep(1);
  2244. }
  2245. /* Expects wl->mutex locked */
  2246. static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
  2247. unsigned int new_mode)
  2248. {
  2249. struct b43legacy_wldev *uninitialized_var(up_dev);
  2250. struct b43legacy_wldev *down_dev;
  2251. int err;
  2252. bool gmode = 0;
  2253. int prev_status;
  2254. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2255. if (err) {
  2256. b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
  2257. phymode_to_string(new_mode));
  2258. return err;
  2259. }
  2260. if ((up_dev == wl->current_dev) &&
  2261. (!!wl->current_dev->phy.gmode == !!gmode))
  2262. /* This device is already running. */
  2263. return 0;
  2264. b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2265. phymode_to_string(new_mode));
  2266. down_dev = wl->current_dev;
  2267. prev_status = b43legacy_status(down_dev);
  2268. /* Shutdown the currently running core. */
  2269. if (prev_status >= B43legacy_STAT_STARTED)
  2270. b43legacy_wireless_core_stop(down_dev);
  2271. if (prev_status >= B43legacy_STAT_INITIALIZED)
  2272. b43legacy_wireless_core_exit(down_dev);
  2273. if (down_dev != up_dev)
  2274. /* We switch to a different core, so we put PHY into
  2275. * RESET on the old core. */
  2276. b43legacy_put_phy_into_reset(down_dev);
  2277. /* Now start the new core. */
  2278. up_dev->phy.gmode = gmode;
  2279. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  2280. err = b43legacy_wireless_core_init(up_dev);
  2281. if (err) {
  2282. b43legacyerr(wl, "Fatal: Could not initialize device"
  2283. " for newly selected %s-PHY mode\n",
  2284. phymode_to_string(new_mode));
  2285. goto init_failure;
  2286. }
  2287. }
  2288. if (prev_status >= B43legacy_STAT_STARTED) {
  2289. err = b43legacy_wireless_core_start(up_dev);
  2290. if (err) {
  2291. b43legacyerr(wl, "Fatal: Coult not start device for "
  2292. "newly selected %s-PHY mode\n",
  2293. phymode_to_string(new_mode));
  2294. b43legacy_wireless_core_exit(up_dev);
  2295. goto init_failure;
  2296. }
  2297. }
  2298. B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
  2299. b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
  2300. wl->current_dev = up_dev;
  2301. return 0;
  2302. init_failure:
  2303. /* Whoops, failed to init the new core. No core is operating now. */
  2304. wl->current_dev = NULL;
  2305. return err;
  2306. }
  2307. /* Write the short and long frame retry limit values. */
  2308. static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
  2309. unsigned int short_retry,
  2310. unsigned int long_retry)
  2311. {
  2312. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2313. * the chip-internal counter. */
  2314. short_retry = min(short_retry, (unsigned int)0xF);
  2315. long_retry = min(long_retry, (unsigned int)0xF);
  2316. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
  2317. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
  2318. }
  2319. static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
  2320. u32 changed)
  2321. {
  2322. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2323. struct b43legacy_wldev *dev;
  2324. struct b43legacy_phy *phy;
  2325. struct ieee80211_conf *conf = &hw->conf;
  2326. unsigned long flags;
  2327. unsigned int new_phymode = 0xFFFF;
  2328. int antenna_tx;
  2329. int antenna_rx;
  2330. int err = 0;
  2331. u32 savedirqs;
  2332. antenna_tx = B43legacy_ANTENNA_DEFAULT;
  2333. antenna_rx = B43legacy_ANTENNA_DEFAULT;
  2334. mutex_lock(&wl->mutex);
  2335. dev = wl->current_dev;
  2336. phy = &dev->phy;
  2337. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  2338. b43legacy_set_retry_limits(dev,
  2339. conf->short_frame_max_tx_count,
  2340. conf->long_frame_max_tx_count);
  2341. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  2342. if (!changed)
  2343. goto out_unlock_mutex;
  2344. /* Switch the PHY mode (if necessary). */
  2345. switch (conf->channel->band) {
  2346. case IEEE80211_BAND_2GHZ:
  2347. if (phy->type == B43legacy_PHYTYPE_B)
  2348. new_phymode = B43legacy_PHYMODE_B;
  2349. else
  2350. new_phymode = B43legacy_PHYMODE_G;
  2351. break;
  2352. default:
  2353. B43legacy_WARN_ON(1);
  2354. }
  2355. err = b43legacy_switch_phymode(wl, new_phymode);
  2356. if (err)
  2357. goto out_unlock_mutex;
  2358. /* Disable IRQs while reconfiguring the device.
  2359. * This makes it possible to drop the spinlock throughout
  2360. * the reconfiguration process. */
  2361. spin_lock_irqsave(&wl->irq_lock, flags);
  2362. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2363. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2364. goto out_unlock_mutex;
  2365. }
  2366. savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  2367. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2368. b43legacy_synchronize_irq(dev);
  2369. /* Switch to the requested channel.
  2370. * The firmware takes care of races with the TX handler. */
  2371. if (conf->channel->hw_value != phy->channel)
  2372. b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
  2373. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  2374. /* Adjust the desired TX power level. */
  2375. if (conf->power_level != 0) {
  2376. if (conf->power_level != phy->power_level) {
  2377. phy->power_level = conf->power_level;
  2378. b43legacy_phy_xmitpower(dev);
  2379. }
  2380. }
  2381. /* Antennas for RX and management frame TX. */
  2382. b43legacy_mgmtframe_txantenna(dev, antenna_tx);
  2383. if (!!conf->radio_enabled != phy->radio_on) {
  2384. if (conf->radio_enabled) {
  2385. b43legacy_radio_turn_on(dev);
  2386. b43legacyinfo(dev->wl, "Radio turned on by software\n");
  2387. if (!dev->radio_hw_enable)
  2388. b43legacyinfo(dev->wl, "The hardware RF-kill"
  2389. " button still turns the radio"
  2390. " physically off. Press the"
  2391. " button to turn it on.\n");
  2392. } else {
  2393. b43legacy_radio_turn_off(dev, 0);
  2394. b43legacyinfo(dev->wl, "Radio turned off by"
  2395. " software\n");
  2396. }
  2397. }
  2398. spin_lock_irqsave(&wl->irq_lock, flags);
  2399. b43legacy_interrupt_enable(dev, savedirqs);
  2400. mmiowb();
  2401. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2402. out_unlock_mutex:
  2403. mutex_unlock(&wl->mutex);
  2404. return err;
  2405. }
  2406. static void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
  2407. {
  2408. struct ieee80211_supported_band *sband =
  2409. dev->wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  2410. struct ieee80211_rate *rate;
  2411. int i;
  2412. u16 basic, direct, offset, basic_offset, rateptr;
  2413. for (i = 0; i < sband->n_bitrates; i++) {
  2414. rate = &sband->bitrates[i];
  2415. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2416. direct = B43legacy_SHM_SH_CCKDIRECT;
  2417. basic = B43legacy_SHM_SH_CCKBASIC;
  2418. offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2419. offset &= 0xF;
  2420. } else {
  2421. direct = B43legacy_SHM_SH_OFDMDIRECT;
  2422. basic = B43legacy_SHM_SH_OFDMBASIC;
  2423. offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2424. offset &= 0xF;
  2425. }
  2426. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  2427. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2428. basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2429. basic_offset &= 0xF;
  2430. } else {
  2431. basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2432. basic_offset &= 0xF;
  2433. }
  2434. /*
  2435. * Get the pointer that we need to point to
  2436. * from the direct map
  2437. */
  2438. rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2439. direct + 2 * basic_offset);
  2440. /* and write it to the basic map */
  2441. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2442. basic + 2 * offset, rateptr);
  2443. }
  2444. }
  2445. static void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
  2446. struct ieee80211_vif *vif,
  2447. struct ieee80211_bss_conf *conf,
  2448. u32 changed)
  2449. {
  2450. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2451. struct b43legacy_wldev *dev;
  2452. struct b43legacy_phy *phy;
  2453. unsigned long flags;
  2454. u32 savedirqs;
  2455. mutex_lock(&wl->mutex);
  2456. B43legacy_WARN_ON(wl->vif != vif);
  2457. dev = wl->current_dev;
  2458. phy = &dev->phy;
  2459. /* Disable IRQs while reconfiguring the device.
  2460. * This makes it possible to drop the spinlock throughout
  2461. * the reconfiguration process. */
  2462. spin_lock_irqsave(&wl->irq_lock, flags);
  2463. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2464. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2465. goto out_unlock_mutex;
  2466. }
  2467. savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  2468. if (changed & BSS_CHANGED_BSSID) {
  2469. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2470. b43legacy_synchronize_irq(dev);
  2471. if (conf->bssid)
  2472. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2473. else
  2474. memset(wl->bssid, 0, ETH_ALEN);
  2475. if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
  2476. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP)) {
  2477. B43legacy_WARN_ON(vif->type != NL80211_IFTYPE_AP);
  2478. if (changed & BSS_CHANGED_BEACON)
  2479. b43legacy_update_templates(wl);
  2480. } else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
  2481. if (changed & BSS_CHANGED_BEACON)
  2482. b43legacy_update_templates(wl);
  2483. }
  2484. b43legacy_write_mac_bssid_templates(dev);
  2485. }
  2486. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2487. }
  2488. b43legacy_mac_suspend(dev);
  2489. if (changed & BSS_CHANGED_BEACON_INT &&
  2490. (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
  2491. b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
  2492. b43legacy_set_beacon_int(dev, conf->beacon_int);
  2493. if (changed & BSS_CHANGED_BASIC_RATES)
  2494. b43legacy_update_basic_rates(dev, conf->basic_rates);
  2495. if (changed & BSS_CHANGED_ERP_SLOT) {
  2496. if (conf->use_short_slot)
  2497. b43legacy_short_slot_timing_enable(dev);
  2498. else
  2499. b43legacy_short_slot_timing_disable(dev);
  2500. }
  2501. b43legacy_mac_enable(dev);
  2502. spin_lock_irqsave(&wl->irq_lock, flags);
  2503. b43legacy_interrupt_enable(dev, savedirqs);
  2504. /* XXX: why? */
  2505. mmiowb();
  2506. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2507. out_unlock_mutex:
  2508. mutex_unlock(&wl->mutex);
  2509. }
  2510. static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
  2511. unsigned int changed,
  2512. unsigned int *fflags,
  2513. int mc_count,
  2514. struct dev_addr_list *mc_list)
  2515. {
  2516. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2517. struct b43legacy_wldev *dev = wl->current_dev;
  2518. unsigned long flags;
  2519. if (!dev) {
  2520. *fflags = 0;
  2521. return;
  2522. }
  2523. spin_lock_irqsave(&wl->irq_lock, flags);
  2524. *fflags &= FIF_PROMISC_IN_BSS |
  2525. FIF_ALLMULTI |
  2526. FIF_FCSFAIL |
  2527. FIF_PLCPFAIL |
  2528. FIF_CONTROL |
  2529. FIF_OTHER_BSS |
  2530. FIF_BCN_PRBRESP_PROMISC;
  2531. changed &= FIF_PROMISC_IN_BSS |
  2532. FIF_ALLMULTI |
  2533. FIF_FCSFAIL |
  2534. FIF_PLCPFAIL |
  2535. FIF_CONTROL |
  2536. FIF_OTHER_BSS |
  2537. FIF_BCN_PRBRESP_PROMISC;
  2538. wl->filter_flags = *fflags;
  2539. if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
  2540. b43legacy_adjust_opmode(dev);
  2541. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2542. }
  2543. /* Locking: wl->mutex */
  2544. static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
  2545. {
  2546. struct b43legacy_wl *wl = dev->wl;
  2547. unsigned long flags;
  2548. if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
  2549. return;
  2550. /* Disable and sync interrupts. We must do this before than
  2551. * setting the status to INITIALIZED, as the interrupt handler
  2552. * won't care about IRQs then. */
  2553. spin_lock_irqsave(&wl->irq_lock, flags);
  2554. dev->irq_savedstate = b43legacy_interrupt_disable(dev,
  2555. B43legacy_IRQ_ALL);
  2556. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
  2557. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2558. b43legacy_synchronize_irq(dev);
  2559. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2560. mutex_unlock(&wl->mutex);
  2561. /* Must unlock as it would otherwise deadlock. No races here.
  2562. * Cancel the possibly running self-rearming periodic work. */
  2563. cancel_delayed_work_sync(&dev->periodic_work);
  2564. mutex_lock(&wl->mutex);
  2565. ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
  2566. b43legacy_mac_suspend(dev);
  2567. free_irq(dev->dev->irq, dev);
  2568. b43legacydbg(wl, "Wireless interface stopped\n");
  2569. }
  2570. /* Locking: wl->mutex */
  2571. static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
  2572. {
  2573. int err;
  2574. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
  2575. drain_txstatus_queue(dev);
  2576. err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
  2577. IRQF_SHARED, KBUILD_MODNAME, dev);
  2578. if (err) {
  2579. b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
  2580. dev->dev->irq);
  2581. goto out;
  2582. }
  2583. /* We are ready to run. */
  2584. b43legacy_set_status(dev, B43legacy_STAT_STARTED);
  2585. /* Start data flow (TX/RX) */
  2586. b43legacy_mac_enable(dev);
  2587. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  2588. /* Start maintenance work */
  2589. b43legacy_periodic_tasks_setup(dev);
  2590. b43legacydbg(dev->wl, "Wireless interface started\n");
  2591. out:
  2592. return err;
  2593. }
  2594. /* Get PHY and RADIO versioning numbers */
  2595. static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
  2596. {
  2597. struct b43legacy_phy *phy = &dev->phy;
  2598. u32 tmp;
  2599. u8 analog_type;
  2600. u8 phy_type;
  2601. u8 phy_rev;
  2602. u16 radio_manuf;
  2603. u16 radio_ver;
  2604. u16 radio_rev;
  2605. int unsupported = 0;
  2606. /* Get PHY versioning */
  2607. tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
  2608. analog_type = (tmp & B43legacy_PHYVER_ANALOG)
  2609. >> B43legacy_PHYVER_ANALOG_SHIFT;
  2610. phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
  2611. phy_rev = (tmp & B43legacy_PHYVER_VERSION);
  2612. switch (phy_type) {
  2613. case B43legacy_PHYTYPE_B:
  2614. if (phy_rev != 2 && phy_rev != 4
  2615. && phy_rev != 6 && phy_rev != 7)
  2616. unsupported = 1;
  2617. break;
  2618. case B43legacy_PHYTYPE_G:
  2619. if (phy_rev > 8)
  2620. unsupported = 1;
  2621. break;
  2622. default:
  2623. unsupported = 1;
  2624. };
  2625. if (unsupported) {
  2626. b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
  2627. "(Analog %u, Type %u, Revision %u)\n",
  2628. analog_type, phy_type, phy_rev);
  2629. return -EOPNOTSUPP;
  2630. }
  2631. b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2632. analog_type, phy_type, phy_rev);
  2633. /* Get RADIO versioning */
  2634. if (dev->dev->bus->chip_id == 0x4317) {
  2635. if (dev->dev->bus->chip_rev == 0)
  2636. tmp = 0x3205017F;
  2637. else if (dev->dev->bus->chip_rev == 1)
  2638. tmp = 0x4205017F;
  2639. else
  2640. tmp = 0x5205017F;
  2641. } else {
  2642. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2643. B43legacy_RADIOCTL_ID);
  2644. tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
  2645. tmp <<= 16;
  2646. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2647. B43legacy_RADIOCTL_ID);
  2648. tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
  2649. }
  2650. radio_manuf = (tmp & 0x00000FFF);
  2651. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2652. radio_rev = (tmp & 0xF0000000) >> 28;
  2653. switch (phy_type) {
  2654. case B43legacy_PHYTYPE_B:
  2655. if ((radio_ver & 0xFFF0) != 0x2050)
  2656. unsupported = 1;
  2657. break;
  2658. case B43legacy_PHYTYPE_G:
  2659. if (radio_ver != 0x2050)
  2660. unsupported = 1;
  2661. break;
  2662. default:
  2663. B43legacy_BUG_ON(1);
  2664. }
  2665. if (unsupported) {
  2666. b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
  2667. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2668. radio_manuf, radio_ver, radio_rev);
  2669. return -EOPNOTSUPP;
  2670. }
  2671. b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
  2672. " Revision %u\n", radio_manuf, radio_ver, radio_rev);
  2673. phy->radio_manuf = radio_manuf;
  2674. phy->radio_ver = radio_ver;
  2675. phy->radio_rev = radio_rev;
  2676. phy->analog = analog_type;
  2677. phy->type = phy_type;
  2678. phy->rev = phy_rev;
  2679. return 0;
  2680. }
  2681. static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
  2682. struct b43legacy_phy *phy)
  2683. {
  2684. struct b43legacy_lopair *lo;
  2685. int i;
  2686. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2687. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2688. /* Assume the radio is enabled. If it's not enabled, the state will
  2689. * immediately get fixed on the first periodic work run. */
  2690. dev->radio_hw_enable = 1;
  2691. phy->savedpctlreg = 0xFFFF;
  2692. phy->aci_enable = 0;
  2693. phy->aci_wlan_automatic = 0;
  2694. phy->aci_hw_rssi = 0;
  2695. lo = phy->_lo_pairs;
  2696. if (lo)
  2697. memset(lo, 0, sizeof(struct b43legacy_lopair) *
  2698. B43legacy_LO_COUNT);
  2699. phy->max_lb_gain = 0;
  2700. phy->trsw_rx_gain = 0;
  2701. /* Set default attenuation values. */
  2702. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2703. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2704. phy->txctl1 = b43legacy_default_txctl1(dev);
  2705. phy->txpwr_offset = 0;
  2706. /* NRSSI */
  2707. phy->nrssislope = 0;
  2708. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2709. phy->nrssi[i] = -1000;
  2710. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2711. phy->nrssi_lt[i] = i;
  2712. phy->lofcal = 0xFFFF;
  2713. phy->initval = 0xFFFF;
  2714. phy->interfmode = B43legacy_INTERFMODE_NONE;
  2715. phy->channel = 0xFF;
  2716. }
  2717. static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
  2718. {
  2719. /* Flags */
  2720. dev->dfq_valid = 0;
  2721. /* Stats */
  2722. memset(&dev->stats, 0, sizeof(dev->stats));
  2723. setup_struct_phy_for_init(dev, &dev->phy);
  2724. /* IRQ related flags */
  2725. dev->irq_reason = 0;
  2726. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2727. dev->irq_savedstate = B43legacy_IRQ_MASKTEMPLATE;
  2728. dev->mac_suspended = 1;
  2729. /* Noise calculation context */
  2730. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2731. }
  2732. static void b43legacy_imcfglo_timeouts_workaround(struct b43legacy_wldev *dev)
  2733. {
  2734. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2735. struct ssb_bus *bus = dev->dev->bus;
  2736. u32 tmp;
  2737. if (bus->pcicore.dev &&
  2738. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2739. bus->pcicore.dev->id.revision <= 5) {
  2740. /* IMCFGLO timeouts workaround. */
  2741. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2742. tmp &= ~SSB_IMCFGLO_REQTO;
  2743. tmp &= ~SSB_IMCFGLO_SERTO;
  2744. switch (bus->bustype) {
  2745. case SSB_BUSTYPE_PCI:
  2746. case SSB_BUSTYPE_PCMCIA:
  2747. tmp |= 0x32;
  2748. break;
  2749. case SSB_BUSTYPE_SSB:
  2750. tmp |= 0x53;
  2751. break;
  2752. }
  2753. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2754. }
  2755. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2756. }
  2757. static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
  2758. bool idle) {
  2759. u16 pu_delay = 1050;
  2760. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  2761. pu_delay = 500;
  2762. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  2763. pu_delay = max(pu_delay, (u16)2400);
  2764. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2765. B43legacy_SHM_SH_SPUWKUP, pu_delay);
  2766. }
  2767. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  2768. static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
  2769. {
  2770. u16 pretbtt;
  2771. /* The time value is in microseconds. */
  2772. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  2773. pretbtt = 2;
  2774. else
  2775. pretbtt = 250;
  2776. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2777. B43legacy_SHM_SH_PRETBTT, pretbtt);
  2778. b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
  2779. }
  2780. /* Shutdown a wireless core */
  2781. /* Locking: wl->mutex */
  2782. static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
  2783. {
  2784. struct b43legacy_phy *phy = &dev->phy;
  2785. u32 macctl;
  2786. B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
  2787. if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
  2788. return;
  2789. b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
  2790. /* Stop the microcode PSM. */
  2791. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2792. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  2793. macctl |= B43legacy_MACCTL_PSM_JMP0;
  2794. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  2795. b43legacy_leds_exit(dev);
  2796. b43legacy_rng_exit(dev->wl);
  2797. b43legacy_pio_free(dev);
  2798. b43legacy_dma_free(dev);
  2799. b43legacy_chip_exit(dev);
  2800. b43legacy_radio_turn_off(dev, 1);
  2801. b43legacy_switch_analog(dev, 0);
  2802. if (phy->dyn_tssi_tbl)
  2803. kfree(phy->tssi2dbm);
  2804. kfree(phy->lo_control);
  2805. phy->lo_control = NULL;
  2806. if (dev->wl->current_beacon) {
  2807. dev_kfree_skb_any(dev->wl->current_beacon);
  2808. dev->wl->current_beacon = NULL;
  2809. }
  2810. ssb_device_disable(dev->dev, 0);
  2811. ssb_bus_may_powerdown(dev->dev->bus);
  2812. }
  2813. static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
  2814. {
  2815. struct b43legacy_phy *phy = &dev->phy;
  2816. int i;
  2817. /* Set default attenuation values. */
  2818. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2819. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2820. phy->txctl1 = b43legacy_default_txctl1(dev);
  2821. phy->txctl2 = 0xFFFF;
  2822. phy->txpwr_offset = 0;
  2823. /* NRSSI */
  2824. phy->nrssislope = 0;
  2825. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2826. phy->nrssi[i] = -1000;
  2827. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2828. phy->nrssi_lt[i] = i;
  2829. phy->lofcal = 0xFFFF;
  2830. phy->initval = 0xFFFF;
  2831. phy->aci_enable = 0;
  2832. phy->aci_wlan_automatic = 0;
  2833. phy->aci_hw_rssi = 0;
  2834. phy->antenna_diversity = 0xFFFF;
  2835. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2836. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2837. /* Flags */
  2838. phy->calibrated = 0;
  2839. if (phy->_lo_pairs)
  2840. memset(phy->_lo_pairs, 0,
  2841. sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
  2842. memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
  2843. }
  2844. /* Initialize a wireless core */
  2845. static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
  2846. {
  2847. struct b43legacy_wl *wl = dev->wl;
  2848. struct ssb_bus *bus = dev->dev->bus;
  2849. struct b43legacy_phy *phy = &dev->phy;
  2850. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2851. int err;
  2852. u32 hf;
  2853. u32 tmp;
  2854. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2855. err = ssb_bus_powerup(bus, 0);
  2856. if (err)
  2857. goto out;
  2858. if (!ssb_device_is_enabled(dev->dev)) {
  2859. tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
  2860. b43legacy_wireless_core_reset(dev, tmp);
  2861. }
  2862. if ((phy->type == B43legacy_PHYTYPE_B) ||
  2863. (phy->type == B43legacy_PHYTYPE_G)) {
  2864. phy->_lo_pairs = kzalloc(sizeof(struct b43legacy_lopair)
  2865. * B43legacy_LO_COUNT,
  2866. GFP_KERNEL);
  2867. if (!phy->_lo_pairs)
  2868. return -ENOMEM;
  2869. }
  2870. setup_struct_wldev_for_init(dev);
  2871. err = b43legacy_phy_init_tssi2dbm_table(dev);
  2872. if (err)
  2873. goto err_kfree_lo_control;
  2874. /* Enable IRQ routing to this device. */
  2875. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  2876. b43legacy_imcfglo_timeouts_workaround(dev);
  2877. prepare_phy_data_for_init(dev);
  2878. b43legacy_phy_calibrate(dev);
  2879. err = b43legacy_chip_init(dev);
  2880. if (err)
  2881. goto err_kfree_tssitbl;
  2882. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2883. B43legacy_SHM_SH_WLCOREREV,
  2884. dev->dev->id.revision);
  2885. hf = b43legacy_hf_read(dev);
  2886. if (phy->type == B43legacy_PHYTYPE_G) {
  2887. hf |= B43legacy_HF_SYMW;
  2888. if (phy->rev == 1)
  2889. hf |= B43legacy_HF_GDCW;
  2890. if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
  2891. hf |= B43legacy_HF_OFDMPABOOST;
  2892. } else if (phy->type == B43legacy_PHYTYPE_B) {
  2893. hf |= B43legacy_HF_SYMW;
  2894. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  2895. hf &= ~B43legacy_HF_GDCW;
  2896. }
  2897. b43legacy_hf_write(dev, hf);
  2898. b43legacy_set_retry_limits(dev,
  2899. B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
  2900. B43legacy_DEFAULT_LONG_RETRY_LIMIT);
  2901. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2902. 0x0044, 3);
  2903. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2904. 0x0046, 2);
  2905. /* Disable sending probe responses from firmware.
  2906. * Setting the MaxTime to one usec will always trigger
  2907. * a timeout, so we never send any probe resp.
  2908. * A timeout of zero is infinite. */
  2909. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2910. B43legacy_SHM_SH_PRMAXTIME, 1);
  2911. b43legacy_rate_memory_init(dev);
  2912. /* Minimum Contention Window */
  2913. if (phy->type == B43legacy_PHYTYPE_B)
  2914. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2915. 0x0003, 31);
  2916. else
  2917. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2918. 0x0003, 15);
  2919. /* Maximum Contention Window */
  2920. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2921. 0x0004, 1023);
  2922. do {
  2923. if (b43legacy_using_pio(dev))
  2924. err = b43legacy_pio_init(dev);
  2925. else {
  2926. err = b43legacy_dma_init(dev);
  2927. if (!err)
  2928. b43legacy_qos_init(dev);
  2929. }
  2930. } while (err == -EAGAIN);
  2931. if (err)
  2932. goto err_chip_exit;
  2933. b43legacy_set_synth_pu_delay(dev, 1);
  2934. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  2935. b43legacy_upload_card_macaddress(dev);
  2936. b43legacy_security_init(dev);
  2937. b43legacy_rng_init(wl);
  2938. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2939. b43legacy_leds_init(dev);
  2940. out:
  2941. return err;
  2942. err_chip_exit:
  2943. b43legacy_chip_exit(dev);
  2944. err_kfree_tssitbl:
  2945. if (phy->dyn_tssi_tbl)
  2946. kfree(phy->tssi2dbm);
  2947. err_kfree_lo_control:
  2948. kfree(phy->lo_control);
  2949. phy->lo_control = NULL;
  2950. ssb_bus_may_powerdown(bus);
  2951. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2952. return err;
  2953. }
  2954. static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
  2955. struct ieee80211_if_init_conf *conf)
  2956. {
  2957. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2958. struct b43legacy_wldev *dev;
  2959. unsigned long flags;
  2960. int err = -EOPNOTSUPP;
  2961. /* TODO: allow WDS/AP devices to coexist */
  2962. if (conf->type != NL80211_IFTYPE_AP &&
  2963. conf->type != NL80211_IFTYPE_STATION &&
  2964. conf->type != NL80211_IFTYPE_WDS &&
  2965. conf->type != NL80211_IFTYPE_ADHOC)
  2966. return -EOPNOTSUPP;
  2967. mutex_lock(&wl->mutex);
  2968. if (wl->operating)
  2969. goto out_mutex_unlock;
  2970. b43legacydbg(wl, "Adding Interface type %d\n", conf->type);
  2971. dev = wl->current_dev;
  2972. wl->operating = 1;
  2973. wl->vif = conf->vif;
  2974. wl->if_type = conf->type;
  2975. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  2976. spin_lock_irqsave(&wl->irq_lock, flags);
  2977. b43legacy_adjust_opmode(dev);
  2978. b43legacy_set_pretbtt(dev);
  2979. b43legacy_set_synth_pu_delay(dev, 0);
  2980. b43legacy_upload_card_macaddress(dev);
  2981. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2982. err = 0;
  2983. out_mutex_unlock:
  2984. mutex_unlock(&wl->mutex);
  2985. return err;
  2986. }
  2987. static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
  2988. struct ieee80211_if_init_conf *conf)
  2989. {
  2990. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2991. struct b43legacy_wldev *dev = wl->current_dev;
  2992. unsigned long flags;
  2993. b43legacydbg(wl, "Removing Interface type %d\n", conf->type);
  2994. mutex_lock(&wl->mutex);
  2995. B43legacy_WARN_ON(!wl->operating);
  2996. B43legacy_WARN_ON(wl->vif != conf->vif);
  2997. wl->vif = NULL;
  2998. wl->operating = 0;
  2999. spin_lock_irqsave(&wl->irq_lock, flags);
  3000. b43legacy_adjust_opmode(dev);
  3001. memset(wl->mac_addr, 0, ETH_ALEN);
  3002. b43legacy_upload_card_macaddress(dev);
  3003. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3004. mutex_unlock(&wl->mutex);
  3005. }
  3006. static int b43legacy_op_start(struct ieee80211_hw *hw)
  3007. {
  3008. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3009. struct b43legacy_wldev *dev = wl->current_dev;
  3010. int did_init = 0;
  3011. int err = 0;
  3012. bool do_rfkill_exit = 0;
  3013. /* First register RFkill.
  3014. * LEDs that are registered later depend on it. */
  3015. b43legacy_rfkill_init(dev);
  3016. /* Kill all old instance specific information to make sure
  3017. * the card won't use it in the short timeframe between start
  3018. * and mac80211 reconfiguring it. */
  3019. memset(wl->bssid, 0, ETH_ALEN);
  3020. memset(wl->mac_addr, 0, ETH_ALEN);
  3021. wl->filter_flags = 0;
  3022. wl->beacon0_uploaded = 0;
  3023. wl->beacon1_uploaded = 0;
  3024. wl->beacon_templates_virgin = 1;
  3025. mutex_lock(&wl->mutex);
  3026. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
  3027. err = b43legacy_wireless_core_init(dev);
  3028. if (err) {
  3029. do_rfkill_exit = 1;
  3030. goto out_mutex_unlock;
  3031. }
  3032. did_init = 1;
  3033. }
  3034. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  3035. err = b43legacy_wireless_core_start(dev);
  3036. if (err) {
  3037. if (did_init)
  3038. b43legacy_wireless_core_exit(dev);
  3039. do_rfkill_exit = 1;
  3040. goto out_mutex_unlock;
  3041. }
  3042. }
  3043. out_mutex_unlock:
  3044. mutex_unlock(&wl->mutex);
  3045. if (do_rfkill_exit)
  3046. b43legacy_rfkill_exit(dev);
  3047. return err;
  3048. }
  3049. static void b43legacy_op_stop(struct ieee80211_hw *hw)
  3050. {
  3051. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3052. struct b43legacy_wldev *dev = wl->current_dev;
  3053. b43legacy_rfkill_exit(dev);
  3054. cancel_work_sync(&(wl->beacon_update_trigger));
  3055. mutex_lock(&wl->mutex);
  3056. if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
  3057. b43legacy_wireless_core_stop(dev);
  3058. b43legacy_wireless_core_exit(dev);
  3059. mutex_unlock(&wl->mutex);
  3060. }
  3061. static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
  3062. struct ieee80211_sta *sta, bool set)
  3063. {
  3064. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3065. unsigned long flags;
  3066. spin_lock_irqsave(&wl->irq_lock, flags);
  3067. b43legacy_update_templates(wl);
  3068. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3069. return 0;
  3070. }
  3071. static const struct ieee80211_ops b43legacy_hw_ops = {
  3072. .tx = b43legacy_op_tx,
  3073. .conf_tx = b43legacy_op_conf_tx,
  3074. .add_interface = b43legacy_op_add_interface,
  3075. .remove_interface = b43legacy_op_remove_interface,
  3076. .config = b43legacy_op_dev_config,
  3077. .bss_info_changed = b43legacy_op_bss_info_changed,
  3078. .configure_filter = b43legacy_op_configure_filter,
  3079. .get_stats = b43legacy_op_get_stats,
  3080. .get_tx_stats = b43legacy_op_get_tx_stats,
  3081. .start = b43legacy_op_start,
  3082. .stop = b43legacy_op_stop,
  3083. .set_tim = b43legacy_op_beacon_set_tim,
  3084. };
  3085. /* Hard-reset the chip. Do not call this directly.
  3086. * Use b43legacy_controller_restart()
  3087. */
  3088. static void b43legacy_chip_reset(struct work_struct *work)
  3089. {
  3090. struct b43legacy_wldev *dev =
  3091. container_of(work, struct b43legacy_wldev, restart_work);
  3092. struct b43legacy_wl *wl = dev->wl;
  3093. int err = 0;
  3094. int prev_status;
  3095. mutex_lock(&wl->mutex);
  3096. prev_status = b43legacy_status(dev);
  3097. /* Bring the device down... */
  3098. if (prev_status >= B43legacy_STAT_STARTED)
  3099. b43legacy_wireless_core_stop(dev);
  3100. if (prev_status >= B43legacy_STAT_INITIALIZED)
  3101. b43legacy_wireless_core_exit(dev);
  3102. /* ...and up again. */
  3103. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  3104. err = b43legacy_wireless_core_init(dev);
  3105. if (err)
  3106. goto out;
  3107. }
  3108. if (prev_status >= B43legacy_STAT_STARTED) {
  3109. err = b43legacy_wireless_core_start(dev);
  3110. if (err) {
  3111. b43legacy_wireless_core_exit(dev);
  3112. goto out;
  3113. }
  3114. }
  3115. out:
  3116. if (err)
  3117. wl->current_dev = NULL; /* Failed to init the dev. */
  3118. mutex_unlock(&wl->mutex);
  3119. if (err)
  3120. b43legacyerr(wl, "Controller restart FAILED\n");
  3121. else
  3122. b43legacyinfo(wl, "Controller restarted\n");
  3123. }
  3124. static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
  3125. int have_bphy,
  3126. int have_gphy)
  3127. {
  3128. struct ieee80211_hw *hw = dev->wl->hw;
  3129. struct b43legacy_phy *phy = &dev->phy;
  3130. phy->possible_phymodes = 0;
  3131. if (have_bphy) {
  3132. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3133. &b43legacy_band_2GHz_BPHY;
  3134. phy->possible_phymodes |= B43legacy_PHYMODE_B;
  3135. }
  3136. if (have_gphy) {
  3137. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3138. &b43legacy_band_2GHz_GPHY;
  3139. phy->possible_phymodes |= B43legacy_PHYMODE_G;
  3140. }
  3141. return 0;
  3142. }
  3143. static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
  3144. {
  3145. /* We release firmware that late to not be required to re-request
  3146. * is all the time when we reinit the core. */
  3147. b43legacy_release_firmware(dev);
  3148. }
  3149. static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
  3150. {
  3151. struct b43legacy_wl *wl = dev->wl;
  3152. struct ssb_bus *bus = dev->dev->bus;
  3153. struct pci_dev *pdev = bus->host_pci;
  3154. int err;
  3155. int have_bphy = 0;
  3156. int have_gphy = 0;
  3157. u32 tmp;
  3158. /* Do NOT do any device initialization here.
  3159. * Do it in wireless_core_init() instead.
  3160. * This function is for gathering basic information about the HW, only.
  3161. * Also some structs may be set up here. But most likely you want to
  3162. * have that in core_init(), too.
  3163. */
  3164. err = ssb_bus_powerup(bus, 0);
  3165. if (err) {
  3166. b43legacyerr(wl, "Bus powerup failed\n");
  3167. goto out;
  3168. }
  3169. /* Get the PHY type. */
  3170. if (dev->dev->id.revision >= 5) {
  3171. u32 tmshigh;
  3172. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3173. have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
  3174. if (!have_gphy)
  3175. have_bphy = 1;
  3176. } else if (dev->dev->id.revision == 4)
  3177. have_gphy = 1;
  3178. else
  3179. have_bphy = 1;
  3180. dev->phy.gmode = (have_gphy || have_bphy);
  3181. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3182. b43legacy_wireless_core_reset(dev, tmp);
  3183. err = b43legacy_phy_versioning(dev);
  3184. if (err)
  3185. goto err_powerdown;
  3186. /* Check if this device supports multiband. */
  3187. if (!pdev ||
  3188. (pdev->device != 0x4312 &&
  3189. pdev->device != 0x4319 &&
  3190. pdev->device != 0x4324)) {
  3191. /* No multiband support. */
  3192. have_bphy = 0;
  3193. have_gphy = 0;
  3194. switch (dev->phy.type) {
  3195. case B43legacy_PHYTYPE_B:
  3196. have_bphy = 1;
  3197. break;
  3198. case B43legacy_PHYTYPE_G:
  3199. have_gphy = 1;
  3200. break;
  3201. default:
  3202. B43legacy_BUG_ON(1);
  3203. }
  3204. }
  3205. dev->phy.gmode = (have_gphy || have_bphy);
  3206. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3207. b43legacy_wireless_core_reset(dev, tmp);
  3208. err = b43legacy_validate_chipaccess(dev);
  3209. if (err)
  3210. goto err_powerdown;
  3211. err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
  3212. if (err)
  3213. goto err_powerdown;
  3214. /* Now set some default "current_dev" */
  3215. if (!wl->current_dev)
  3216. wl->current_dev = dev;
  3217. INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
  3218. b43legacy_radio_turn_off(dev, 1);
  3219. b43legacy_switch_analog(dev, 0);
  3220. ssb_device_disable(dev->dev, 0);
  3221. ssb_bus_may_powerdown(bus);
  3222. out:
  3223. return err;
  3224. err_powerdown:
  3225. ssb_bus_may_powerdown(bus);
  3226. return err;
  3227. }
  3228. static void b43legacy_one_core_detach(struct ssb_device *dev)
  3229. {
  3230. struct b43legacy_wldev *wldev;
  3231. struct b43legacy_wl *wl;
  3232. /* Do not cancel ieee80211-workqueue based work here.
  3233. * See comment in b43legacy_remove(). */
  3234. wldev = ssb_get_drvdata(dev);
  3235. wl = wldev->wl;
  3236. b43legacy_debugfs_remove_device(wldev);
  3237. b43legacy_wireless_core_detach(wldev);
  3238. list_del(&wldev->list);
  3239. wl->nr_devs--;
  3240. ssb_set_drvdata(dev, NULL);
  3241. kfree(wldev);
  3242. }
  3243. static int b43legacy_one_core_attach(struct ssb_device *dev,
  3244. struct b43legacy_wl *wl)
  3245. {
  3246. struct b43legacy_wldev *wldev;
  3247. struct pci_dev *pdev;
  3248. int err = -ENOMEM;
  3249. if (!list_empty(&wl->devlist)) {
  3250. /* We are not the first core on this chip. */
  3251. pdev = dev->bus->host_pci;
  3252. /* Only special chips support more than one wireless
  3253. * core, although some of the other chips have more than
  3254. * one wireless core as well. Check for this and
  3255. * bail out early.
  3256. */
  3257. if (!pdev ||
  3258. ((pdev->device != 0x4321) &&
  3259. (pdev->device != 0x4313) &&
  3260. (pdev->device != 0x431A))) {
  3261. b43legacydbg(wl, "Ignoring unconnected 802.11 core\n");
  3262. return -ENODEV;
  3263. }
  3264. }
  3265. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3266. if (!wldev)
  3267. goto out;
  3268. wldev->dev = dev;
  3269. wldev->wl = wl;
  3270. b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
  3271. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3272. tasklet_init(&wldev->isr_tasklet,
  3273. (void (*)(unsigned long))b43legacy_interrupt_tasklet,
  3274. (unsigned long)wldev);
  3275. if (modparam_pio)
  3276. wldev->__using_pio = 1;
  3277. INIT_LIST_HEAD(&wldev->list);
  3278. err = b43legacy_wireless_core_attach(wldev);
  3279. if (err)
  3280. goto err_kfree_wldev;
  3281. list_add(&wldev->list, &wl->devlist);
  3282. wl->nr_devs++;
  3283. ssb_set_drvdata(dev, wldev);
  3284. b43legacy_debugfs_add_device(wldev);
  3285. out:
  3286. return err;
  3287. err_kfree_wldev:
  3288. kfree(wldev);
  3289. return err;
  3290. }
  3291. static void b43legacy_sprom_fixup(struct ssb_bus *bus)
  3292. {
  3293. /* boardflags workarounds */
  3294. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3295. bus->boardinfo.type == 0x4E &&
  3296. bus->boardinfo.rev > 0x40)
  3297. bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
  3298. }
  3299. static void b43legacy_wireless_exit(struct ssb_device *dev,
  3300. struct b43legacy_wl *wl)
  3301. {
  3302. struct ieee80211_hw *hw = wl->hw;
  3303. ssb_set_devtypedata(dev, NULL);
  3304. ieee80211_free_hw(hw);
  3305. }
  3306. static int b43legacy_wireless_init(struct ssb_device *dev)
  3307. {
  3308. struct ssb_sprom *sprom = &dev->bus->sprom;
  3309. struct ieee80211_hw *hw;
  3310. struct b43legacy_wl *wl;
  3311. int err = -ENOMEM;
  3312. b43legacy_sprom_fixup(dev->bus);
  3313. hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
  3314. if (!hw) {
  3315. b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
  3316. goto out;
  3317. }
  3318. /* fill hw info */
  3319. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  3320. IEEE80211_HW_SIGNAL_DBM |
  3321. IEEE80211_HW_NOISE_DBM;
  3322. hw->wiphy->interface_modes =
  3323. BIT(NL80211_IFTYPE_AP) |
  3324. BIT(NL80211_IFTYPE_STATION) |
  3325. BIT(NL80211_IFTYPE_WDS) |
  3326. BIT(NL80211_IFTYPE_ADHOC);
  3327. hw->queues = 1; /* FIXME: hardware has more queues */
  3328. hw->max_rates = 2;
  3329. SET_IEEE80211_DEV(hw, dev->dev);
  3330. if (is_valid_ether_addr(sprom->et1mac))
  3331. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  3332. else
  3333. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  3334. /* Get and initialize struct b43legacy_wl */
  3335. wl = hw_to_b43legacy_wl(hw);
  3336. memset(wl, 0, sizeof(*wl));
  3337. wl->hw = hw;
  3338. spin_lock_init(&wl->irq_lock);
  3339. spin_lock_init(&wl->leds_lock);
  3340. mutex_init(&wl->mutex);
  3341. INIT_LIST_HEAD(&wl->devlist);
  3342. INIT_WORK(&wl->beacon_update_trigger, b43legacy_beacon_update_trigger_work);
  3343. ssb_set_devtypedata(dev, wl);
  3344. b43legacyinfo(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3345. err = 0;
  3346. out:
  3347. return err;
  3348. }
  3349. static int b43legacy_probe(struct ssb_device *dev,
  3350. const struct ssb_device_id *id)
  3351. {
  3352. struct b43legacy_wl *wl;
  3353. int err;
  3354. int first = 0;
  3355. wl = ssb_get_devtypedata(dev);
  3356. if (!wl) {
  3357. /* Probing the first core - setup common struct b43legacy_wl */
  3358. first = 1;
  3359. err = b43legacy_wireless_init(dev);
  3360. if (err)
  3361. goto out;
  3362. wl = ssb_get_devtypedata(dev);
  3363. B43legacy_WARN_ON(!wl);
  3364. }
  3365. err = b43legacy_one_core_attach(dev, wl);
  3366. if (err)
  3367. goto err_wireless_exit;
  3368. if (first) {
  3369. err = ieee80211_register_hw(wl->hw);
  3370. if (err)
  3371. goto err_one_core_detach;
  3372. }
  3373. out:
  3374. return err;
  3375. err_one_core_detach:
  3376. b43legacy_one_core_detach(dev);
  3377. err_wireless_exit:
  3378. if (first)
  3379. b43legacy_wireless_exit(dev, wl);
  3380. return err;
  3381. }
  3382. static void b43legacy_remove(struct ssb_device *dev)
  3383. {
  3384. struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
  3385. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3386. /* We must cancel any work here before unregistering from ieee80211,
  3387. * as the ieee80211 unreg will destroy the workqueue. */
  3388. cancel_work_sync(&wldev->restart_work);
  3389. B43legacy_WARN_ON(!wl);
  3390. if (wl->current_dev == wldev)
  3391. ieee80211_unregister_hw(wl->hw);
  3392. b43legacy_one_core_detach(dev);
  3393. if (list_empty(&wl->devlist))
  3394. /* Last core on the chip unregistered.
  3395. * We can destroy common struct b43legacy_wl.
  3396. */
  3397. b43legacy_wireless_exit(dev, wl);
  3398. }
  3399. /* Perform a hardware reset. This can be called from any context. */
  3400. void b43legacy_controller_restart(struct b43legacy_wldev *dev,
  3401. const char *reason)
  3402. {
  3403. /* Must avoid requeueing, if we are in shutdown. */
  3404. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
  3405. return;
  3406. b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
  3407. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  3408. }
  3409. #ifdef CONFIG_PM
  3410. static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
  3411. {
  3412. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3413. struct b43legacy_wl *wl = wldev->wl;
  3414. b43legacydbg(wl, "Suspending...\n");
  3415. mutex_lock(&wl->mutex);
  3416. wldev->suspend_init_status = b43legacy_status(wldev);
  3417. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
  3418. b43legacy_wireless_core_stop(wldev);
  3419. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
  3420. b43legacy_wireless_core_exit(wldev);
  3421. mutex_unlock(&wl->mutex);
  3422. b43legacydbg(wl, "Device suspended.\n");
  3423. return 0;
  3424. }
  3425. static int b43legacy_resume(struct ssb_device *dev)
  3426. {
  3427. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3428. struct b43legacy_wl *wl = wldev->wl;
  3429. int err = 0;
  3430. b43legacydbg(wl, "Resuming...\n");
  3431. mutex_lock(&wl->mutex);
  3432. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
  3433. err = b43legacy_wireless_core_init(wldev);
  3434. if (err) {
  3435. b43legacyerr(wl, "Resume failed at core init\n");
  3436. goto out;
  3437. }
  3438. }
  3439. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
  3440. err = b43legacy_wireless_core_start(wldev);
  3441. if (err) {
  3442. b43legacy_wireless_core_exit(wldev);
  3443. b43legacyerr(wl, "Resume failed at core start\n");
  3444. goto out;
  3445. }
  3446. }
  3447. b43legacydbg(wl, "Device resumed.\n");
  3448. out:
  3449. mutex_unlock(&wl->mutex);
  3450. return err;
  3451. }
  3452. #else /* CONFIG_PM */
  3453. # define b43legacy_suspend NULL
  3454. # define b43legacy_resume NULL
  3455. #endif /* CONFIG_PM */
  3456. static struct ssb_driver b43legacy_ssb_driver = {
  3457. .name = KBUILD_MODNAME,
  3458. .id_table = b43legacy_ssb_tbl,
  3459. .probe = b43legacy_probe,
  3460. .remove = b43legacy_remove,
  3461. .suspend = b43legacy_suspend,
  3462. .resume = b43legacy_resume,
  3463. };
  3464. static void b43legacy_print_driverinfo(void)
  3465. {
  3466. const char *feat_pci = "", *feat_leds = "", *feat_rfkill = "",
  3467. *feat_pio = "", *feat_dma = "";
  3468. #ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
  3469. feat_pci = "P";
  3470. #endif
  3471. #ifdef CONFIG_B43LEGACY_LEDS
  3472. feat_leds = "L";
  3473. #endif
  3474. #ifdef CONFIG_B43LEGACY_RFKILL
  3475. feat_rfkill = "R";
  3476. #endif
  3477. #ifdef CONFIG_B43LEGACY_PIO
  3478. feat_pio = "I";
  3479. #endif
  3480. #ifdef CONFIG_B43LEGACY_DMA
  3481. feat_dma = "D";
  3482. #endif
  3483. printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
  3484. "[ Features: %s%s%s%s%s, Firmware-ID: "
  3485. B43legacy_SUPPORTED_FIRMWARE_ID " ]\n",
  3486. feat_pci, feat_leds, feat_rfkill, feat_pio, feat_dma);
  3487. }
  3488. static int __init b43legacy_init(void)
  3489. {
  3490. int err;
  3491. b43legacy_debugfs_init();
  3492. err = ssb_driver_register(&b43legacy_ssb_driver);
  3493. if (err)
  3494. goto err_dfs_exit;
  3495. b43legacy_print_driverinfo();
  3496. return err;
  3497. err_dfs_exit:
  3498. b43legacy_debugfs_exit();
  3499. return err;
  3500. }
  3501. static void __exit b43legacy_exit(void)
  3502. {
  3503. ssb_driver_unregister(&b43legacy_ssb_driver);
  3504. b43legacy_debugfs_exit();
  3505. }
  3506. module_init(b43legacy_init)
  3507. module_exit(b43legacy_exit)