main.c 108 KB

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