main.c 102 KB

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