main.c 105 KB

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