main.c 107 KB

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