main.c 108 KB

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