main.c 108 KB

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