main.c 102 KB

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