main.c 110 KB

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