main.c 108 KB

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