main.c 109 KB

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