main.c 108 KB

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