main.c 103 KB

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