main.c 105 KB

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