main.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006
  1. /*
  2. Broadcom B43 wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
  4. Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
  5. Copyright (c) 2005-2009 Michael Buesch <mb@bu3sch.de>
  6. Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  7. Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Some parts of the code in this file are derived from the ipw2200
  9. driver Copyright(c) 2003 - 2004 Intel Corporation.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  21. Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/firmware.h>
  29. #include <linux/wireless.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/io.h>
  33. #include <linux/dma-mapping.h>
  34. #include <asm/unaligned.h>
  35. #include "b43.h"
  36. #include "main.h"
  37. #include "debugfs.h"
  38. #include "phy_common.h"
  39. #include "phy_g.h"
  40. #include "phy_n.h"
  41. #include "dma.h"
  42. #include "pio.h"
  43. #include "sysfs.h"
  44. #include "xmit.h"
  45. #include "lo.h"
  46. #include "pcmcia.h"
  47. MODULE_DESCRIPTION("Broadcom B43 wireless driver");
  48. MODULE_AUTHOR("Martin Langer");
  49. MODULE_AUTHOR("Stefano Brivio");
  50. MODULE_AUTHOR("Michael Buesch");
  51. MODULE_LICENSE("GPL");
  52. MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
  53. static int modparam_bad_frames_preempt;
  54. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  55. MODULE_PARM_DESC(bad_frames_preempt,
  56. "enable(1) / disable(0) Bad Frames Preemption");
  57. static char modparam_fwpostfix[16];
  58. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  59. MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
  60. static int modparam_hwpctl;
  61. module_param_named(hwpctl, modparam_hwpctl, int, 0444);
  62. MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
  63. static int modparam_nohwcrypt;
  64. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  65. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  66. int b43_modparam_qos = 1;
  67. module_param_named(qos, b43_modparam_qos, int, 0444);
  68. MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
  69. static int modparam_btcoex = 1;
  70. module_param_named(btcoex, modparam_btcoex, int, 0444);
  71. MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
  72. int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
  73. module_param_named(verbose, b43_modparam_verbose, int, 0644);
  74. MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
  75. static const struct ssb_device_id b43_ssb_tbl[] = {
  76. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
  77. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
  78. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
  79. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
  80. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
  81. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
  82. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
  83. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
  84. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
  85. SSB_DEVTABLE_END
  86. };
  87. MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
  88. /* Channel and ratetables are shared for all devices.
  89. * They can't be const, because ieee80211 puts some precalculated
  90. * data in there. This data is the same for all devices, so we don't
  91. * get concurrency issues */
  92. #define RATETAB_ENT(_rateid, _flags) \
  93. { \
  94. .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
  95. .hw_value = (_rateid), \
  96. .flags = (_flags), \
  97. }
  98. /*
  99. * NOTE: When changing this, sync with xmit.c's
  100. * b43_plcp_get_bitrate_idx_* functions!
  101. */
  102. static struct ieee80211_rate __b43_ratetable[] = {
  103. RATETAB_ENT(B43_CCK_RATE_1MB, 0),
  104. RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
  105. RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
  106. RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
  107. RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
  108. RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
  109. RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
  110. RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
  111. RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
  112. RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
  113. RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
  114. RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
  115. };
  116. #define b43_a_ratetable (__b43_ratetable + 4)
  117. #define b43_a_ratetable_size 8
  118. #define b43_b_ratetable (__b43_ratetable + 0)
  119. #define b43_b_ratetable_size 4
  120. #define b43_g_ratetable (__b43_ratetable + 0)
  121. #define b43_g_ratetable_size 12
  122. #define CHAN4G(_channel, _freq, _flags) { \
  123. .band = IEEE80211_BAND_2GHZ, \
  124. .center_freq = (_freq), \
  125. .hw_value = (_channel), \
  126. .flags = (_flags), \
  127. .max_antenna_gain = 0, \
  128. .max_power = 30, \
  129. }
  130. static struct ieee80211_channel b43_2ghz_chantable[] = {
  131. CHAN4G(1, 2412, 0),
  132. CHAN4G(2, 2417, 0),
  133. CHAN4G(3, 2422, 0),
  134. CHAN4G(4, 2427, 0),
  135. CHAN4G(5, 2432, 0),
  136. CHAN4G(6, 2437, 0),
  137. CHAN4G(7, 2442, 0),
  138. CHAN4G(8, 2447, 0),
  139. CHAN4G(9, 2452, 0),
  140. CHAN4G(10, 2457, 0),
  141. CHAN4G(11, 2462, 0),
  142. CHAN4G(12, 2467, 0),
  143. CHAN4G(13, 2472, 0),
  144. CHAN4G(14, 2484, 0),
  145. };
  146. #undef CHAN4G
  147. #define CHAN5G(_channel, _flags) { \
  148. .band = IEEE80211_BAND_5GHZ, \
  149. .center_freq = 5000 + (5 * (_channel)), \
  150. .hw_value = (_channel), \
  151. .flags = (_flags), \
  152. .max_antenna_gain = 0, \
  153. .max_power = 30, \
  154. }
  155. static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
  156. CHAN5G(32, 0), CHAN5G(34, 0),
  157. CHAN5G(36, 0), CHAN5G(38, 0),
  158. CHAN5G(40, 0), CHAN5G(42, 0),
  159. CHAN5G(44, 0), CHAN5G(46, 0),
  160. CHAN5G(48, 0), CHAN5G(50, 0),
  161. CHAN5G(52, 0), CHAN5G(54, 0),
  162. CHAN5G(56, 0), CHAN5G(58, 0),
  163. CHAN5G(60, 0), CHAN5G(62, 0),
  164. CHAN5G(64, 0), CHAN5G(66, 0),
  165. CHAN5G(68, 0), CHAN5G(70, 0),
  166. CHAN5G(72, 0), CHAN5G(74, 0),
  167. CHAN5G(76, 0), CHAN5G(78, 0),
  168. CHAN5G(80, 0), CHAN5G(82, 0),
  169. CHAN5G(84, 0), CHAN5G(86, 0),
  170. CHAN5G(88, 0), CHAN5G(90, 0),
  171. CHAN5G(92, 0), CHAN5G(94, 0),
  172. CHAN5G(96, 0), CHAN5G(98, 0),
  173. CHAN5G(100, 0), CHAN5G(102, 0),
  174. CHAN5G(104, 0), CHAN5G(106, 0),
  175. CHAN5G(108, 0), CHAN5G(110, 0),
  176. CHAN5G(112, 0), CHAN5G(114, 0),
  177. CHAN5G(116, 0), CHAN5G(118, 0),
  178. CHAN5G(120, 0), CHAN5G(122, 0),
  179. CHAN5G(124, 0), CHAN5G(126, 0),
  180. CHAN5G(128, 0), CHAN5G(130, 0),
  181. CHAN5G(132, 0), CHAN5G(134, 0),
  182. CHAN5G(136, 0), CHAN5G(138, 0),
  183. CHAN5G(140, 0), CHAN5G(142, 0),
  184. CHAN5G(144, 0), CHAN5G(145, 0),
  185. CHAN5G(146, 0), CHAN5G(147, 0),
  186. CHAN5G(148, 0), CHAN5G(149, 0),
  187. CHAN5G(150, 0), CHAN5G(151, 0),
  188. CHAN5G(152, 0), CHAN5G(153, 0),
  189. CHAN5G(154, 0), CHAN5G(155, 0),
  190. CHAN5G(156, 0), CHAN5G(157, 0),
  191. CHAN5G(158, 0), CHAN5G(159, 0),
  192. CHAN5G(160, 0), CHAN5G(161, 0),
  193. CHAN5G(162, 0), CHAN5G(163, 0),
  194. CHAN5G(164, 0), CHAN5G(165, 0),
  195. CHAN5G(166, 0), CHAN5G(168, 0),
  196. CHAN5G(170, 0), CHAN5G(172, 0),
  197. CHAN5G(174, 0), CHAN5G(176, 0),
  198. CHAN5G(178, 0), CHAN5G(180, 0),
  199. CHAN5G(182, 0), CHAN5G(184, 0),
  200. CHAN5G(186, 0), CHAN5G(188, 0),
  201. CHAN5G(190, 0), CHAN5G(192, 0),
  202. CHAN5G(194, 0), CHAN5G(196, 0),
  203. CHAN5G(198, 0), CHAN5G(200, 0),
  204. CHAN5G(202, 0), CHAN5G(204, 0),
  205. CHAN5G(206, 0), CHAN5G(208, 0),
  206. CHAN5G(210, 0), CHAN5G(212, 0),
  207. CHAN5G(214, 0), CHAN5G(216, 0),
  208. CHAN5G(218, 0), CHAN5G(220, 0),
  209. CHAN5G(222, 0), CHAN5G(224, 0),
  210. CHAN5G(226, 0), CHAN5G(228, 0),
  211. };
  212. static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
  213. CHAN5G(34, 0), CHAN5G(36, 0),
  214. CHAN5G(38, 0), CHAN5G(40, 0),
  215. CHAN5G(42, 0), CHAN5G(44, 0),
  216. CHAN5G(46, 0), CHAN5G(48, 0),
  217. CHAN5G(52, 0), CHAN5G(56, 0),
  218. CHAN5G(60, 0), CHAN5G(64, 0),
  219. CHAN5G(100, 0), CHAN5G(104, 0),
  220. CHAN5G(108, 0), CHAN5G(112, 0),
  221. CHAN5G(116, 0), CHAN5G(120, 0),
  222. CHAN5G(124, 0), CHAN5G(128, 0),
  223. CHAN5G(132, 0), CHAN5G(136, 0),
  224. CHAN5G(140, 0), CHAN5G(149, 0),
  225. CHAN5G(153, 0), CHAN5G(157, 0),
  226. CHAN5G(161, 0), CHAN5G(165, 0),
  227. CHAN5G(184, 0), CHAN5G(188, 0),
  228. CHAN5G(192, 0), CHAN5G(196, 0),
  229. CHAN5G(200, 0), CHAN5G(204, 0),
  230. CHAN5G(208, 0), CHAN5G(212, 0),
  231. CHAN5G(216, 0),
  232. };
  233. #undef CHAN5G
  234. static struct ieee80211_supported_band b43_band_5GHz_nphy = {
  235. .band = IEEE80211_BAND_5GHZ,
  236. .channels = b43_5ghz_nphy_chantable,
  237. .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
  238. .bitrates = b43_a_ratetable,
  239. .n_bitrates = b43_a_ratetable_size,
  240. };
  241. static struct ieee80211_supported_band b43_band_5GHz_aphy = {
  242. .band = IEEE80211_BAND_5GHZ,
  243. .channels = b43_5ghz_aphy_chantable,
  244. .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
  245. .bitrates = b43_a_ratetable,
  246. .n_bitrates = b43_a_ratetable_size,
  247. };
  248. static struct ieee80211_supported_band b43_band_2GHz = {
  249. .band = IEEE80211_BAND_2GHZ,
  250. .channels = b43_2ghz_chantable,
  251. .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
  252. .bitrates = b43_g_ratetable,
  253. .n_bitrates = b43_g_ratetable_size,
  254. };
  255. static void b43_wireless_core_exit(struct b43_wldev *dev);
  256. static int b43_wireless_core_init(struct b43_wldev *dev);
  257. static void b43_wireless_core_stop(struct b43_wldev *dev);
  258. static int b43_wireless_core_start(struct b43_wldev *dev);
  259. static int b43_ratelimit(struct b43_wl *wl)
  260. {
  261. if (!wl || !wl->current_dev)
  262. return 1;
  263. if (b43_status(wl->current_dev) < B43_STAT_STARTED)
  264. return 1;
  265. /* We are up and running.
  266. * Ratelimit the messages to avoid DoS over the net. */
  267. return net_ratelimit();
  268. }
  269. void b43info(struct b43_wl *wl, const char *fmt, ...)
  270. {
  271. va_list args;
  272. if (b43_modparam_verbose < B43_VERBOSITY_INFO)
  273. return;
  274. if (!b43_ratelimit(wl))
  275. return;
  276. va_start(args, fmt);
  277. printk(KERN_INFO "b43-%s: ",
  278. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  279. vprintk(fmt, args);
  280. va_end(args);
  281. }
  282. void b43err(struct b43_wl *wl, const char *fmt, ...)
  283. {
  284. va_list args;
  285. if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
  286. return;
  287. if (!b43_ratelimit(wl))
  288. return;
  289. va_start(args, fmt);
  290. printk(KERN_ERR "b43-%s ERROR: ",
  291. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  292. vprintk(fmt, args);
  293. va_end(args);
  294. }
  295. void b43warn(struct b43_wl *wl, const char *fmt, ...)
  296. {
  297. va_list args;
  298. if (b43_modparam_verbose < B43_VERBOSITY_WARN)
  299. return;
  300. if (!b43_ratelimit(wl))
  301. return;
  302. va_start(args, fmt);
  303. printk(KERN_WARNING "b43-%s warning: ",
  304. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  305. vprintk(fmt, args);
  306. va_end(args);
  307. }
  308. void b43dbg(struct b43_wl *wl, const char *fmt, ...)
  309. {
  310. va_list args;
  311. if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
  312. return;
  313. va_start(args, fmt);
  314. printk(KERN_DEBUG "b43-%s debug: ",
  315. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  316. vprintk(fmt, args);
  317. va_end(args);
  318. }
  319. static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
  320. {
  321. u32 macctl;
  322. B43_WARN_ON(offset % 4 != 0);
  323. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  324. if (macctl & B43_MACCTL_BE)
  325. val = swab32(val);
  326. b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
  327. mmiowb();
  328. b43_write32(dev, B43_MMIO_RAM_DATA, val);
  329. }
  330. static inline void b43_shm_control_word(struct b43_wldev *dev,
  331. u16 routing, u16 offset)
  332. {
  333. u32 control;
  334. /* "offset" is the WORD offset. */
  335. control = routing;
  336. control <<= 16;
  337. control |= offset;
  338. b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
  339. }
  340. u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  341. {
  342. u32 ret;
  343. if (routing == B43_SHM_SHARED) {
  344. B43_WARN_ON(offset & 0x0001);
  345. if (offset & 0x0003) {
  346. /* Unaligned access */
  347. b43_shm_control_word(dev, routing, offset >> 2);
  348. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  349. ret <<= 16;
  350. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  351. ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
  352. goto out;
  353. }
  354. offset >>= 2;
  355. }
  356. b43_shm_control_word(dev, routing, offset);
  357. ret = b43_read32(dev, B43_MMIO_SHM_DATA);
  358. out:
  359. return ret;
  360. }
  361. u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  362. {
  363. struct b43_wl *wl = dev->wl;
  364. unsigned long flags;
  365. u32 ret;
  366. spin_lock_irqsave(&wl->shm_lock, flags);
  367. ret = __b43_shm_read32(dev, routing, offset);
  368. spin_unlock_irqrestore(&wl->shm_lock, flags);
  369. return ret;
  370. }
  371. u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  372. {
  373. u16 ret;
  374. if (routing == B43_SHM_SHARED) {
  375. B43_WARN_ON(offset & 0x0001);
  376. if (offset & 0x0003) {
  377. /* Unaligned access */
  378. b43_shm_control_word(dev, routing, offset >> 2);
  379. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  380. goto out;
  381. }
  382. offset >>= 2;
  383. }
  384. b43_shm_control_word(dev, routing, offset);
  385. ret = b43_read16(dev, B43_MMIO_SHM_DATA);
  386. out:
  387. return ret;
  388. }
  389. u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  390. {
  391. struct b43_wl *wl = dev->wl;
  392. unsigned long flags;
  393. u16 ret;
  394. spin_lock_irqsave(&wl->shm_lock, flags);
  395. ret = __b43_shm_read16(dev, routing, offset);
  396. spin_unlock_irqrestore(&wl->shm_lock, flags);
  397. return ret;
  398. }
  399. void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  400. {
  401. if (routing == B43_SHM_SHARED) {
  402. B43_WARN_ON(offset & 0x0001);
  403. if (offset & 0x0003) {
  404. /* Unaligned access */
  405. b43_shm_control_word(dev, routing, offset >> 2);
  406. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
  407. (value >> 16) & 0xffff);
  408. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  409. b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
  410. return;
  411. }
  412. offset >>= 2;
  413. }
  414. b43_shm_control_word(dev, routing, offset);
  415. b43_write32(dev, B43_MMIO_SHM_DATA, value);
  416. }
  417. void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  418. {
  419. struct b43_wl *wl = dev->wl;
  420. unsigned long flags;
  421. spin_lock_irqsave(&wl->shm_lock, flags);
  422. __b43_shm_write32(dev, routing, offset, value);
  423. spin_unlock_irqrestore(&wl->shm_lock, flags);
  424. }
  425. void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  426. {
  427. if (routing == B43_SHM_SHARED) {
  428. B43_WARN_ON(offset & 0x0001);
  429. if (offset & 0x0003) {
  430. /* Unaligned access */
  431. b43_shm_control_word(dev, routing, offset >> 2);
  432. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
  433. return;
  434. }
  435. offset >>= 2;
  436. }
  437. b43_shm_control_word(dev, routing, offset);
  438. b43_write16(dev, B43_MMIO_SHM_DATA, value);
  439. }
  440. void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  441. {
  442. struct b43_wl *wl = dev->wl;
  443. unsigned long flags;
  444. spin_lock_irqsave(&wl->shm_lock, flags);
  445. __b43_shm_write16(dev, routing, offset, value);
  446. spin_unlock_irqrestore(&wl->shm_lock, flags);
  447. }
  448. /* Read HostFlags */
  449. u64 b43_hf_read(struct b43_wldev *dev)
  450. {
  451. u64 ret;
  452. ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
  453. ret <<= 16;
  454. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI);
  455. ret <<= 16;
  456. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
  457. return ret;
  458. }
  459. /* Write HostFlags */
  460. void b43_hf_write(struct b43_wldev *dev, u64 value)
  461. {
  462. u16 lo, mi, hi;
  463. lo = (value & 0x00000000FFFFULL);
  464. mi = (value & 0x0000FFFF0000ULL) >> 16;
  465. hi = (value & 0xFFFF00000000ULL) >> 32;
  466. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO, lo);
  467. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI, mi);
  468. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
  469. }
  470. void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
  471. {
  472. u32 low, high;
  473. B43_WARN_ON(dev->dev->id.revision < 3);
  474. /* The hardware guarantees us an atomic read, if we
  475. * read the low register first. */
  476. low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
  477. high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  478. *tsf = high;
  479. *tsf <<= 32;
  480. *tsf |= low;
  481. }
  482. static void b43_time_lock(struct b43_wldev *dev)
  483. {
  484. u32 macctl;
  485. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  486. macctl |= B43_MACCTL_TBTTHOLD;
  487. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  488. /* Commit the write */
  489. b43_read32(dev, B43_MMIO_MACCTL);
  490. }
  491. static void b43_time_unlock(struct b43_wldev *dev)
  492. {
  493. u32 macctl;
  494. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  495. macctl &= ~B43_MACCTL_TBTTHOLD;
  496. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  497. /* Commit the write */
  498. b43_read32(dev, B43_MMIO_MACCTL);
  499. }
  500. static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
  501. {
  502. u32 low, high;
  503. B43_WARN_ON(dev->dev->id.revision < 3);
  504. low = tsf;
  505. high = (tsf >> 32);
  506. /* The hardware guarantees us an atomic write, if we
  507. * write the low register first. */
  508. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
  509. mmiowb();
  510. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
  511. mmiowb();
  512. }
  513. void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
  514. {
  515. b43_time_lock(dev);
  516. b43_tsf_write_locked(dev, tsf);
  517. b43_time_unlock(dev);
  518. }
  519. static
  520. void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
  521. {
  522. static const u8 zero_addr[ETH_ALEN] = { 0 };
  523. u16 data;
  524. if (!mac)
  525. mac = zero_addr;
  526. offset |= 0x0020;
  527. b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
  528. data = mac[0];
  529. data |= mac[1] << 8;
  530. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  531. data = mac[2];
  532. data |= mac[3] << 8;
  533. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  534. data = mac[4];
  535. data |= mac[5] << 8;
  536. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  537. }
  538. static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
  539. {
  540. const u8 *mac;
  541. const u8 *bssid;
  542. u8 mac_bssid[ETH_ALEN * 2];
  543. int i;
  544. u32 tmp;
  545. bssid = dev->wl->bssid;
  546. mac = dev->wl->mac_addr;
  547. b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
  548. memcpy(mac_bssid, mac, ETH_ALEN);
  549. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  550. /* Write our MAC address and BSSID to template ram */
  551. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  552. tmp = (u32) (mac_bssid[i + 0]);
  553. tmp |= (u32) (mac_bssid[i + 1]) << 8;
  554. tmp |= (u32) (mac_bssid[i + 2]) << 16;
  555. tmp |= (u32) (mac_bssid[i + 3]) << 24;
  556. b43_ram_write(dev, 0x20 + i, tmp);
  557. }
  558. }
  559. static void b43_upload_card_macaddress(struct b43_wldev *dev)
  560. {
  561. b43_write_mac_bssid_templates(dev);
  562. b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
  563. }
  564. static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
  565. {
  566. /* slot_time is in usec. */
  567. if (dev->phy.type != B43_PHYTYPE_G)
  568. return;
  569. b43_write16(dev, 0x684, 510 + slot_time);
  570. b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
  571. }
  572. static void b43_short_slot_timing_enable(struct b43_wldev *dev)
  573. {
  574. b43_set_slot_time(dev, 9);
  575. }
  576. static void b43_short_slot_timing_disable(struct b43_wldev *dev)
  577. {
  578. b43_set_slot_time(dev, 20);
  579. }
  580. /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
  581. * Returns the _previously_ enabled IRQ mask.
  582. */
  583. static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
  584. {
  585. u32 old_mask;
  586. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  587. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
  588. return old_mask;
  589. }
  590. /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
  591. * Returns the _previously_ enabled IRQ mask.
  592. */
  593. static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
  594. {
  595. u32 old_mask;
  596. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  597. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
  598. return old_mask;
  599. }
  600. /* Synchronize IRQ top- and bottom-half.
  601. * IRQs must be masked before calling this.
  602. * This must not be called with the irq_lock held.
  603. */
  604. static void b43_synchronize_irq(struct b43_wldev *dev)
  605. {
  606. synchronize_irq(dev->dev->irq);
  607. tasklet_kill(&dev->isr_tasklet);
  608. }
  609. /* DummyTransmission function, as documented on
  610. * http://bcm-specs.sipsolutions.net/DummyTransmission
  611. */
  612. void b43_dummy_transmission(struct b43_wldev *dev)
  613. {
  614. struct b43_wl *wl = dev->wl;
  615. struct b43_phy *phy = &dev->phy;
  616. unsigned int i, max_loop;
  617. u16 value;
  618. u32 buffer[5] = {
  619. 0x00000000,
  620. 0x00D40000,
  621. 0x00000000,
  622. 0x01000000,
  623. 0x00000000,
  624. };
  625. switch (phy->type) {
  626. case B43_PHYTYPE_A:
  627. max_loop = 0x1E;
  628. buffer[0] = 0x000201CC;
  629. break;
  630. case B43_PHYTYPE_B:
  631. case B43_PHYTYPE_G:
  632. max_loop = 0xFA;
  633. buffer[0] = 0x000B846E;
  634. break;
  635. default:
  636. B43_WARN_ON(1);
  637. return;
  638. }
  639. spin_lock_irq(&wl->irq_lock);
  640. write_lock(&wl->tx_lock);
  641. for (i = 0; i < 5; i++)
  642. b43_ram_write(dev, i * 4, buffer[i]);
  643. /* Commit writes */
  644. b43_read32(dev, B43_MMIO_MACCTL);
  645. b43_write16(dev, 0x0568, 0x0000);
  646. b43_write16(dev, 0x07C0, 0x0000);
  647. value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
  648. b43_write16(dev, 0x050C, value);
  649. b43_write16(dev, 0x0508, 0x0000);
  650. b43_write16(dev, 0x050A, 0x0000);
  651. b43_write16(dev, 0x054C, 0x0000);
  652. b43_write16(dev, 0x056A, 0x0014);
  653. b43_write16(dev, 0x0568, 0x0826);
  654. b43_write16(dev, 0x0500, 0x0000);
  655. b43_write16(dev, 0x0502, 0x0030);
  656. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  657. b43_radio_write16(dev, 0x0051, 0x0017);
  658. for (i = 0x00; i < max_loop; i++) {
  659. value = b43_read16(dev, 0x050E);
  660. if (value & 0x0080)
  661. break;
  662. udelay(10);
  663. }
  664. for (i = 0x00; i < 0x0A; i++) {
  665. value = b43_read16(dev, 0x050E);
  666. if (value & 0x0400)
  667. break;
  668. udelay(10);
  669. }
  670. for (i = 0x00; i < 0x19; i++) {
  671. value = b43_read16(dev, 0x0690);
  672. if (!(value & 0x0100))
  673. break;
  674. udelay(10);
  675. }
  676. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  677. b43_radio_write16(dev, 0x0051, 0x0037);
  678. write_unlock(&wl->tx_lock);
  679. spin_unlock_irq(&wl->irq_lock);
  680. }
  681. static void key_write(struct b43_wldev *dev,
  682. u8 index, u8 algorithm, const u8 *key)
  683. {
  684. unsigned int i;
  685. u32 offset;
  686. u16 value;
  687. u16 kidx;
  688. /* Key index/algo block */
  689. kidx = b43_kidx_to_fw(dev, index);
  690. value = ((kidx << 4) | algorithm);
  691. b43_shm_write16(dev, B43_SHM_SHARED,
  692. B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
  693. /* Write the key to the Key Table Pointer offset */
  694. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  695. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  696. value = key[i];
  697. value |= (u16) (key[i + 1]) << 8;
  698. b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
  699. }
  700. }
  701. static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
  702. {
  703. u32 addrtmp[2] = { 0, 0, };
  704. u8 per_sta_keys_start = 8;
  705. if (b43_new_kidx_api(dev))
  706. per_sta_keys_start = 4;
  707. B43_WARN_ON(index < per_sta_keys_start);
  708. /* We have two default TX keys and possibly two default RX keys.
  709. * Physical mac 0 is mapped to physical key 4 or 8, depending
  710. * on the firmware version.
  711. * So we must adjust the index here.
  712. */
  713. index -= per_sta_keys_start;
  714. if (addr) {
  715. addrtmp[0] = addr[0];
  716. addrtmp[0] |= ((u32) (addr[1]) << 8);
  717. addrtmp[0] |= ((u32) (addr[2]) << 16);
  718. addrtmp[0] |= ((u32) (addr[3]) << 24);
  719. addrtmp[1] = addr[4];
  720. addrtmp[1] |= ((u32) (addr[5]) << 8);
  721. }
  722. if (dev->dev->id.revision >= 5) {
  723. /* Receive match transmitter address mechanism */
  724. b43_shm_write32(dev, B43_SHM_RCMTA,
  725. (index * 2) + 0, addrtmp[0]);
  726. b43_shm_write16(dev, B43_SHM_RCMTA,
  727. (index * 2) + 1, addrtmp[1]);
  728. } else {
  729. /* RXE (Receive Engine) and
  730. * PSM (Programmable State Machine) mechanism
  731. */
  732. if (index < 8) {
  733. /* TODO write to RCM 16, 19, 22 and 25 */
  734. } else {
  735. b43_shm_write32(dev, B43_SHM_SHARED,
  736. B43_SHM_SH_PSM + (index * 6) + 0,
  737. addrtmp[0]);
  738. b43_shm_write16(dev, B43_SHM_SHARED,
  739. B43_SHM_SH_PSM + (index * 6) + 4,
  740. addrtmp[1]);
  741. }
  742. }
  743. }
  744. static void do_key_write(struct b43_wldev *dev,
  745. u8 index, u8 algorithm,
  746. const u8 *key, size_t key_len, const u8 *mac_addr)
  747. {
  748. u8 buf[B43_SEC_KEYSIZE] = { 0, };
  749. u8 per_sta_keys_start = 8;
  750. if (b43_new_kidx_api(dev))
  751. per_sta_keys_start = 4;
  752. B43_WARN_ON(index >= dev->max_nr_keys);
  753. B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
  754. if (index >= per_sta_keys_start)
  755. keymac_write(dev, index, NULL); /* First zero out mac. */
  756. if (key)
  757. memcpy(buf, key, key_len);
  758. key_write(dev, index, algorithm, buf);
  759. if (index >= per_sta_keys_start)
  760. keymac_write(dev, index, mac_addr);
  761. dev->key[index].algorithm = algorithm;
  762. }
  763. static int b43_key_write(struct b43_wldev *dev,
  764. int index, u8 algorithm,
  765. const u8 *key, size_t key_len,
  766. const u8 *mac_addr,
  767. struct ieee80211_key_conf *keyconf)
  768. {
  769. int i;
  770. int sta_keys_start;
  771. if (key_len > B43_SEC_KEYSIZE)
  772. return -EINVAL;
  773. for (i = 0; i < dev->max_nr_keys; i++) {
  774. /* Check that we don't already have this key. */
  775. B43_WARN_ON(dev->key[i].keyconf == keyconf);
  776. }
  777. if (index < 0) {
  778. /* Pairwise key. Get an empty slot for the key. */
  779. if (b43_new_kidx_api(dev))
  780. sta_keys_start = 4;
  781. else
  782. sta_keys_start = 8;
  783. for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
  784. if (!dev->key[i].keyconf) {
  785. /* found empty */
  786. index = i;
  787. break;
  788. }
  789. }
  790. if (index < 0) {
  791. b43warn(dev->wl, "Out of hardware key memory\n");
  792. return -ENOSPC;
  793. }
  794. } else
  795. B43_WARN_ON(index > 3);
  796. do_key_write(dev, index, algorithm, key, key_len, mac_addr);
  797. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  798. /* Default RX key */
  799. B43_WARN_ON(mac_addr);
  800. do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
  801. }
  802. keyconf->hw_key_idx = index;
  803. dev->key[index].keyconf = keyconf;
  804. return 0;
  805. }
  806. static int b43_key_clear(struct b43_wldev *dev, int index)
  807. {
  808. if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
  809. return -EINVAL;
  810. do_key_write(dev, index, B43_SEC_ALGO_NONE,
  811. NULL, B43_SEC_KEYSIZE, NULL);
  812. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  813. do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
  814. NULL, B43_SEC_KEYSIZE, NULL);
  815. }
  816. dev->key[index].keyconf = NULL;
  817. return 0;
  818. }
  819. static void b43_clear_keys(struct b43_wldev *dev)
  820. {
  821. int i;
  822. for (i = 0; i < dev->max_nr_keys; i++)
  823. b43_key_clear(dev, i);
  824. }
  825. static void b43_dump_keymemory(struct b43_wldev *dev)
  826. {
  827. unsigned int i, index, offset;
  828. DECLARE_MAC_BUF(macbuf);
  829. u8 mac[ETH_ALEN];
  830. u16 algo;
  831. u32 rcmta0;
  832. u16 rcmta1;
  833. u64 hf;
  834. struct b43_key *key;
  835. if (!b43_debug(dev, B43_DBG_KEYS))
  836. return;
  837. hf = b43_hf_read(dev);
  838. b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
  839. !!(hf & B43_HF_USEDEFKEYS));
  840. for (index = 0; index < dev->max_nr_keys; index++) {
  841. key = &(dev->key[index]);
  842. printk(KERN_DEBUG "Key slot %02u: %s",
  843. index, (key->keyconf == NULL) ? " " : "*");
  844. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  845. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  846. u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
  847. printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
  848. }
  849. algo = b43_shm_read16(dev, B43_SHM_SHARED,
  850. B43_SHM_SH_KEYIDXBLOCK + (index * 2));
  851. printk(" Algo: %04X/%02X", algo, key->algorithm);
  852. if (index >= 4) {
  853. rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
  854. ((index - 4) * 2) + 0);
  855. rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
  856. ((index - 4) * 2) + 1);
  857. *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
  858. *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
  859. printk(" MAC: %s",
  860. print_mac(macbuf, mac));
  861. } else
  862. printk(" DEFAULT KEY");
  863. printk("\n");
  864. }
  865. }
  866. void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
  867. {
  868. u32 macctl;
  869. u16 ucstat;
  870. bool hwps;
  871. bool awake;
  872. int i;
  873. B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
  874. (ps_flags & B43_PS_DISABLED));
  875. B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
  876. if (ps_flags & B43_PS_ENABLED) {
  877. hwps = 1;
  878. } else if (ps_flags & B43_PS_DISABLED) {
  879. hwps = 0;
  880. } else {
  881. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  882. // and thus is not an AP and we are associated, set bit 25
  883. }
  884. if (ps_flags & B43_PS_AWAKE) {
  885. awake = 1;
  886. } else if (ps_flags & B43_PS_ASLEEP) {
  887. awake = 0;
  888. } else {
  889. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  890. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  891. // successful, set bit26
  892. }
  893. /* FIXME: For now we force awake-on and hwps-off */
  894. hwps = 0;
  895. awake = 1;
  896. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  897. if (hwps)
  898. macctl |= B43_MACCTL_HWPS;
  899. else
  900. macctl &= ~B43_MACCTL_HWPS;
  901. if (awake)
  902. macctl |= B43_MACCTL_AWAKE;
  903. else
  904. macctl &= ~B43_MACCTL_AWAKE;
  905. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  906. /* Commit write */
  907. b43_read32(dev, B43_MMIO_MACCTL);
  908. if (awake && dev->dev->id.revision >= 5) {
  909. /* Wait for the microcode to wake up. */
  910. for (i = 0; i < 100; i++) {
  911. ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
  912. B43_SHM_SH_UCODESTAT);
  913. if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
  914. break;
  915. udelay(10);
  916. }
  917. }
  918. }
  919. void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
  920. {
  921. u32 tmslow;
  922. u32 macctl;
  923. flags |= B43_TMSLOW_PHYCLKEN;
  924. flags |= B43_TMSLOW_PHYRESET;
  925. ssb_device_enable(dev->dev, flags);
  926. msleep(2); /* Wait for the PLL to turn on. */
  927. /* Now take the PHY out of Reset again */
  928. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  929. tmslow |= SSB_TMSLOW_FGC;
  930. tmslow &= ~B43_TMSLOW_PHYRESET;
  931. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  932. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  933. msleep(1);
  934. tmslow &= ~SSB_TMSLOW_FGC;
  935. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  936. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  937. msleep(1);
  938. /* Turn Analog ON, but only if we already know the PHY-type.
  939. * This protects against very early setup where we don't know the
  940. * PHY-type, yet. wireless_core_reset will be called once again later,
  941. * when we know the PHY-type. */
  942. if (dev->phy.ops)
  943. dev->phy.ops->switch_analog(dev, 1);
  944. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  945. macctl &= ~B43_MACCTL_GMODE;
  946. if (flags & B43_TMSLOW_GMODE)
  947. macctl |= B43_MACCTL_GMODE;
  948. macctl |= B43_MACCTL_IHR_ENABLED;
  949. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  950. }
  951. static void handle_irq_transmit_status(struct b43_wldev *dev)
  952. {
  953. u32 v0, v1;
  954. u16 tmp;
  955. struct b43_txstatus stat;
  956. while (1) {
  957. v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  958. if (!(v0 & 0x00000001))
  959. break;
  960. v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  961. stat.cookie = (v0 >> 16);
  962. stat.seq = (v1 & 0x0000FFFF);
  963. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  964. tmp = (v0 & 0x0000FFFF);
  965. stat.frame_count = ((tmp & 0xF000) >> 12);
  966. stat.rts_count = ((tmp & 0x0F00) >> 8);
  967. stat.supp_reason = ((tmp & 0x001C) >> 2);
  968. stat.pm_indicated = !!(tmp & 0x0080);
  969. stat.intermediate = !!(tmp & 0x0040);
  970. stat.for_ampdu = !!(tmp & 0x0020);
  971. stat.acked = !!(tmp & 0x0002);
  972. b43_handle_txstatus(dev, &stat);
  973. }
  974. }
  975. static void drain_txstatus_queue(struct b43_wldev *dev)
  976. {
  977. u32 dummy;
  978. if (dev->dev->id.revision < 5)
  979. return;
  980. /* Read all entries from the microcode TXstatus FIFO
  981. * and throw them away.
  982. */
  983. while (1) {
  984. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  985. if (!(dummy & 0x00000001))
  986. break;
  987. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  988. }
  989. }
  990. static u32 b43_jssi_read(struct b43_wldev *dev)
  991. {
  992. u32 val = 0;
  993. val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
  994. val <<= 16;
  995. val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
  996. return val;
  997. }
  998. static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
  999. {
  1000. b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
  1001. b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
  1002. }
  1003. static void b43_generate_noise_sample(struct b43_wldev *dev)
  1004. {
  1005. b43_jssi_write(dev, 0x7F7F7F7F);
  1006. b43_write32(dev, B43_MMIO_MACCMD,
  1007. b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
  1008. }
  1009. static void b43_calculate_link_quality(struct b43_wldev *dev)
  1010. {
  1011. /* Top half of Link Quality calculation. */
  1012. if (dev->phy.type != B43_PHYTYPE_G)
  1013. return;
  1014. if (dev->noisecalc.calculation_running)
  1015. return;
  1016. dev->noisecalc.calculation_running = 1;
  1017. dev->noisecalc.nr_samples = 0;
  1018. b43_generate_noise_sample(dev);
  1019. }
  1020. static void handle_irq_noise(struct b43_wldev *dev)
  1021. {
  1022. struct b43_phy_g *phy = dev->phy.g;
  1023. u16 tmp;
  1024. u8 noise[4];
  1025. u8 i, j;
  1026. s32 average;
  1027. /* Bottom half of Link Quality calculation. */
  1028. if (dev->phy.type != B43_PHYTYPE_G)
  1029. return;
  1030. /* Possible race condition: It might be possible that the user
  1031. * changed to a different channel in the meantime since we
  1032. * started the calculation. We ignore that fact, since it's
  1033. * not really that much of a problem. The background noise is
  1034. * an estimation only anyway. Slightly wrong results will get damped
  1035. * by the averaging of the 8 sample rounds. Additionally the
  1036. * value is shortlived. So it will be replaced by the next noise
  1037. * calculation round soon. */
  1038. B43_WARN_ON(!dev->noisecalc.calculation_running);
  1039. *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
  1040. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  1041. noise[2] == 0x7F || noise[3] == 0x7F)
  1042. goto generate_new;
  1043. /* Get the noise samples. */
  1044. B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
  1045. i = dev->noisecalc.nr_samples;
  1046. noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1047. noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1048. noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1049. noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1050. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  1051. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  1052. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  1053. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  1054. dev->noisecalc.nr_samples++;
  1055. if (dev->noisecalc.nr_samples == 8) {
  1056. /* Calculate the Link Quality by the noise samples. */
  1057. average = 0;
  1058. for (i = 0; i < 8; i++) {
  1059. for (j = 0; j < 4; j++)
  1060. average += dev->noisecalc.samples[i][j];
  1061. }
  1062. average /= (8 * 4);
  1063. average *= 125;
  1064. average += 64;
  1065. average /= 128;
  1066. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
  1067. tmp = (tmp / 128) & 0x1F;
  1068. if (tmp >= 8)
  1069. average += 2;
  1070. else
  1071. average -= 25;
  1072. if (tmp == 8)
  1073. average -= 72;
  1074. else
  1075. average -= 48;
  1076. dev->stats.link_noise = average;
  1077. dev->noisecalc.calculation_running = 0;
  1078. return;
  1079. }
  1080. generate_new:
  1081. b43_generate_noise_sample(dev);
  1082. }
  1083. static void handle_irq_tbtt_indication(struct b43_wldev *dev)
  1084. {
  1085. if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
  1086. ///TODO: PS TBTT
  1087. } else {
  1088. if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
  1089. b43_power_saving_ctl_bits(dev, 0);
  1090. }
  1091. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  1092. dev->dfq_valid = 1;
  1093. }
  1094. static void handle_irq_atim_end(struct b43_wldev *dev)
  1095. {
  1096. if (dev->dfq_valid) {
  1097. b43_write32(dev, B43_MMIO_MACCMD,
  1098. b43_read32(dev, B43_MMIO_MACCMD)
  1099. | B43_MACCMD_DFQ_VALID);
  1100. dev->dfq_valid = 0;
  1101. }
  1102. }
  1103. static void handle_irq_pmq(struct b43_wldev *dev)
  1104. {
  1105. u32 tmp;
  1106. //TODO: AP mode.
  1107. while (1) {
  1108. tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
  1109. if (!(tmp & 0x00000008))
  1110. break;
  1111. }
  1112. /* 16bit write is odd, but correct. */
  1113. b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
  1114. }
  1115. static void b43_write_template_common(struct b43_wldev *dev,
  1116. const u8 *data, u16 size,
  1117. u16 ram_offset,
  1118. u16 shm_size_offset, u8 rate)
  1119. {
  1120. u32 i, tmp;
  1121. struct b43_plcp_hdr4 plcp;
  1122. plcp.data = 0;
  1123. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  1124. b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  1125. ram_offset += sizeof(u32);
  1126. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  1127. * So leave the first two bytes of the next write blank.
  1128. */
  1129. tmp = (u32) (data[0]) << 16;
  1130. tmp |= (u32) (data[1]) << 24;
  1131. b43_ram_write(dev, ram_offset, tmp);
  1132. ram_offset += sizeof(u32);
  1133. for (i = 2; i < size; i += sizeof(u32)) {
  1134. tmp = (u32) (data[i + 0]);
  1135. if (i + 1 < size)
  1136. tmp |= (u32) (data[i + 1]) << 8;
  1137. if (i + 2 < size)
  1138. tmp |= (u32) (data[i + 2]) << 16;
  1139. if (i + 3 < size)
  1140. tmp |= (u32) (data[i + 3]) << 24;
  1141. b43_ram_write(dev, ram_offset + i - 2, tmp);
  1142. }
  1143. b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
  1144. size + sizeof(struct b43_plcp_hdr6));
  1145. }
  1146. /* Check if the use of the antenna that ieee80211 told us to
  1147. * use is possible. This will fall back to DEFAULT.
  1148. * "antenna_nr" is the antenna identifier we got from ieee80211. */
  1149. u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
  1150. u8 antenna_nr)
  1151. {
  1152. u8 antenna_mask;
  1153. if (antenna_nr == 0) {
  1154. /* Zero means "use default antenna". That's always OK. */
  1155. return 0;
  1156. }
  1157. /* Get the mask of available antennas. */
  1158. if (dev->phy.gmode)
  1159. antenna_mask = dev->dev->bus->sprom.ant_available_bg;
  1160. else
  1161. antenna_mask = dev->dev->bus->sprom.ant_available_a;
  1162. if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
  1163. /* This antenna is not available. Fall back to default. */
  1164. return 0;
  1165. }
  1166. return antenna_nr;
  1167. }
  1168. /* Convert a b43 antenna number value to the PHY TX control value. */
  1169. static u16 b43_antenna_to_phyctl(int antenna)
  1170. {
  1171. switch (antenna) {
  1172. case B43_ANTENNA0:
  1173. return B43_TXH_PHY_ANT0;
  1174. case B43_ANTENNA1:
  1175. return B43_TXH_PHY_ANT1;
  1176. case B43_ANTENNA2:
  1177. return B43_TXH_PHY_ANT2;
  1178. case B43_ANTENNA3:
  1179. return B43_TXH_PHY_ANT3;
  1180. case B43_ANTENNA_AUTO:
  1181. return B43_TXH_PHY_ANT01AUTO;
  1182. }
  1183. B43_WARN_ON(1);
  1184. return 0;
  1185. }
  1186. static void b43_write_beacon_template(struct b43_wldev *dev,
  1187. u16 ram_offset,
  1188. u16 shm_size_offset)
  1189. {
  1190. unsigned int i, len, variable_len;
  1191. const struct ieee80211_mgmt *bcn;
  1192. const u8 *ie;
  1193. bool tim_found = 0;
  1194. unsigned int rate;
  1195. u16 ctl;
  1196. int antenna;
  1197. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
  1198. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  1199. len = min((size_t) dev->wl->current_beacon->len,
  1200. 0x200 - sizeof(struct b43_plcp_hdr6));
  1201. rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
  1202. b43_write_template_common(dev, (const u8 *)bcn,
  1203. len, ram_offset, shm_size_offset, rate);
  1204. /* Write the PHY TX control parameters. */
  1205. antenna = B43_ANTENNA_DEFAULT;
  1206. antenna = b43_antenna_to_phyctl(antenna);
  1207. ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1208. /* We can't send beacons with short preamble. Would get PHY errors. */
  1209. ctl &= ~B43_TXH_PHY_SHORTPRMBL;
  1210. ctl &= ~B43_TXH_PHY_ANT;
  1211. ctl &= ~B43_TXH_PHY_ENC;
  1212. ctl |= antenna;
  1213. if (b43_is_cck_rate(rate))
  1214. ctl |= B43_TXH_PHY_ENC_CCK;
  1215. else
  1216. ctl |= B43_TXH_PHY_ENC_OFDM;
  1217. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  1218. /* Find the position of the TIM and the DTIM_period value
  1219. * and write them to SHM. */
  1220. ie = bcn->u.beacon.variable;
  1221. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1222. for (i = 0; i < variable_len - 2; ) {
  1223. uint8_t ie_id, ie_len;
  1224. ie_id = ie[i];
  1225. ie_len = ie[i + 1];
  1226. if (ie_id == 5) {
  1227. u16 tim_position;
  1228. u16 dtim_period;
  1229. /* This is the TIM Information Element */
  1230. /* Check whether the ie_len is in the beacon data range. */
  1231. if (variable_len < ie_len + 2 + i)
  1232. break;
  1233. /* A valid TIM is at least 4 bytes long. */
  1234. if (ie_len < 4)
  1235. break;
  1236. tim_found = 1;
  1237. tim_position = sizeof(struct b43_plcp_hdr6);
  1238. tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1239. tim_position += i;
  1240. dtim_period = ie[i + 3];
  1241. b43_shm_write16(dev, B43_SHM_SHARED,
  1242. B43_SHM_SH_TIMBPOS, tim_position);
  1243. b43_shm_write16(dev, B43_SHM_SHARED,
  1244. B43_SHM_SH_DTIMPER, dtim_period);
  1245. break;
  1246. }
  1247. i += ie_len + 2;
  1248. }
  1249. if (!tim_found) {
  1250. /*
  1251. * If ucode wants to modify TIM do it behind the beacon, this
  1252. * will happen, for example, when doing mesh networking.
  1253. */
  1254. b43_shm_write16(dev, B43_SHM_SHARED,
  1255. B43_SHM_SH_TIMBPOS,
  1256. len + sizeof(struct b43_plcp_hdr6));
  1257. b43_shm_write16(dev, B43_SHM_SHARED,
  1258. B43_SHM_SH_DTIMPER, 0);
  1259. }
  1260. b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
  1261. }
  1262. static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  1263. u16 shm_offset, u16 size,
  1264. struct ieee80211_rate *rate)
  1265. {
  1266. struct b43_plcp_hdr4 plcp;
  1267. u32 tmp;
  1268. __le16 dur;
  1269. plcp.data = 0;
  1270. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
  1271. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1272. dev->wl->vif, size,
  1273. rate);
  1274. /* Write PLCP in two parts and timing for packet transfer */
  1275. tmp = le32_to_cpu(plcp.data);
  1276. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
  1277. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
  1278. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
  1279. }
  1280. /* Instead of using custom probe response template, this function
  1281. * just patches custom beacon template by:
  1282. * 1) Changing packet type
  1283. * 2) Patching duration field
  1284. * 3) Stripping TIM
  1285. */
  1286. static const u8 *b43_generate_probe_resp(struct b43_wldev *dev,
  1287. u16 *dest_size,
  1288. struct ieee80211_rate *rate)
  1289. {
  1290. const u8 *src_data;
  1291. u8 *dest_data;
  1292. u16 src_size, elem_size, src_pos, dest_pos;
  1293. __le16 dur;
  1294. struct ieee80211_hdr *hdr;
  1295. size_t ie_start;
  1296. src_size = dev->wl->current_beacon->len;
  1297. src_data = (const u8 *)dev->wl->current_beacon->data;
  1298. /* Get the start offset of the variable IEs in the packet. */
  1299. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  1300. B43_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1301. if (B43_WARN_ON(src_size < ie_start))
  1302. return NULL;
  1303. dest_data = kmalloc(src_size, GFP_ATOMIC);
  1304. if (unlikely(!dest_data))
  1305. return NULL;
  1306. /* Copy the static data and all Information Elements, except the TIM. */
  1307. memcpy(dest_data, src_data, ie_start);
  1308. src_pos = ie_start;
  1309. dest_pos = ie_start;
  1310. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  1311. elem_size = src_data[src_pos + 1] + 2;
  1312. if (src_data[src_pos] == 5) {
  1313. /* This is the TIM. */
  1314. continue;
  1315. }
  1316. memcpy(dest_data + dest_pos, src_data + src_pos,
  1317. elem_size);
  1318. dest_pos += elem_size;
  1319. }
  1320. *dest_size = dest_pos;
  1321. hdr = (struct ieee80211_hdr *)dest_data;
  1322. /* Set the frame control. */
  1323. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1324. IEEE80211_STYPE_PROBE_RESP);
  1325. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1326. dev->wl->vif, *dest_size,
  1327. rate);
  1328. hdr->duration_id = dur;
  1329. return dest_data;
  1330. }
  1331. static void b43_write_probe_resp_template(struct b43_wldev *dev,
  1332. u16 ram_offset,
  1333. u16 shm_size_offset,
  1334. struct ieee80211_rate *rate)
  1335. {
  1336. const u8 *probe_resp_data;
  1337. u16 size;
  1338. size = dev->wl->current_beacon->len;
  1339. probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
  1340. if (unlikely(!probe_resp_data))
  1341. return;
  1342. /* Looks like PLCP headers plus packet timings are stored for
  1343. * all possible basic rates
  1344. */
  1345. b43_write_probe_resp_plcp(dev, 0x31A, size, &b43_b_ratetable[0]);
  1346. b43_write_probe_resp_plcp(dev, 0x32C, size, &b43_b_ratetable[1]);
  1347. b43_write_probe_resp_plcp(dev, 0x33E, size, &b43_b_ratetable[2]);
  1348. b43_write_probe_resp_plcp(dev, 0x350, size, &b43_b_ratetable[3]);
  1349. size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
  1350. b43_write_template_common(dev, probe_resp_data,
  1351. size, ram_offset, shm_size_offset,
  1352. rate->hw_value);
  1353. kfree(probe_resp_data);
  1354. }
  1355. static void b43_upload_beacon0(struct b43_wldev *dev)
  1356. {
  1357. struct b43_wl *wl = dev->wl;
  1358. if (wl->beacon0_uploaded)
  1359. return;
  1360. b43_write_beacon_template(dev, 0x68, 0x18);
  1361. /* FIXME: Probe resp upload doesn't really belong here,
  1362. * but we don't use that feature anyway. */
  1363. b43_write_probe_resp_template(dev, 0x268, 0x4A,
  1364. &__b43_ratetable[3]);
  1365. wl->beacon0_uploaded = 1;
  1366. }
  1367. static void b43_upload_beacon1(struct b43_wldev *dev)
  1368. {
  1369. struct b43_wl *wl = dev->wl;
  1370. if (wl->beacon1_uploaded)
  1371. return;
  1372. b43_write_beacon_template(dev, 0x468, 0x1A);
  1373. wl->beacon1_uploaded = 1;
  1374. }
  1375. static void handle_irq_beacon(struct b43_wldev *dev)
  1376. {
  1377. struct b43_wl *wl = dev->wl;
  1378. u32 cmd, beacon0_valid, beacon1_valid;
  1379. if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
  1380. !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  1381. return;
  1382. /* This is the bottom half of the asynchronous beacon update. */
  1383. /* Ignore interrupt in the future. */
  1384. dev->irq_savedstate &= ~B43_IRQ_BEACON;
  1385. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1386. beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
  1387. beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
  1388. /* Schedule interrupt manually, if busy. */
  1389. if (beacon0_valid && beacon1_valid) {
  1390. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
  1391. dev->irq_savedstate |= B43_IRQ_BEACON;
  1392. return;
  1393. }
  1394. if (unlikely(wl->beacon_templates_virgin)) {
  1395. /* We never uploaded a beacon before.
  1396. * Upload both templates now, but only mark one valid. */
  1397. wl->beacon_templates_virgin = 0;
  1398. b43_upload_beacon0(dev);
  1399. b43_upload_beacon1(dev);
  1400. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1401. cmd |= B43_MACCMD_BEACON0_VALID;
  1402. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1403. } else {
  1404. if (!beacon0_valid) {
  1405. b43_upload_beacon0(dev);
  1406. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1407. cmd |= B43_MACCMD_BEACON0_VALID;
  1408. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1409. } else if (!beacon1_valid) {
  1410. b43_upload_beacon1(dev);
  1411. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1412. cmd |= B43_MACCMD_BEACON1_VALID;
  1413. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1414. }
  1415. }
  1416. }
  1417. static void b43_beacon_update_trigger_work(struct work_struct *work)
  1418. {
  1419. struct b43_wl *wl = container_of(work, struct b43_wl,
  1420. beacon_update_trigger);
  1421. struct b43_wldev *dev;
  1422. mutex_lock(&wl->mutex);
  1423. dev = wl->current_dev;
  1424. if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
  1425. spin_lock_irq(&wl->irq_lock);
  1426. /* update beacon right away or defer to irq */
  1427. dev->irq_savedstate = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1428. handle_irq_beacon(dev);
  1429. /* The handler might have updated the IRQ mask. */
  1430. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK,
  1431. dev->irq_savedstate);
  1432. mmiowb();
  1433. spin_unlock_irq(&wl->irq_lock);
  1434. }
  1435. mutex_unlock(&wl->mutex);
  1436. }
  1437. /* Asynchronously update the packet templates in template RAM.
  1438. * Locking: Requires wl->irq_lock to be locked. */
  1439. static void b43_update_templates(struct b43_wl *wl)
  1440. {
  1441. struct sk_buff *beacon;
  1442. /* This is the top half of the ansynchronous beacon update.
  1443. * The bottom half is the beacon IRQ.
  1444. * Beacon update must be asynchronous to avoid sending an
  1445. * invalid beacon. This can happen for example, if the firmware
  1446. * transmits a beacon while we are updating it. */
  1447. /* We could modify the existing beacon and set the aid bit in
  1448. * the TIM field, but that would probably require resizing and
  1449. * moving of data within the beacon template.
  1450. * Simply request a new beacon and let mac80211 do the hard work. */
  1451. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1452. if (unlikely(!beacon))
  1453. return;
  1454. if (wl->current_beacon)
  1455. dev_kfree_skb_any(wl->current_beacon);
  1456. wl->current_beacon = beacon;
  1457. wl->beacon0_uploaded = 0;
  1458. wl->beacon1_uploaded = 0;
  1459. queue_work(wl->hw->workqueue, &wl->beacon_update_trigger);
  1460. }
  1461. static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
  1462. {
  1463. b43_time_lock(dev);
  1464. if (dev->dev->id.revision >= 3) {
  1465. b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
  1466. b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
  1467. } else {
  1468. b43_write16(dev, 0x606, (beacon_int >> 6));
  1469. b43_write16(dev, 0x610, beacon_int);
  1470. }
  1471. b43_time_unlock(dev);
  1472. b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
  1473. }
  1474. static void b43_handle_firmware_panic(struct b43_wldev *dev)
  1475. {
  1476. u16 reason;
  1477. /* Read the register that contains the reason code for the panic. */
  1478. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
  1479. b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
  1480. switch (reason) {
  1481. default:
  1482. b43dbg(dev->wl, "The panic reason is unknown.\n");
  1483. /* fallthrough */
  1484. case B43_FWPANIC_DIE:
  1485. /* Do not restart the controller or firmware.
  1486. * The device is nonfunctional from now on.
  1487. * Restarting would result in this panic to trigger again,
  1488. * so we avoid that recursion. */
  1489. break;
  1490. case B43_FWPANIC_RESTART:
  1491. b43_controller_restart(dev, "Microcode panic");
  1492. break;
  1493. }
  1494. }
  1495. static void handle_irq_ucode_debug(struct b43_wldev *dev)
  1496. {
  1497. unsigned int i, cnt;
  1498. u16 reason, marker_id, marker_line;
  1499. __le16 *buf;
  1500. /* The proprietary firmware doesn't have this IRQ. */
  1501. if (!dev->fw.opensource)
  1502. return;
  1503. /* Read the register that contains the reason code for this IRQ. */
  1504. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
  1505. switch (reason) {
  1506. case B43_DEBUGIRQ_PANIC:
  1507. b43_handle_firmware_panic(dev);
  1508. break;
  1509. case B43_DEBUGIRQ_DUMP_SHM:
  1510. if (!B43_DEBUG)
  1511. break; /* Only with driver debugging enabled. */
  1512. buf = kmalloc(4096, GFP_ATOMIC);
  1513. if (!buf) {
  1514. b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
  1515. goto out;
  1516. }
  1517. for (i = 0; i < 4096; i += 2) {
  1518. u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
  1519. buf[i / 2] = cpu_to_le16(tmp);
  1520. }
  1521. b43info(dev->wl, "Shared memory dump:\n");
  1522. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
  1523. 16, 2, buf, 4096, 1);
  1524. kfree(buf);
  1525. break;
  1526. case B43_DEBUGIRQ_DUMP_REGS:
  1527. if (!B43_DEBUG)
  1528. break; /* Only with driver debugging enabled. */
  1529. b43info(dev->wl, "Microcode register dump:\n");
  1530. for (i = 0, cnt = 0; i < 64; i++) {
  1531. u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
  1532. if (cnt == 0)
  1533. printk(KERN_INFO);
  1534. printk("r%02u: 0x%04X ", i, tmp);
  1535. cnt++;
  1536. if (cnt == 6) {
  1537. printk("\n");
  1538. cnt = 0;
  1539. }
  1540. }
  1541. printk("\n");
  1542. break;
  1543. case B43_DEBUGIRQ_MARKER:
  1544. if (!B43_DEBUG)
  1545. break; /* Only with driver debugging enabled. */
  1546. marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1547. B43_MARKER_ID_REG);
  1548. marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1549. B43_MARKER_LINE_REG);
  1550. b43info(dev->wl, "The firmware just executed the MARKER(%u) "
  1551. "at line number %u\n",
  1552. marker_id, marker_line);
  1553. break;
  1554. default:
  1555. b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
  1556. reason);
  1557. }
  1558. out:
  1559. /* Acknowledge the debug-IRQ, so the firmware can continue. */
  1560. b43_shm_write16(dev, B43_SHM_SCRATCH,
  1561. B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
  1562. }
  1563. /* Interrupt handler bottom-half */
  1564. static void b43_interrupt_tasklet(struct b43_wldev *dev)
  1565. {
  1566. u32 reason;
  1567. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1568. u32 merged_dma_reason = 0;
  1569. int i;
  1570. unsigned long flags;
  1571. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1572. B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
  1573. reason = dev->irq_reason;
  1574. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1575. dma_reason[i] = dev->dma_reason[i];
  1576. merged_dma_reason |= dma_reason[i];
  1577. }
  1578. if (unlikely(reason & B43_IRQ_MAC_TXERR))
  1579. b43err(dev->wl, "MAC transmission error\n");
  1580. if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
  1581. b43err(dev->wl, "PHY transmission error\n");
  1582. rmb();
  1583. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1584. atomic_set(&dev->phy.txerr_cnt,
  1585. B43_PHY_TX_BADNESS_LIMIT);
  1586. b43err(dev->wl, "Too many PHY TX errors, "
  1587. "restarting the controller\n");
  1588. b43_controller_restart(dev, "PHY TX errors");
  1589. }
  1590. }
  1591. if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
  1592. B43_DMAIRQ_NONFATALMASK))) {
  1593. if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
  1594. b43err(dev->wl, "Fatal DMA error: "
  1595. "0x%08X, 0x%08X, 0x%08X, "
  1596. "0x%08X, 0x%08X, 0x%08X\n",
  1597. dma_reason[0], dma_reason[1],
  1598. dma_reason[2], dma_reason[3],
  1599. dma_reason[4], dma_reason[5]);
  1600. b43_controller_restart(dev, "DMA error");
  1601. mmiowb();
  1602. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1603. return;
  1604. }
  1605. if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
  1606. b43err(dev->wl, "DMA error: "
  1607. "0x%08X, 0x%08X, 0x%08X, "
  1608. "0x%08X, 0x%08X, 0x%08X\n",
  1609. dma_reason[0], dma_reason[1],
  1610. dma_reason[2], dma_reason[3],
  1611. dma_reason[4], dma_reason[5]);
  1612. }
  1613. }
  1614. if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
  1615. handle_irq_ucode_debug(dev);
  1616. if (reason & B43_IRQ_TBTT_INDI)
  1617. handle_irq_tbtt_indication(dev);
  1618. if (reason & B43_IRQ_ATIM_END)
  1619. handle_irq_atim_end(dev);
  1620. if (reason & B43_IRQ_BEACON)
  1621. handle_irq_beacon(dev);
  1622. if (reason & B43_IRQ_PMQ)
  1623. handle_irq_pmq(dev);
  1624. if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
  1625. ;/* TODO */
  1626. if (reason & B43_IRQ_NOISESAMPLE_OK)
  1627. handle_irq_noise(dev);
  1628. /* Check the DMA reason registers for received data. */
  1629. if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
  1630. if (b43_using_pio_transfers(dev))
  1631. b43_pio_rx(dev->pio.rx_queue);
  1632. else
  1633. b43_dma_rx(dev->dma.rx_ring);
  1634. }
  1635. B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
  1636. B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
  1637. B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
  1638. B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
  1639. B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
  1640. if (reason & B43_IRQ_TX_OK)
  1641. handle_irq_transmit_status(dev);
  1642. b43_interrupt_enable(dev, dev->irq_savedstate);
  1643. mmiowb();
  1644. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1645. }
  1646. static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
  1647. {
  1648. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
  1649. b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
  1650. b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
  1651. b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
  1652. b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
  1653. b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
  1654. b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
  1655. }
  1656. /* Interrupt handler top-half */
  1657. static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
  1658. {
  1659. irqreturn_t ret = IRQ_NONE;
  1660. struct b43_wldev *dev = dev_id;
  1661. u32 reason;
  1662. if (!dev)
  1663. return IRQ_NONE;
  1664. spin_lock(&dev->wl->irq_lock);
  1665. if (b43_status(dev) < B43_STAT_STARTED)
  1666. goto out;
  1667. reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1668. if (reason == 0xffffffff) /* shared IRQ */
  1669. goto out;
  1670. ret = IRQ_HANDLED;
  1671. reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1672. if (!reason)
  1673. goto out;
  1674. dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
  1675. & 0x0001DC00;
  1676. dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
  1677. & 0x0000DC00;
  1678. dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
  1679. & 0x0000DC00;
  1680. dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
  1681. & 0x0001DC00;
  1682. dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
  1683. & 0x0000DC00;
  1684. dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
  1685. & 0x0000DC00;
  1686. b43_interrupt_ack(dev, reason);
  1687. /* disable all IRQs. They are enabled again in the bottom half. */
  1688. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1689. /* save the reason code and call our bottom half. */
  1690. dev->irq_reason = reason;
  1691. tasklet_schedule(&dev->isr_tasklet);
  1692. out:
  1693. mmiowb();
  1694. spin_unlock(&dev->wl->irq_lock);
  1695. return ret;
  1696. }
  1697. void b43_do_release_fw(struct b43_firmware_file *fw)
  1698. {
  1699. release_firmware(fw->data);
  1700. fw->data = NULL;
  1701. fw->filename = NULL;
  1702. }
  1703. static void b43_release_firmware(struct b43_wldev *dev)
  1704. {
  1705. b43_do_release_fw(&dev->fw.ucode);
  1706. b43_do_release_fw(&dev->fw.pcm);
  1707. b43_do_release_fw(&dev->fw.initvals);
  1708. b43_do_release_fw(&dev->fw.initvals_band);
  1709. }
  1710. static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
  1711. {
  1712. const char text[] =
  1713. "You must go to " \
  1714. "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
  1715. "and download the correct firmware for this driver version. " \
  1716. "Please carefully read all instructions on this website.\n";
  1717. if (error)
  1718. b43err(wl, text);
  1719. else
  1720. b43warn(wl, text);
  1721. }
  1722. int b43_do_request_fw(struct b43_request_fw_context *ctx,
  1723. const char *name,
  1724. struct b43_firmware_file *fw)
  1725. {
  1726. const struct firmware *blob;
  1727. struct b43_fw_header *hdr;
  1728. u32 size;
  1729. int err;
  1730. if (!name) {
  1731. /* Don't fetch anything. Free possibly cached firmware. */
  1732. /* FIXME: We should probably keep it anyway, to save some headache
  1733. * on suspend/resume with multiband devices. */
  1734. b43_do_release_fw(fw);
  1735. return 0;
  1736. }
  1737. if (fw->filename) {
  1738. if ((fw->type == ctx->req_type) &&
  1739. (strcmp(fw->filename, name) == 0))
  1740. return 0; /* Already have this fw. */
  1741. /* Free the cached firmware first. */
  1742. /* FIXME: We should probably do this later after we successfully
  1743. * got the new fw. This could reduce headache with multiband devices.
  1744. * We could also redesign this to cache the firmware for all possible
  1745. * bands all the time. */
  1746. b43_do_release_fw(fw);
  1747. }
  1748. switch (ctx->req_type) {
  1749. case B43_FWTYPE_PROPRIETARY:
  1750. snprintf(ctx->fwname, sizeof(ctx->fwname),
  1751. "b43%s/%s.fw",
  1752. modparam_fwpostfix, name);
  1753. break;
  1754. case B43_FWTYPE_OPENSOURCE:
  1755. snprintf(ctx->fwname, sizeof(ctx->fwname),
  1756. "b43-open%s/%s.fw",
  1757. modparam_fwpostfix, name);
  1758. break;
  1759. default:
  1760. B43_WARN_ON(1);
  1761. return -ENOSYS;
  1762. }
  1763. err = request_firmware(&blob, ctx->fwname, ctx->dev->dev->dev);
  1764. if (err == -ENOENT) {
  1765. snprintf(ctx->errors[ctx->req_type],
  1766. sizeof(ctx->errors[ctx->req_type]),
  1767. "Firmware file \"%s\" not found\n", ctx->fwname);
  1768. return err;
  1769. } else if (err) {
  1770. snprintf(ctx->errors[ctx->req_type],
  1771. sizeof(ctx->errors[ctx->req_type]),
  1772. "Firmware file \"%s\" request failed (err=%d)\n",
  1773. ctx->fwname, err);
  1774. return err;
  1775. }
  1776. if (blob->size < sizeof(struct b43_fw_header))
  1777. goto err_format;
  1778. hdr = (struct b43_fw_header *)(blob->data);
  1779. switch (hdr->type) {
  1780. case B43_FW_TYPE_UCODE:
  1781. case B43_FW_TYPE_PCM:
  1782. size = be32_to_cpu(hdr->size);
  1783. if (size != blob->size - sizeof(struct b43_fw_header))
  1784. goto err_format;
  1785. /* fallthrough */
  1786. case B43_FW_TYPE_IV:
  1787. if (hdr->ver != 1)
  1788. goto err_format;
  1789. break;
  1790. default:
  1791. goto err_format;
  1792. }
  1793. fw->data = blob;
  1794. fw->filename = name;
  1795. fw->type = ctx->req_type;
  1796. return 0;
  1797. err_format:
  1798. snprintf(ctx->errors[ctx->req_type],
  1799. sizeof(ctx->errors[ctx->req_type]),
  1800. "Firmware file \"%s\" format error.\n", ctx->fwname);
  1801. release_firmware(blob);
  1802. return -EPROTO;
  1803. }
  1804. static int b43_try_request_fw(struct b43_request_fw_context *ctx)
  1805. {
  1806. struct b43_wldev *dev = ctx->dev;
  1807. struct b43_firmware *fw = &ctx->dev->fw;
  1808. const u8 rev = ctx->dev->dev->id.revision;
  1809. const char *filename;
  1810. u32 tmshigh;
  1811. int err;
  1812. /* Get microcode */
  1813. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1814. if ((rev >= 5) && (rev <= 10))
  1815. filename = "ucode5";
  1816. else if ((rev >= 11) && (rev <= 12))
  1817. filename = "ucode11";
  1818. else if (rev >= 13)
  1819. filename = "ucode13";
  1820. else
  1821. goto err_no_ucode;
  1822. err = b43_do_request_fw(ctx, filename, &fw->ucode);
  1823. if (err)
  1824. goto err_load;
  1825. /* Get PCM code */
  1826. if ((rev >= 5) && (rev <= 10))
  1827. filename = "pcm5";
  1828. else if (rev >= 11)
  1829. filename = NULL;
  1830. else
  1831. goto err_no_pcm;
  1832. fw->pcm_request_failed = 0;
  1833. err = b43_do_request_fw(ctx, filename, &fw->pcm);
  1834. if (err == -ENOENT) {
  1835. /* We did not find a PCM file? Not fatal, but
  1836. * core rev <= 10 must do without hwcrypto then. */
  1837. fw->pcm_request_failed = 1;
  1838. } else if (err)
  1839. goto err_load;
  1840. /* Get initvals */
  1841. switch (dev->phy.type) {
  1842. case B43_PHYTYPE_A:
  1843. if ((rev >= 5) && (rev <= 10)) {
  1844. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1845. filename = "a0g1initvals5";
  1846. else
  1847. filename = "a0g0initvals5";
  1848. } else
  1849. goto err_no_initvals;
  1850. break;
  1851. case B43_PHYTYPE_G:
  1852. if ((rev >= 5) && (rev <= 10))
  1853. filename = "b0g0initvals5";
  1854. else if (rev >= 13)
  1855. filename = "b0g0initvals13";
  1856. else
  1857. goto err_no_initvals;
  1858. break;
  1859. case B43_PHYTYPE_N:
  1860. if ((rev >= 11) && (rev <= 12))
  1861. filename = "n0initvals11";
  1862. else
  1863. goto err_no_initvals;
  1864. break;
  1865. default:
  1866. goto err_no_initvals;
  1867. }
  1868. err = b43_do_request_fw(ctx, filename, &fw->initvals);
  1869. if (err)
  1870. goto err_load;
  1871. /* Get bandswitch initvals */
  1872. switch (dev->phy.type) {
  1873. case B43_PHYTYPE_A:
  1874. if ((rev >= 5) && (rev <= 10)) {
  1875. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1876. filename = "a0g1bsinitvals5";
  1877. else
  1878. filename = "a0g0bsinitvals5";
  1879. } else if (rev >= 11)
  1880. filename = NULL;
  1881. else
  1882. goto err_no_initvals;
  1883. break;
  1884. case B43_PHYTYPE_G:
  1885. if ((rev >= 5) && (rev <= 10))
  1886. filename = "b0g0bsinitvals5";
  1887. else if (rev >= 11)
  1888. filename = NULL;
  1889. else
  1890. goto err_no_initvals;
  1891. break;
  1892. case B43_PHYTYPE_N:
  1893. if ((rev >= 11) && (rev <= 12))
  1894. filename = "n0bsinitvals11";
  1895. else
  1896. goto err_no_initvals;
  1897. break;
  1898. default:
  1899. goto err_no_initvals;
  1900. }
  1901. err = b43_do_request_fw(ctx, filename, &fw->initvals_band);
  1902. if (err)
  1903. goto err_load;
  1904. return 0;
  1905. err_no_ucode:
  1906. err = ctx->fatal_failure = -EOPNOTSUPP;
  1907. b43err(dev->wl, "The driver does not know which firmware (ucode) "
  1908. "is required for your device (wl-core rev %u)\n", rev);
  1909. goto error;
  1910. err_no_pcm:
  1911. err = ctx->fatal_failure = -EOPNOTSUPP;
  1912. b43err(dev->wl, "The driver does not know which firmware (PCM) "
  1913. "is required for your device (wl-core rev %u)\n", rev);
  1914. goto error;
  1915. err_no_initvals:
  1916. err = ctx->fatal_failure = -EOPNOTSUPP;
  1917. b43err(dev->wl, "The driver does not know which firmware (initvals) "
  1918. "is required for your device (wl-core rev %u)\n", rev);
  1919. goto error;
  1920. err_load:
  1921. /* We failed to load this firmware image. The error message
  1922. * already is in ctx->errors. Return and let our caller decide
  1923. * what to do. */
  1924. goto error;
  1925. error:
  1926. b43_release_firmware(dev);
  1927. return err;
  1928. }
  1929. static int b43_request_firmware(struct b43_wldev *dev)
  1930. {
  1931. struct b43_request_fw_context *ctx;
  1932. unsigned int i;
  1933. int err;
  1934. const char *errmsg;
  1935. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1936. if (!ctx)
  1937. return -ENOMEM;
  1938. ctx->dev = dev;
  1939. ctx->req_type = B43_FWTYPE_PROPRIETARY;
  1940. err = b43_try_request_fw(ctx);
  1941. if (!err)
  1942. goto out; /* Successfully loaded it. */
  1943. err = ctx->fatal_failure;
  1944. if (err)
  1945. goto out;
  1946. ctx->req_type = B43_FWTYPE_OPENSOURCE;
  1947. err = b43_try_request_fw(ctx);
  1948. if (!err)
  1949. goto out; /* Successfully loaded it. */
  1950. err = ctx->fatal_failure;
  1951. if (err)
  1952. goto out;
  1953. /* Could not find a usable firmware. Print the errors. */
  1954. for (i = 0; i < B43_NR_FWTYPES; i++) {
  1955. errmsg = ctx->errors[i];
  1956. if (strlen(errmsg))
  1957. b43err(dev->wl, errmsg);
  1958. }
  1959. b43_print_fw_helptext(dev->wl, 1);
  1960. err = -ENOENT;
  1961. out:
  1962. kfree(ctx);
  1963. return err;
  1964. }
  1965. static int b43_upload_microcode(struct b43_wldev *dev)
  1966. {
  1967. const size_t hdr_len = sizeof(struct b43_fw_header);
  1968. const __be32 *data;
  1969. unsigned int i, len;
  1970. u16 fwrev, fwpatch, fwdate, fwtime;
  1971. u32 tmp, macctl;
  1972. int err = 0;
  1973. /* Jump the microcode PSM to offset 0 */
  1974. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1975. B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
  1976. macctl |= B43_MACCTL_PSM_JMP0;
  1977. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1978. /* Zero out all microcode PSM registers and shared memory. */
  1979. for (i = 0; i < 64; i++)
  1980. b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
  1981. for (i = 0; i < 4096; i += 2)
  1982. b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
  1983. /* Upload Microcode. */
  1984. data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
  1985. len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
  1986. b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
  1987. for (i = 0; i < len; i++) {
  1988. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1989. udelay(10);
  1990. }
  1991. if (dev->fw.pcm.data) {
  1992. /* Upload PCM data. */
  1993. data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
  1994. len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
  1995. b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
  1996. b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
  1997. /* No need for autoinc bit in SHM_HW */
  1998. b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
  1999. for (i = 0; i < len; i++) {
  2000. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  2001. udelay(10);
  2002. }
  2003. }
  2004. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
  2005. /* Start the microcode PSM */
  2006. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  2007. macctl &= ~B43_MACCTL_PSM_JMP0;
  2008. macctl |= B43_MACCTL_PSM_RUN;
  2009. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2010. /* Wait for the microcode to load and respond */
  2011. i = 0;
  2012. while (1) {
  2013. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2014. if (tmp == B43_IRQ_MAC_SUSPENDED)
  2015. break;
  2016. i++;
  2017. if (i >= 20) {
  2018. b43err(dev->wl, "Microcode not responding\n");
  2019. b43_print_fw_helptext(dev->wl, 1);
  2020. err = -ENODEV;
  2021. goto error;
  2022. }
  2023. msleep_interruptible(50);
  2024. if (signal_pending(current)) {
  2025. err = -EINTR;
  2026. goto error;
  2027. }
  2028. }
  2029. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
  2030. /* Get and check the revisions. */
  2031. fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
  2032. fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
  2033. fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
  2034. fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
  2035. if (fwrev <= 0x128) {
  2036. b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
  2037. "binary drivers older than version 4.x is unsupported. "
  2038. "You must upgrade your firmware files.\n");
  2039. b43_print_fw_helptext(dev->wl, 1);
  2040. err = -EOPNOTSUPP;
  2041. goto error;
  2042. }
  2043. dev->fw.rev = fwrev;
  2044. dev->fw.patch = fwpatch;
  2045. dev->fw.opensource = (fwdate == 0xFFFF);
  2046. if (dev->fw.opensource) {
  2047. /* Patchlevel info is encoded in the "time" field. */
  2048. dev->fw.patch = fwtime;
  2049. b43info(dev->wl, "Loading OpenSource firmware version %u.%u%s\n",
  2050. dev->fw.rev, dev->fw.patch,
  2051. dev->fw.pcm_request_failed ? " (Hardware crypto not supported)" : "");
  2052. } else {
  2053. b43info(dev->wl, "Loading firmware version %u.%u "
  2054. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
  2055. fwrev, fwpatch,
  2056. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  2057. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
  2058. if (dev->fw.pcm_request_failed) {
  2059. b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
  2060. "Hardware accelerated cryptography is disabled.\n");
  2061. b43_print_fw_helptext(dev->wl, 0);
  2062. }
  2063. }
  2064. if (b43_is_old_txhdr_format(dev)) {
  2065. /* We're over the deadline, but we keep support for old fw
  2066. * until it turns out to be in major conflict with something new. */
  2067. b43warn(dev->wl, "You are using an old firmware image. "
  2068. "Support for old firmware will be removed soon "
  2069. "(official deadline was July 2008).\n");
  2070. b43_print_fw_helptext(dev->wl, 0);
  2071. }
  2072. return 0;
  2073. error:
  2074. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  2075. macctl &= ~B43_MACCTL_PSM_RUN;
  2076. macctl |= B43_MACCTL_PSM_JMP0;
  2077. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2078. return err;
  2079. }
  2080. static int b43_write_initvals(struct b43_wldev *dev,
  2081. const struct b43_iv *ivals,
  2082. size_t count,
  2083. size_t array_size)
  2084. {
  2085. const struct b43_iv *iv;
  2086. u16 offset;
  2087. size_t i;
  2088. bool bit32;
  2089. BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
  2090. iv = ivals;
  2091. for (i = 0; i < count; i++) {
  2092. if (array_size < sizeof(iv->offset_size))
  2093. goto err_format;
  2094. array_size -= sizeof(iv->offset_size);
  2095. offset = be16_to_cpu(iv->offset_size);
  2096. bit32 = !!(offset & B43_IV_32BIT);
  2097. offset &= B43_IV_OFFSET_MASK;
  2098. if (offset >= 0x1000)
  2099. goto err_format;
  2100. if (bit32) {
  2101. u32 value;
  2102. if (array_size < sizeof(iv->data.d32))
  2103. goto err_format;
  2104. array_size -= sizeof(iv->data.d32);
  2105. value = get_unaligned_be32(&iv->data.d32);
  2106. b43_write32(dev, offset, value);
  2107. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2108. sizeof(__be16) +
  2109. sizeof(__be32));
  2110. } else {
  2111. u16 value;
  2112. if (array_size < sizeof(iv->data.d16))
  2113. goto err_format;
  2114. array_size -= sizeof(iv->data.d16);
  2115. value = be16_to_cpu(iv->data.d16);
  2116. b43_write16(dev, offset, value);
  2117. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2118. sizeof(__be16) +
  2119. sizeof(__be16));
  2120. }
  2121. }
  2122. if (array_size)
  2123. goto err_format;
  2124. return 0;
  2125. err_format:
  2126. b43err(dev->wl, "Initial Values Firmware file-format error.\n");
  2127. b43_print_fw_helptext(dev->wl, 1);
  2128. return -EPROTO;
  2129. }
  2130. static int b43_upload_initvals(struct b43_wldev *dev)
  2131. {
  2132. const size_t hdr_len = sizeof(struct b43_fw_header);
  2133. const struct b43_fw_header *hdr;
  2134. struct b43_firmware *fw = &dev->fw;
  2135. const struct b43_iv *ivals;
  2136. size_t count;
  2137. int err;
  2138. hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
  2139. ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
  2140. count = be32_to_cpu(hdr->size);
  2141. err = b43_write_initvals(dev, ivals, count,
  2142. fw->initvals.data->size - hdr_len);
  2143. if (err)
  2144. goto out;
  2145. if (fw->initvals_band.data) {
  2146. hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
  2147. ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
  2148. count = be32_to_cpu(hdr->size);
  2149. err = b43_write_initvals(dev, ivals, count,
  2150. fw->initvals_band.data->size - hdr_len);
  2151. if (err)
  2152. goto out;
  2153. }
  2154. out:
  2155. return err;
  2156. }
  2157. /* Initialize the GPIOs
  2158. * http://bcm-specs.sipsolutions.net/GPIO
  2159. */
  2160. static int b43_gpio_init(struct b43_wldev *dev)
  2161. {
  2162. struct ssb_bus *bus = dev->dev->bus;
  2163. struct ssb_device *gpiodev, *pcidev = NULL;
  2164. u32 mask, set;
  2165. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2166. & ~B43_MACCTL_GPOUTSMSK);
  2167. b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
  2168. | 0x000F);
  2169. mask = 0x0000001F;
  2170. set = 0x0000000F;
  2171. if (dev->dev->bus->chip_id == 0x4301) {
  2172. mask |= 0x0060;
  2173. set |= 0x0060;
  2174. }
  2175. if (0 /* FIXME: conditional unknown */ ) {
  2176. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2177. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2178. | 0x0100);
  2179. mask |= 0x0180;
  2180. set |= 0x0180;
  2181. }
  2182. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
  2183. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2184. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2185. | 0x0200);
  2186. mask |= 0x0200;
  2187. set |= 0x0200;
  2188. }
  2189. if (dev->dev->id.revision >= 2)
  2190. mask |= 0x0010; /* FIXME: This is redundant. */
  2191. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2192. pcidev = bus->pcicore.dev;
  2193. #endif
  2194. gpiodev = bus->chipco.dev ? : pcidev;
  2195. if (!gpiodev)
  2196. return 0;
  2197. ssb_write32(gpiodev, B43_GPIO_CONTROL,
  2198. (ssb_read32(gpiodev, B43_GPIO_CONTROL)
  2199. & mask) | set);
  2200. return 0;
  2201. }
  2202. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  2203. static void b43_gpio_cleanup(struct b43_wldev *dev)
  2204. {
  2205. struct ssb_bus *bus = dev->dev->bus;
  2206. struct ssb_device *gpiodev, *pcidev = NULL;
  2207. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2208. pcidev = bus->pcicore.dev;
  2209. #endif
  2210. gpiodev = bus->chipco.dev ? : pcidev;
  2211. if (!gpiodev)
  2212. return;
  2213. ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
  2214. }
  2215. /* http://bcm-specs.sipsolutions.net/EnableMac */
  2216. void b43_mac_enable(struct b43_wldev *dev)
  2217. {
  2218. if (b43_debug(dev, B43_DBG_FIRMWARE)) {
  2219. u16 fwstate;
  2220. fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
  2221. B43_SHM_SH_UCODESTAT);
  2222. if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
  2223. (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
  2224. b43err(dev->wl, "b43_mac_enable(): The firmware "
  2225. "should be suspended, but current state is %u\n",
  2226. fwstate);
  2227. }
  2228. }
  2229. dev->mac_suspended--;
  2230. B43_WARN_ON(dev->mac_suspended < 0);
  2231. if (dev->mac_suspended == 0) {
  2232. b43_write32(dev, B43_MMIO_MACCTL,
  2233. b43_read32(dev, B43_MMIO_MACCTL)
  2234. | B43_MACCTL_ENABLED);
  2235. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  2236. B43_IRQ_MAC_SUSPENDED);
  2237. /* Commit writes */
  2238. b43_read32(dev, B43_MMIO_MACCTL);
  2239. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2240. b43_power_saving_ctl_bits(dev, 0);
  2241. }
  2242. }
  2243. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  2244. void b43_mac_suspend(struct b43_wldev *dev)
  2245. {
  2246. int i;
  2247. u32 tmp;
  2248. might_sleep();
  2249. B43_WARN_ON(dev->mac_suspended < 0);
  2250. if (dev->mac_suspended == 0) {
  2251. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  2252. b43_write32(dev, B43_MMIO_MACCTL,
  2253. b43_read32(dev, B43_MMIO_MACCTL)
  2254. & ~B43_MACCTL_ENABLED);
  2255. /* force pci to flush the write */
  2256. b43_read32(dev, B43_MMIO_MACCTL);
  2257. for (i = 35; i; i--) {
  2258. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2259. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2260. goto out;
  2261. udelay(10);
  2262. }
  2263. /* Hm, it seems this will take some time. Use msleep(). */
  2264. for (i = 40; i; i--) {
  2265. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2266. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2267. goto out;
  2268. msleep(1);
  2269. }
  2270. b43err(dev->wl, "MAC suspend failed\n");
  2271. }
  2272. out:
  2273. dev->mac_suspended++;
  2274. }
  2275. static void b43_adjust_opmode(struct b43_wldev *dev)
  2276. {
  2277. struct b43_wl *wl = dev->wl;
  2278. u32 ctl;
  2279. u16 cfp_pretbtt;
  2280. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  2281. /* Reset status to STA infrastructure mode. */
  2282. ctl &= ~B43_MACCTL_AP;
  2283. ctl &= ~B43_MACCTL_KEEP_CTL;
  2284. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  2285. ctl &= ~B43_MACCTL_KEEP_BAD;
  2286. ctl &= ~B43_MACCTL_PROMISC;
  2287. ctl &= ~B43_MACCTL_BEACPROMISC;
  2288. ctl |= B43_MACCTL_INFRA;
  2289. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  2290. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  2291. ctl |= B43_MACCTL_AP;
  2292. else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
  2293. ctl &= ~B43_MACCTL_INFRA;
  2294. if (wl->filter_flags & FIF_CONTROL)
  2295. ctl |= B43_MACCTL_KEEP_CTL;
  2296. if (wl->filter_flags & FIF_FCSFAIL)
  2297. ctl |= B43_MACCTL_KEEP_BAD;
  2298. if (wl->filter_flags & FIF_PLCPFAIL)
  2299. ctl |= B43_MACCTL_KEEP_BADPLCP;
  2300. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  2301. ctl |= B43_MACCTL_PROMISC;
  2302. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  2303. ctl |= B43_MACCTL_BEACPROMISC;
  2304. /* Workaround: On old hardware the HW-MAC-address-filter
  2305. * doesn't work properly, so always run promisc in filter
  2306. * it in software. */
  2307. if (dev->dev->id.revision <= 4)
  2308. ctl |= B43_MACCTL_PROMISC;
  2309. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  2310. cfp_pretbtt = 2;
  2311. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  2312. if (dev->dev->bus->chip_id == 0x4306 &&
  2313. dev->dev->bus->chip_rev == 3)
  2314. cfp_pretbtt = 100;
  2315. else
  2316. cfp_pretbtt = 50;
  2317. }
  2318. b43_write16(dev, 0x612, cfp_pretbtt);
  2319. }
  2320. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  2321. {
  2322. u16 offset;
  2323. if (is_ofdm) {
  2324. offset = 0x480;
  2325. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  2326. } else {
  2327. offset = 0x4C0;
  2328. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  2329. }
  2330. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  2331. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  2332. }
  2333. static void b43_rate_memory_init(struct b43_wldev *dev)
  2334. {
  2335. switch (dev->phy.type) {
  2336. case B43_PHYTYPE_A:
  2337. case B43_PHYTYPE_G:
  2338. case B43_PHYTYPE_N:
  2339. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  2340. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  2341. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  2342. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  2343. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  2344. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  2345. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  2346. if (dev->phy.type == B43_PHYTYPE_A)
  2347. break;
  2348. /* fallthrough */
  2349. case B43_PHYTYPE_B:
  2350. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  2351. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  2352. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  2353. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  2354. break;
  2355. default:
  2356. B43_WARN_ON(1);
  2357. }
  2358. }
  2359. /* Set the default values for the PHY TX Control Words. */
  2360. static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
  2361. {
  2362. u16 ctl = 0;
  2363. ctl |= B43_TXH_PHY_ENC_CCK;
  2364. ctl |= B43_TXH_PHY_ANT01AUTO;
  2365. ctl |= B43_TXH_PHY_TXPWR;
  2366. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  2367. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
  2368. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
  2369. }
  2370. /* Set the TX-Antenna for management frames sent by firmware. */
  2371. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  2372. {
  2373. u16 ant;
  2374. u16 tmp;
  2375. ant = b43_antenna_to_phyctl(antenna);
  2376. /* For ACK/CTS */
  2377. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  2378. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2379. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  2380. /* For Probe Resposes */
  2381. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  2382. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2383. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  2384. }
  2385. /* This is the opposite of b43_chip_init() */
  2386. static void b43_chip_exit(struct b43_wldev *dev)
  2387. {
  2388. b43_phy_exit(dev);
  2389. b43_gpio_cleanup(dev);
  2390. /* firmware is released later */
  2391. }
  2392. /* Initialize the chip
  2393. * http://bcm-specs.sipsolutions.net/ChipInit
  2394. */
  2395. static int b43_chip_init(struct b43_wldev *dev)
  2396. {
  2397. struct b43_phy *phy = &dev->phy;
  2398. int err;
  2399. u32 value32, macctl;
  2400. u16 value16;
  2401. /* Initialize the MAC control */
  2402. macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
  2403. if (dev->phy.gmode)
  2404. macctl |= B43_MACCTL_GMODE;
  2405. macctl |= B43_MACCTL_INFRA;
  2406. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2407. err = b43_request_firmware(dev);
  2408. if (err)
  2409. goto out;
  2410. err = b43_upload_microcode(dev);
  2411. if (err)
  2412. goto out; /* firmware is released later */
  2413. err = b43_gpio_init(dev);
  2414. if (err)
  2415. goto out; /* firmware is released later */
  2416. err = b43_upload_initvals(dev);
  2417. if (err)
  2418. goto err_gpio_clean;
  2419. /* Turn the Analog on and initialize the PHY. */
  2420. phy->ops->switch_analog(dev, 1);
  2421. err = b43_phy_init(dev);
  2422. if (err)
  2423. goto err_gpio_clean;
  2424. /* Disable Interference Mitigation. */
  2425. if (phy->ops->interf_mitigation)
  2426. phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
  2427. /* Select the antennae */
  2428. if (phy->ops->set_rx_antenna)
  2429. phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  2430. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  2431. if (phy->type == B43_PHYTYPE_B) {
  2432. value16 = b43_read16(dev, 0x005E);
  2433. value16 |= 0x0004;
  2434. b43_write16(dev, 0x005E, value16);
  2435. }
  2436. b43_write32(dev, 0x0100, 0x01000000);
  2437. if (dev->dev->id.revision < 5)
  2438. b43_write32(dev, 0x010C, 0x01000000);
  2439. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2440. & ~B43_MACCTL_INFRA);
  2441. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2442. | B43_MACCTL_INFRA);
  2443. /* Probe Response Timeout value */
  2444. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  2445. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  2446. /* Initially set the wireless operation mode. */
  2447. b43_adjust_opmode(dev);
  2448. if (dev->dev->id.revision < 3) {
  2449. b43_write16(dev, 0x060E, 0x0000);
  2450. b43_write16(dev, 0x0610, 0x8000);
  2451. b43_write16(dev, 0x0604, 0x0000);
  2452. b43_write16(dev, 0x0606, 0x0200);
  2453. } else {
  2454. b43_write32(dev, 0x0188, 0x80000000);
  2455. b43_write32(dev, 0x018C, 0x02000000);
  2456. }
  2457. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  2458. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  2459. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  2460. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  2461. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  2462. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  2463. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  2464. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  2465. value32 |= 0x00100000;
  2466. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  2467. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  2468. dev->dev->bus->chipco.fast_pwrup_delay);
  2469. err = 0;
  2470. b43dbg(dev->wl, "Chip initialized\n");
  2471. out:
  2472. return err;
  2473. err_gpio_clean:
  2474. b43_gpio_cleanup(dev);
  2475. return err;
  2476. }
  2477. static void b43_periodic_every60sec(struct b43_wldev *dev)
  2478. {
  2479. const struct b43_phy_operations *ops = dev->phy.ops;
  2480. if (ops->pwork_60sec)
  2481. ops->pwork_60sec(dev);
  2482. /* Force check the TX power emission now. */
  2483. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
  2484. }
  2485. static void b43_periodic_every30sec(struct b43_wldev *dev)
  2486. {
  2487. /* Update device statistics. */
  2488. b43_calculate_link_quality(dev);
  2489. }
  2490. static void b43_periodic_every15sec(struct b43_wldev *dev)
  2491. {
  2492. struct b43_phy *phy = &dev->phy;
  2493. u16 wdr;
  2494. if (dev->fw.opensource) {
  2495. /* Check if the firmware is still alive.
  2496. * It will reset the watchdog counter to 0 in its idle loop. */
  2497. wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
  2498. if (unlikely(wdr)) {
  2499. b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
  2500. b43_controller_restart(dev, "Firmware watchdog");
  2501. return;
  2502. } else {
  2503. b43_shm_write16(dev, B43_SHM_SCRATCH,
  2504. B43_WATCHDOG_REG, 1);
  2505. }
  2506. }
  2507. if (phy->ops->pwork_15sec)
  2508. phy->ops->pwork_15sec(dev);
  2509. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2510. wmb();
  2511. }
  2512. static void do_periodic_work(struct b43_wldev *dev)
  2513. {
  2514. unsigned int state;
  2515. state = dev->periodic_state;
  2516. if (state % 4 == 0)
  2517. b43_periodic_every60sec(dev);
  2518. if (state % 2 == 0)
  2519. b43_periodic_every30sec(dev);
  2520. b43_periodic_every15sec(dev);
  2521. }
  2522. /* Periodic work locking policy:
  2523. * The whole periodic work handler is protected by
  2524. * wl->mutex. If another lock is needed somewhere in the
  2525. * pwork callchain, it's aquired in-place, where it's needed.
  2526. */
  2527. static void b43_periodic_work_handler(struct work_struct *work)
  2528. {
  2529. struct b43_wldev *dev = container_of(work, struct b43_wldev,
  2530. periodic_work.work);
  2531. struct b43_wl *wl = dev->wl;
  2532. unsigned long delay;
  2533. mutex_lock(&wl->mutex);
  2534. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2535. goto out;
  2536. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2537. goto out_requeue;
  2538. do_periodic_work(dev);
  2539. dev->periodic_state++;
  2540. out_requeue:
  2541. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2542. delay = msecs_to_jiffies(50);
  2543. else
  2544. delay = round_jiffies_relative(HZ * 15);
  2545. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  2546. out:
  2547. mutex_unlock(&wl->mutex);
  2548. }
  2549. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2550. {
  2551. struct delayed_work *work = &dev->periodic_work;
  2552. dev->periodic_state = 0;
  2553. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2554. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2555. }
  2556. /* Check if communication with the device works correctly. */
  2557. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2558. {
  2559. u32 v, backup;
  2560. backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2561. /* Check for read/write and endianness problems. */
  2562. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2563. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2564. goto error;
  2565. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2566. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2567. goto error;
  2568. b43_shm_write32(dev, B43_SHM_SHARED, 0, backup);
  2569. if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
  2570. /* The 32bit register shadows the two 16bit registers
  2571. * with update sideeffects. Validate this. */
  2572. b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
  2573. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
  2574. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
  2575. goto error;
  2576. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
  2577. goto error;
  2578. }
  2579. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
  2580. v = b43_read32(dev, B43_MMIO_MACCTL);
  2581. v |= B43_MACCTL_GMODE;
  2582. if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2583. goto error;
  2584. return 0;
  2585. error:
  2586. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2587. return -ENODEV;
  2588. }
  2589. static void b43_security_init(struct b43_wldev *dev)
  2590. {
  2591. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2592. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2593. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2594. /* KTP is a word address, but we address SHM bytewise.
  2595. * So multiply by two.
  2596. */
  2597. dev->ktp *= 2;
  2598. if (dev->dev->id.revision >= 5) {
  2599. /* Number of RCMTA address slots */
  2600. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2601. }
  2602. b43_clear_keys(dev);
  2603. }
  2604. static int b43_rng_read(struct hwrng *rng, u32 *data)
  2605. {
  2606. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2607. unsigned long flags;
  2608. /* Don't take wl->mutex here, as it could deadlock with
  2609. * hwrng internal locking. It's not needed to take
  2610. * wl->mutex here, anyway. */
  2611. spin_lock_irqsave(&wl->irq_lock, flags);
  2612. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2613. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2614. return (sizeof(u16));
  2615. }
  2616. static void b43_rng_exit(struct b43_wl *wl)
  2617. {
  2618. if (wl->rng_initialized)
  2619. hwrng_unregister(&wl->rng);
  2620. }
  2621. static int b43_rng_init(struct b43_wl *wl)
  2622. {
  2623. int err;
  2624. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2625. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2626. wl->rng.name = wl->rng_name;
  2627. wl->rng.data_read = b43_rng_read;
  2628. wl->rng.priv = (unsigned long)wl;
  2629. wl->rng_initialized = 1;
  2630. err = hwrng_register(&wl->rng);
  2631. if (err) {
  2632. wl->rng_initialized = 0;
  2633. b43err(wl, "Failed to register the random "
  2634. "number generator (%d)\n", err);
  2635. }
  2636. return err;
  2637. }
  2638. static int b43_op_tx(struct ieee80211_hw *hw,
  2639. struct sk_buff *skb)
  2640. {
  2641. struct b43_wl *wl = hw_to_b43_wl(hw);
  2642. struct b43_wldev *dev = wl->current_dev;
  2643. unsigned long flags;
  2644. int err;
  2645. if (unlikely(skb->len < 2 + 2 + 6)) {
  2646. /* Too short, this can't be a valid frame. */
  2647. goto drop_packet;
  2648. }
  2649. B43_WARN_ON(skb_shinfo(skb)->nr_frags);
  2650. if (unlikely(!dev))
  2651. goto drop_packet;
  2652. /* Transmissions on seperate queues can run concurrently. */
  2653. read_lock_irqsave(&wl->tx_lock, flags);
  2654. err = -ENODEV;
  2655. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2656. if (b43_using_pio_transfers(dev))
  2657. err = b43_pio_tx(dev, skb);
  2658. else
  2659. err = b43_dma_tx(dev, skb);
  2660. }
  2661. read_unlock_irqrestore(&wl->tx_lock, flags);
  2662. if (unlikely(err))
  2663. goto drop_packet;
  2664. return NETDEV_TX_OK;
  2665. drop_packet:
  2666. /* We can not transmit this packet. Drop it. */
  2667. dev_kfree_skb_any(skb);
  2668. return NETDEV_TX_OK;
  2669. }
  2670. /* Locking: wl->irq_lock */
  2671. static void b43_qos_params_upload(struct b43_wldev *dev,
  2672. const struct ieee80211_tx_queue_params *p,
  2673. u16 shm_offset)
  2674. {
  2675. u16 params[B43_NR_QOSPARAMS];
  2676. int bslots, tmp;
  2677. unsigned int i;
  2678. bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
  2679. memset(&params, 0, sizeof(params));
  2680. params[B43_QOSPARAM_TXOP] = p->txop * 32;
  2681. params[B43_QOSPARAM_CWMIN] = p->cw_min;
  2682. params[B43_QOSPARAM_CWMAX] = p->cw_max;
  2683. params[B43_QOSPARAM_CWCUR] = p->cw_min;
  2684. params[B43_QOSPARAM_AIFS] = p->aifs;
  2685. params[B43_QOSPARAM_BSLOTS] = bslots;
  2686. params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
  2687. for (i = 0; i < ARRAY_SIZE(params); i++) {
  2688. if (i == B43_QOSPARAM_STATUS) {
  2689. tmp = b43_shm_read16(dev, B43_SHM_SHARED,
  2690. shm_offset + (i * 2));
  2691. /* Mark the parameters as updated. */
  2692. tmp |= 0x100;
  2693. b43_shm_write16(dev, B43_SHM_SHARED,
  2694. shm_offset + (i * 2),
  2695. tmp);
  2696. } else {
  2697. b43_shm_write16(dev, B43_SHM_SHARED,
  2698. shm_offset + (i * 2),
  2699. params[i]);
  2700. }
  2701. }
  2702. }
  2703. /* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
  2704. static const u16 b43_qos_shm_offsets[] = {
  2705. /* [mac80211-queue-nr] = SHM_OFFSET, */
  2706. [0] = B43_QOS_VOICE,
  2707. [1] = B43_QOS_VIDEO,
  2708. [2] = B43_QOS_BESTEFFORT,
  2709. [3] = B43_QOS_BACKGROUND,
  2710. };
  2711. /* Update all QOS parameters in hardware. */
  2712. static void b43_qos_upload_all(struct b43_wldev *dev)
  2713. {
  2714. struct b43_wl *wl = dev->wl;
  2715. struct b43_qos_params *params;
  2716. unsigned int i;
  2717. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2718. ARRAY_SIZE(wl->qos_params));
  2719. b43_mac_suspend(dev);
  2720. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2721. params = &(wl->qos_params[i]);
  2722. b43_qos_params_upload(dev, &(params->p),
  2723. b43_qos_shm_offsets[i]);
  2724. }
  2725. b43_mac_enable(dev);
  2726. }
  2727. static void b43_qos_clear(struct b43_wl *wl)
  2728. {
  2729. struct b43_qos_params *params;
  2730. unsigned int i;
  2731. /* Initialize QoS parameters to sane defaults. */
  2732. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2733. ARRAY_SIZE(wl->qos_params));
  2734. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2735. params = &(wl->qos_params[i]);
  2736. switch (b43_qos_shm_offsets[i]) {
  2737. case B43_QOS_VOICE:
  2738. params->p.txop = 0;
  2739. params->p.aifs = 2;
  2740. params->p.cw_min = 0x0001;
  2741. params->p.cw_max = 0x0001;
  2742. break;
  2743. case B43_QOS_VIDEO:
  2744. params->p.txop = 0;
  2745. params->p.aifs = 2;
  2746. params->p.cw_min = 0x0001;
  2747. params->p.cw_max = 0x0001;
  2748. break;
  2749. case B43_QOS_BESTEFFORT:
  2750. params->p.txop = 0;
  2751. params->p.aifs = 3;
  2752. params->p.cw_min = 0x0001;
  2753. params->p.cw_max = 0x03FF;
  2754. break;
  2755. case B43_QOS_BACKGROUND:
  2756. params->p.txop = 0;
  2757. params->p.aifs = 7;
  2758. params->p.cw_min = 0x0001;
  2759. params->p.cw_max = 0x03FF;
  2760. break;
  2761. default:
  2762. B43_WARN_ON(1);
  2763. }
  2764. }
  2765. }
  2766. /* Initialize the core's QOS capabilities */
  2767. static void b43_qos_init(struct b43_wldev *dev)
  2768. {
  2769. /* Upload the current QOS parameters. */
  2770. b43_qos_upload_all(dev);
  2771. /* Enable QOS support. */
  2772. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
  2773. b43_write16(dev, B43_MMIO_IFSCTL,
  2774. b43_read16(dev, B43_MMIO_IFSCTL)
  2775. | B43_MMIO_IFSCTL_USE_EDCF);
  2776. }
  2777. static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
  2778. const struct ieee80211_tx_queue_params *params)
  2779. {
  2780. struct b43_wl *wl = hw_to_b43_wl(hw);
  2781. struct b43_wldev *dev;
  2782. unsigned int queue = (unsigned int)_queue;
  2783. int err = -ENODEV;
  2784. if (queue >= ARRAY_SIZE(wl->qos_params)) {
  2785. /* Queue not available or don't support setting
  2786. * params on this queue. Return success to not
  2787. * confuse mac80211. */
  2788. return 0;
  2789. }
  2790. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2791. ARRAY_SIZE(wl->qos_params));
  2792. mutex_lock(&wl->mutex);
  2793. dev = wl->current_dev;
  2794. if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
  2795. goto out_unlock;
  2796. memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
  2797. b43_mac_suspend(dev);
  2798. b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
  2799. b43_qos_shm_offsets[queue]);
  2800. b43_mac_enable(dev);
  2801. err = 0;
  2802. out_unlock:
  2803. mutex_unlock(&wl->mutex);
  2804. return err;
  2805. }
  2806. static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
  2807. struct ieee80211_tx_queue_stats *stats)
  2808. {
  2809. struct b43_wl *wl = hw_to_b43_wl(hw);
  2810. struct b43_wldev *dev = wl->current_dev;
  2811. unsigned long flags;
  2812. int err = -ENODEV;
  2813. if (!dev)
  2814. goto out;
  2815. spin_lock_irqsave(&wl->irq_lock, flags);
  2816. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2817. if (b43_using_pio_transfers(dev))
  2818. b43_pio_get_tx_stats(dev, stats);
  2819. else
  2820. b43_dma_get_tx_stats(dev, stats);
  2821. err = 0;
  2822. }
  2823. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2824. out:
  2825. return err;
  2826. }
  2827. static int b43_op_get_stats(struct ieee80211_hw *hw,
  2828. struct ieee80211_low_level_stats *stats)
  2829. {
  2830. struct b43_wl *wl = hw_to_b43_wl(hw);
  2831. unsigned long flags;
  2832. spin_lock_irqsave(&wl->irq_lock, flags);
  2833. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2834. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2835. return 0;
  2836. }
  2837. static u64 b43_op_get_tsf(struct ieee80211_hw *hw)
  2838. {
  2839. struct b43_wl *wl = hw_to_b43_wl(hw);
  2840. struct b43_wldev *dev;
  2841. u64 tsf;
  2842. mutex_lock(&wl->mutex);
  2843. spin_lock_irq(&wl->irq_lock);
  2844. dev = wl->current_dev;
  2845. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
  2846. b43_tsf_read(dev, &tsf);
  2847. else
  2848. tsf = 0;
  2849. spin_unlock_irq(&wl->irq_lock);
  2850. mutex_unlock(&wl->mutex);
  2851. return tsf;
  2852. }
  2853. static void b43_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2854. {
  2855. struct b43_wl *wl = hw_to_b43_wl(hw);
  2856. struct b43_wldev *dev;
  2857. mutex_lock(&wl->mutex);
  2858. spin_lock_irq(&wl->irq_lock);
  2859. dev = wl->current_dev;
  2860. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
  2861. b43_tsf_write(dev, tsf);
  2862. spin_unlock_irq(&wl->irq_lock);
  2863. mutex_unlock(&wl->mutex);
  2864. }
  2865. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2866. {
  2867. struct ssb_device *sdev = dev->dev;
  2868. u32 tmslow;
  2869. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2870. tmslow &= ~B43_TMSLOW_GMODE;
  2871. tmslow |= B43_TMSLOW_PHYRESET;
  2872. tmslow |= SSB_TMSLOW_FGC;
  2873. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2874. msleep(1);
  2875. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2876. tmslow &= ~SSB_TMSLOW_FGC;
  2877. tmslow |= B43_TMSLOW_PHYRESET;
  2878. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2879. msleep(1);
  2880. }
  2881. static const char *band_to_string(enum ieee80211_band band)
  2882. {
  2883. switch (band) {
  2884. case IEEE80211_BAND_5GHZ:
  2885. return "5";
  2886. case IEEE80211_BAND_2GHZ:
  2887. return "2.4";
  2888. default:
  2889. break;
  2890. }
  2891. B43_WARN_ON(1);
  2892. return "";
  2893. }
  2894. /* Expects wl->mutex locked */
  2895. static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
  2896. {
  2897. struct b43_wldev *up_dev = NULL;
  2898. struct b43_wldev *down_dev;
  2899. struct b43_wldev *d;
  2900. int err;
  2901. bool uninitialized_var(gmode);
  2902. int prev_status;
  2903. /* Find a device and PHY which supports the band. */
  2904. list_for_each_entry(d, &wl->devlist, list) {
  2905. switch (chan->band) {
  2906. case IEEE80211_BAND_5GHZ:
  2907. if (d->phy.supports_5ghz) {
  2908. up_dev = d;
  2909. gmode = 0;
  2910. }
  2911. break;
  2912. case IEEE80211_BAND_2GHZ:
  2913. if (d->phy.supports_2ghz) {
  2914. up_dev = d;
  2915. gmode = 1;
  2916. }
  2917. break;
  2918. default:
  2919. B43_WARN_ON(1);
  2920. return -EINVAL;
  2921. }
  2922. if (up_dev)
  2923. break;
  2924. }
  2925. if (!up_dev) {
  2926. b43err(wl, "Could not find a device for %s-GHz band operation\n",
  2927. band_to_string(chan->band));
  2928. return -ENODEV;
  2929. }
  2930. if ((up_dev == wl->current_dev) &&
  2931. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2932. /* This device is already running. */
  2933. return 0;
  2934. }
  2935. b43dbg(wl, "Switching to %s-GHz band\n",
  2936. band_to_string(chan->band));
  2937. down_dev = wl->current_dev;
  2938. prev_status = b43_status(down_dev);
  2939. /* Shutdown the currently running core. */
  2940. if (prev_status >= B43_STAT_STARTED)
  2941. b43_wireless_core_stop(down_dev);
  2942. if (prev_status >= B43_STAT_INITIALIZED)
  2943. b43_wireless_core_exit(down_dev);
  2944. if (down_dev != up_dev) {
  2945. /* We switch to a different core, so we put PHY into
  2946. * RESET on the old core. */
  2947. b43_put_phy_into_reset(down_dev);
  2948. }
  2949. /* Now start the new core. */
  2950. up_dev->phy.gmode = gmode;
  2951. if (prev_status >= B43_STAT_INITIALIZED) {
  2952. err = b43_wireless_core_init(up_dev);
  2953. if (err) {
  2954. b43err(wl, "Fatal: Could not initialize device for "
  2955. "selected %s-GHz band\n",
  2956. band_to_string(chan->band));
  2957. goto init_failure;
  2958. }
  2959. }
  2960. if (prev_status >= B43_STAT_STARTED) {
  2961. err = b43_wireless_core_start(up_dev);
  2962. if (err) {
  2963. b43err(wl, "Fatal: Coult not start device for "
  2964. "selected %s-GHz band\n",
  2965. band_to_string(chan->band));
  2966. b43_wireless_core_exit(up_dev);
  2967. goto init_failure;
  2968. }
  2969. }
  2970. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2971. wl->current_dev = up_dev;
  2972. return 0;
  2973. init_failure:
  2974. /* Whoops, failed to init the new core. No core is operating now. */
  2975. wl->current_dev = NULL;
  2976. return err;
  2977. }
  2978. /* Write the short and long frame retry limit values. */
  2979. static void b43_set_retry_limits(struct b43_wldev *dev,
  2980. unsigned int short_retry,
  2981. unsigned int long_retry)
  2982. {
  2983. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2984. * the chip-internal counter. */
  2985. short_retry = min(short_retry, (unsigned int)0xF);
  2986. long_retry = min(long_retry, (unsigned int)0xF);
  2987. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
  2988. short_retry);
  2989. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
  2990. long_retry);
  2991. }
  2992. static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
  2993. {
  2994. struct b43_wl *wl = hw_to_b43_wl(hw);
  2995. struct b43_wldev *dev;
  2996. struct b43_phy *phy;
  2997. struct ieee80211_conf *conf = &hw->conf;
  2998. unsigned long flags;
  2999. int antenna;
  3000. int err = 0;
  3001. mutex_lock(&wl->mutex);
  3002. /* Switch the band (if necessary). This might change the active core. */
  3003. err = b43_switch_band(wl, conf->channel);
  3004. if (err)
  3005. goto out_unlock_mutex;
  3006. dev = wl->current_dev;
  3007. phy = &dev->phy;
  3008. b43_mac_suspend(dev);
  3009. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  3010. b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
  3011. conf->long_frame_max_tx_count);
  3012. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  3013. if (!changed)
  3014. goto out_mac_enable;
  3015. /* Switch to the requested channel.
  3016. * The firmware takes care of races with the TX handler. */
  3017. if (conf->channel->hw_value != phy->channel)
  3018. b43_switch_channel(dev, conf->channel->hw_value);
  3019. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  3020. /* Adjust the desired TX power level. */
  3021. if (conf->power_level != 0) {
  3022. spin_lock_irqsave(&wl->irq_lock, flags);
  3023. if (conf->power_level != phy->desired_txpower) {
  3024. phy->desired_txpower = conf->power_level;
  3025. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
  3026. B43_TXPWR_IGNORE_TSSI);
  3027. }
  3028. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3029. }
  3030. /* Antennas for RX and management frame TX. */
  3031. antenna = B43_ANTENNA_DEFAULT;
  3032. b43_mgmtframe_txantenna(dev, antenna);
  3033. antenna = B43_ANTENNA_DEFAULT;
  3034. if (phy->ops->set_rx_antenna)
  3035. phy->ops->set_rx_antenna(dev, antenna);
  3036. /* Update templates for AP/mesh mode. */
  3037. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  3038. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  3039. b43_set_beacon_int(dev, conf->beacon_int);
  3040. if (!!conf->radio_enabled != phy->radio_on) {
  3041. if (conf->radio_enabled) {
  3042. b43_software_rfkill(dev, RFKILL_STATE_UNBLOCKED);
  3043. b43info(dev->wl, "Radio turned on by software\n");
  3044. if (!dev->radio_hw_enable) {
  3045. b43info(dev->wl, "The hardware RF-kill button "
  3046. "still turns the radio physically off. "
  3047. "Press the button to turn it on.\n");
  3048. }
  3049. } else {
  3050. b43_software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
  3051. b43info(dev->wl, "Radio turned off by software\n");
  3052. }
  3053. }
  3054. out_mac_enable:
  3055. b43_mac_enable(dev);
  3056. out_unlock_mutex:
  3057. mutex_unlock(&wl->mutex);
  3058. return err;
  3059. }
  3060. static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
  3061. {
  3062. struct ieee80211_supported_band *sband =
  3063. dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
  3064. struct ieee80211_rate *rate;
  3065. int i;
  3066. u16 basic, direct, offset, basic_offset, rateptr;
  3067. for (i = 0; i < sband->n_bitrates; i++) {
  3068. rate = &sband->bitrates[i];
  3069. if (b43_is_cck_rate(rate->hw_value)) {
  3070. direct = B43_SHM_SH_CCKDIRECT;
  3071. basic = B43_SHM_SH_CCKBASIC;
  3072. offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  3073. offset &= 0xF;
  3074. } else {
  3075. direct = B43_SHM_SH_OFDMDIRECT;
  3076. basic = B43_SHM_SH_OFDMBASIC;
  3077. offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  3078. offset &= 0xF;
  3079. }
  3080. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  3081. if (b43_is_cck_rate(rate->hw_value)) {
  3082. basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  3083. basic_offset &= 0xF;
  3084. } else {
  3085. basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  3086. basic_offset &= 0xF;
  3087. }
  3088. /*
  3089. * Get the pointer that we need to point to
  3090. * from the direct map
  3091. */
  3092. rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
  3093. direct + 2 * basic_offset);
  3094. /* and write it to the basic map */
  3095. b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
  3096. rateptr);
  3097. }
  3098. }
  3099. static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
  3100. struct ieee80211_vif *vif,
  3101. struct ieee80211_bss_conf *conf,
  3102. u32 changed)
  3103. {
  3104. struct b43_wl *wl = hw_to_b43_wl(hw);
  3105. struct b43_wldev *dev;
  3106. mutex_lock(&wl->mutex);
  3107. dev = wl->current_dev;
  3108. if (!dev || b43_status(dev) < B43_STAT_STARTED)
  3109. goto out_unlock_mutex;
  3110. b43_mac_suspend(dev);
  3111. if (changed & BSS_CHANGED_BASIC_RATES)
  3112. b43_update_basic_rates(dev, conf->basic_rates);
  3113. if (changed & BSS_CHANGED_ERP_SLOT) {
  3114. if (conf->use_short_slot)
  3115. b43_short_slot_timing_enable(dev);
  3116. else
  3117. b43_short_slot_timing_disable(dev);
  3118. }
  3119. b43_mac_enable(dev);
  3120. out_unlock_mutex:
  3121. mutex_unlock(&wl->mutex);
  3122. return;
  3123. }
  3124. static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3125. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  3126. struct ieee80211_key_conf *key)
  3127. {
  3128. struct b43_wl *wl = hw_to_b43_wl(hw);
  3129. struct b43_wldev *dev;
  3130. u8 algorithm;
  3131. u8 index;
  3132. int err;
  3133. static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  3134. if (modparam_nohwcrypt)
  3135. return -ENOSPC; /* User disabled HW-crypto */
  3136. mutex_lock(&wl->mutex);
  3137. spin_lock_irq(&wl->irq_lock);
  3138. write_lock(&wl->tx_lock);
  3139. /* Why do we need all this locking here?
  3140. * mutex -> Every config operation must take it.
  3141. * irq_lock -> We modify the dev->key array, which is accessed
  3142. * in the IRQ handlers.
  3143. * tx_lock -> We modify the dev->key array, which is accessed
  3144. * in the TX handler.
  3145. */
  3146. dev = wl->current_dev;
  3147. err = -ENODEV;
  3148. if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
  3149. goto out_unlock;
  3150. if (dev->fw.pcm_request_failed) {
  3151. /* We don't have firmware for the crypto engine.
  3152. * Must use software-crypto. */
  3153. err = -EOPNOTSUPP;
  3154. goto out_unlock;
  3155. }
  3156. err = -EINVAL;
  3157. switch (key->alg) {
  3158. case ALG_WEP:
  3159. if (key->keylen == LEN_WEP40)
  3160. algorithm = B43_SEC_ALGO_WEP40;
  3161. else
  3162. algorithm = B43_SEC_ALGO_WEP104;
  3163. break;
  3164. case ALG_TKIP:
  3165. algorithm = B43_SEC_ALGO_TKIP;
  3166. break;
  3167. case ALG_CCMP:
  3168. algorithm = B43_SEC_ALGO_AES;
  3169. break;
  3170. default:
  3171. B43_WARN_ON(1);
  3172. goto out_unlock;
  3173. }
  3174. index = (u8) (key->keyidx);
  3175. if (index > 3)
  3176. goto out_unlock;
  3177. switch (cmd) {
  3178. case SET_KEY:
  3179. if (algorithm == B43_SEC_ALGO_TKIP) {
  3180. /* FIXME: No TKIP hardware encryption for now. */
  3181. err = -EOPNOTSUPP;
  3182. goto out_unlock;
  3183. }
  3184. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  3185. if (WARN_ON(!sta)) {
  3186. err = -EOPNOTSUPP;
  3187. goto out_unlock;
  3188. }
  3189. /* Pairwise key with an assigned MAC address. */
  3190. err = b43_key_write(dev, -1, algorithm,
  3191. key->key, key->keylen,
  3192. sta->addr, key);
  3193. } else {
  3194. /* Group key */
  3195. err = b43_key_write(dev, index, algorithm,
  3196. key->key, key->keylen, NULL, key);
  3197. }
  3198. if (err)
  3199. goto out_unlock;
  3200. if (algorithm == B43_SEC_ALGO_WEP40 ||
  3201. algorithm == B43_SEC_ALGO_WEP104) {
  3202. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  3203. } else {
  3204. b43_hf_write(dev,
  3205. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  3206. }
  3207. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  3208. break;
  3209. case DISABLE_KEY: {
  3210. err = b43_key_clear(dev, key->hw_key_idx);
  3211. if (err)
  3212. goto out_unlock;
  3213. break;
  3214. }
  3215. default:
  3216. B43_WARN_ON(1);
  3217. }
  3218. out_unlock:
  3219. if (!err) {
  3220. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  3221. "mac: %pM\n",
  3222. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  3223. sta ? sta->addr : bcast_addr);
  3224. b43_dump_keymemory(dev);
  3225. }
  3226. write_unlock(&wl->tx_lock);
  3227. spin_unlock_irq(&wl->irq_lock);
  3228. mutex_unlock(&wl->mutex);
  3229. return err;
  3230. }
  3231. static void b43_op_configure_filter(struct ieee80211_hw *hw,
  3232. unsigned int changed, unsigned int *fflags,
  3233. int mc_count, struct dev_addr_list *mc_list)
  3234. {
  3235. struct b43_wl *wl = hw_to_b43_wl(hw);
  3236. struct b43_wldev *dev = wl->current_dev;
  3237. unsigned long flags;
  3238. if (!dev) {
  3239. *fflags = 0;
  3240. return;
  3241. }
  3242. spin_lock_irqsave(&wl->irq_lock, flags);
  3243. *fflags &= FIF_PROMISC_IN_BSS |
  3244. FIF_ALLMULTI |
  3245. FIF_FCSFAIL |
  3246. FIF_PLCPFAIL |
  3247. FIF_CONTROL |
  3248. FIF_OTHER_BSS |
  3249. FIF_BCN_PRBRESP_PROMISC;
  3250. changed &= FIF_PROMISC_IN_BSS |
  3251. FIF_ALLMULTI |
  3252. FIF_FCSFAIL |
  3253. FIF_PLCPFAIL |
  3254. FIF_CONTROL |
  3255. FIF_OTHER_BSS |
  3256. FIF_BCN_PRBRESP_PROMISC;
  3257. wl->filter_flags = *fflags;
  3258. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  3259. b43_adjust_opmode(dev);
  3260. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3261. }
  3262. static int b43_op_config_interface(struct ieee80211_hw *hw,
  3263. struct ieee80211_vif *vif,
  3264. struct ieee80211_if_conf *conf)
  3265. {
  3266. struct b43_wl *wl = hw_to_b43_wl(hw);
  3267. struct b43_wldev *dev = wl->current_dev;
  3268. unsigned long flags;
  3269. if (!dev)
  3270. return -ENODEV;
  3271. mutex_lock(&wl->mutex);
  3272. spin_lock_irqsave(&wl->irq_lock, flags);
  3273. B43_WARN_ON(wl->vif != vif);
  3274. if (conf->bssid)
  3275. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  3276. else
  3277. memset(wl->bssid, 0, ETH_ALEN);
  3278. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  3279. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  3280. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT)) {
  3281. B43_WARN_ON(vif->type != wl->if_type);
  3282. if (conf->changed & IEEE80211_IFCC_BEACON)
  3283. b43_update_templates(wl);
  3284. } else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
  3285. if (conf->changed & IEEE80211_IFCC_BEACON)
  3286. b43_update_templates(wl);
  3287. }
  3288. b43_write_mac_bssid_templates(dev);
  3289. }
  3290. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3291. mutex_unlock(&wl->mutex);
  3292. return 0;
  3293. }
  3294. /* Locking: wl->mutex */
  3295. static void b43_wireless_core_stop(struct b43_wldev *dev)
  3296. {
  3297. struct b43_wl *wl = dev->wl;
  3298. unsigned long flags;
  3299. if (b43_status(dev) < B43_STAT_STARTED)
  3300. return;
  3301. /* Disable and sync interrupts. We must do this before than
  3302. * setting the status to INITIALIZED, as the interrupt handler
  3303. * won't care about IRQs then. */
  3304. spin_lock_irqsave(&wl->irq_lock, flags);
  3305. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  3306. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  3307. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3308. b43_synchronize_irq(dev);
  3309. write_lock_irqsave(&wl->tx_lock, flags);
  3310. b43_set_status(dev, B43_STAT_INITIALIZED);
  3311. write_unlock_irqrestore(&wl->tx_lock, flags);
  3312. b43_pio_stop(dev);
  3313. mutex_unlock(&wl->mutex);
  3314. /* Must unlock as it would otherwise deadlock. No races here.
  3315. * Cancel the possibly running self-rearming periodic work. */
  3316. cancel_delayed_work_sync(&dev->periodic_work);
  3317. mutex_lock(&wl->mutex);
  3318. b43_mac_suspend(dev);
  3319. free_irq(dev->dev->irq, dev);
  3320. b43dbg(wl, "Wireless interface stopped\n");
  3321. }
  3322. /* Locking: wl->mutex */
  3323. static int b43_wireless_core_start(struct b43_wldev *dev)
  3324. {
  3325. int err;
  3326. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  3327. drain_txstatus_queue(dev);
  3328. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  3329. IRQF_SHARED, KBUILD_MODNAME, dev);
  3330. if (err) {
  3331. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  3332. goto out;
  3333. }
  3334. /* We are ready to run. */
  3335. b43_set_status(dev, B43_STAT_STARTED);
  3336. /* Start data flow (TX/RX). */
  3337. b43_mac_enable(dev);
  3338. b43_interrupt_enable(dev, dev->irq_savedstate);
  3339. /* Start maintainance work */
  3340. b43_periodic_tasks_setup(dev);
  3341. b43dbg(dev->wl, "Wireless interface started\n");
  3342. out:
  3343. return err;
  3344. }
  3345. /* Get PHY and RADIO versioning numbers */
  3346. static int b43_phy_versioning(struct b43_wldev *dev)
  3347. {
  3348. struct b43_phy *phy = &dev->phy;
  3349. u32 tmp;
  3350. u8 analog_type;
  3351. u8 phy_type;
  3352. u8 phy_rev;
  3353. u16 radio_manuf;
  3354. u16 radio_ver;
  3355. u16 radio_rev;
  3356. int unsupported = 0;
  3357. /* Get PHY versioning */
  3358. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  3359. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  3360. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  3361. phy_rev = (tmp & B43_PHYVER_VERSION);
  3362. switch (phy_type) {
  3363. case B43_PHYTYPE_A:
  3364. if (phy_rev >= 4)
  3365. unsupported = 1;
  3366. break;
  3367. case B43_PHYTYPE_B:
  3368. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  3369. && phy_rev != 7)
  3370. unsupported = 1;
  3371. break;
  3372. case B43_PHYTYPE_G:
  3373. if (phy_rev > 9)
  3374. unsupported = 1;
  3375. break;
  3376. #ifdef CONFIG_B43_NPHY
  3377. case B43_PHYTYPE_N:
  3378. if (phy_rev > 4)
  3379. unsupported = 1;
  3380. break;
  3381. #endif
  3382. #ifdef CONFIG_B43_PHY_LP
  3383. case B43_PHYTYPE_LP:
  3384. if (phy_rev > 1)
  3385. unsupported = 1;
  3386. break;
  3387. #endif
  3388. default:
  3389. unsupported = 1;
  3390. };
  3391. if (unsupported) {
  3392. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  3393. "(Analog %u, Type %u, Revision %u)\n",
  3394. analog_type, phy_type, phy_rev);
  3395. return -EOPNOTSUPP;
  3396. }
  3397. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  3398. analog_type, phy_type, phy_rev);
  3399. /* Get RADIO versioning */
  3400. if (dev->dev->bus->chip_id == 0x4317) {
  3401. if (dev->dev->bus->chip_rev == 0)
  3402. tmp = 0x3205017F;
  3403. else if (dev->dev->bus->chip_rev == 1)
  3404. tmp = 0x4205017F;
  3405. else
  3406. tmp = 0x5205017F;
  3407. } else {
  3408. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3409. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  3410. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3411. tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
  3412. }
  3413. radio_manuf = (tmp & 0x00000FFF);
  3414. radio_ver = (tmp & 0x0FFFF000) >> 12;
  3415. radio_rev = (tmp & 0xF0000000) >> 28;
  3416. if (radio_manuf != 0x17F /* Broadcom */)
  3417. unsupported = 1;
  3418. switch (phy_type) {
  3419. case B43_PHYTYPE_A:
  3420. if (radio_ver != 0x2060)
  3421. unsupported = 1;
  3422. if (radio_rev != 1)
  3423. unsupported = 1;
  3424. if (radio_manuf != 0x17F)
  3425. unsupported = 1;
  3426. break;
  3427. case B43_PHYTYPE_B:
  3428. if ((radio_ver & 0xFFF0) != 0x2050)
  3429. unsupported = 1;
  3430. break;
  3431. case B43_PHYTYPE_G:
  3432. if (radio_ver != 0x2050)
  3433. unsupported = 1;
  3434. break;
  3435. case B43_PHYTYPE_N:
  3436. if (radio_ver != 0x2055 && radio_ver != 0x2056)
  3437. unsupported = 1;
  3438. break;
  3439. case B43_PHYTYPE_LP:
  3440. if (radio_ver != 0x2062)
  3441. unsupported = 1;
  3442. break;
  3443. default:
  3444. B43_WARN_ON(1);
  3445. }
  3446. if (unsupported) {
  3447. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  3448. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  3449. radio_manuf, radio_ver, radio_rev);
  3450. return -EOPNOTSUPP;
  3451. }
  3452. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  3453. radio_manuf, radio_ver, radio_rev);
  3454. phy->radio_manuf = radio_manuf;
  3455. phy->radio_ver = radio_ver;
  3456. phy->radio_rev = radio_rev;
  3457. phy->analog = analog_type;
  3458. phy->type = phy_type;
  3459. phy->rev = phy_rev;
  3460. return 0;
  3461. }
  3462. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  3463. struct b43_phy *phy)
  3464. {
  3465. phy->hardware_power_control = !!modparam_hwpctl;
  3466. phy->next_txpwr_check_time = jiffies;
  3467. /* PHY TX errors counter. */
  3468. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  3469. #if B43_DEBUG
  3470. phy->phy_locked = 0;
  3471. phy->radio_locked = 0;
  3472. #endif
  3473. }
  3474. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  3475. {
  3476. dev->dfq_valid = 0;
  3477. /* Assume the radio is enabled. If it's not enabled, the state will
  3478. * immediately get fixed on the first periodic work run. */
  3479. dev->radio_hw_enable = 1;
  3480. /* Stats */
  3481. memset(&dev->stats, 0, sizeof(dev->stats));
  3482. setup_struct_phy_for_init(dev, &dev->phy);
  3483. /* IRQ related flags */
  3484. dev->irq_reason = 0;
  3485. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  3486. dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
  3487. if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
  3488. dev->irq_savedstate &= ~B43_IRQ_PHY_TXERR;
  3489. dev->mac_suspended = 1;
  3490. /* Noise calculation context */
  3491. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  3492. }
  3493. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  3494. {
  3495. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  3496. u64 hf;
  3497. if (!modparam_btcoex)
  3498. return;
  3499. if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
  3500. return;
  3501. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  3502. return;
  3503. hf = b43_hf_read(dev);
  3504. if (sprom->boardflags_lo & B43_BFL_BTCMOD)
  3505. hf |= B43_HF_BTCOEXALT;
  3506. else
  3507. hf |= B43_HF_BTCOEX;
  3508. b43_hf_write(dev, hf);
  3509. }
  3510. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  3511. {
  3512. if (!modparam_btcoex)
  3513. return;
  3514. //TODO
  3515. }
  3516. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  3517. {
  3518. #ifdef CONFIG_SSB_DRIVER_PCICORE
  3519. struct ssb_bus *bus = dev->dev->bus;
  3520. u32 tmp;
  3521. if (bus->pcicore.dev &&
  3522. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  3523. bus->pcicore.dev->id.revision <= 5) {
  3524. /* IMCFGLO timeouts workaround. */
  3525. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  3526. tmp &= ~SSB_IMCFGLO_REQTO;
  3527. tmp &= ~SSB_IMCFGLO_SERTO;
  3528. switch (bus->bustype) {
  3529. case SSB_BUSTYPE_PCI:
  3530. case SSB_BUSTYPE_PCMCIA:
  3531. tmp |= 0x32;
  3532. break;
  3533. case SSB_BUSTYPE_SSB:
  3534. tmp |= 0x53;
  3535. break;
  3536. }
  3537. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  3538. }
  3539. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  3540. }
  3541. static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
  3542. {
  3543. u16 pu_delay;
  3544. /* The time value is in microseconds. */
  3545. if (dev->phy.type == B43_PHYTYPE_A)
  3546. pu_delay = 3700;
  3547. else
  3548. pu_delay = 1050;
  3549. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  3550. pu_delay = 500;
  3551. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  3552. pu_delay = max(pu_delay, (u16)2400);
  3553. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
  3554. }
  3555. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  3556. static void b43_set_pretbtt(struct b43_wldev *dev)
  3557. {
  3558. u16 pretbtt;
  3559. /* The time value is in microseconds. */
  3560. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
  3561. pretbtt = 2;
  3562. } else {
  3563. if (dev->phy.type == B43_PHYTYPE_A)
  3564. pretbtt = 120;
  3565. else
  3566. pretbtt = 250;
  3567. }
  3568. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
  3569. b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
  3570. }
  3571. /* Shutdown a wireless core */
  3572. /* Locking: wl->mutex */
  3573. static void b43_wireless_core_exit(struct b43_wldev *dev)
  3574. {
  3575. u32 macctl;
  3576. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  3577. if (b43_status(dev) != B43_STAT_INITIALIZED)
  3578. return;
  3579. b43_set_status(dev, B43_STAT_UNINIT);
  3580. /* Stop the microcode PSM. */
  3581. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  3582. macctl &= ~B43_MACCTL_PSM_RUN;
  3583. macctl |= B43_MACCTL_PSM_JMP0;
  3584. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  3585. if (!dev->suspend_in_progress) {
  3586. b43_leds_exit(dev);
  3587. b43_rng_exit(dev->wl);
  3588. }
  3589. b43_dma_free(dev);
  3590. b43_pio_free(dev);
  3591. b43_chip_exit(dev);
  3592. dev->phy.ops->switch_analog(dev, 0);
  3593. if (dev->wl->current_beacon) {
  3594. dev_kfree_skb_any(dev->wl->current_beacon);
  3595. dev->wl->current_beacon = NULL;
  3596. }
  3597. ssb_device_disable(dev->dev, 0);
  3598. ssb_bus_may_powerdown(dev->dev->bus);
  3599. }
  3600. /* Initialize a wireless core */
  3601. static int b43_wireless_core_init(struct b43_wldev *dev)
  3602. {
  3603. struct b43_wl *wl = dev->wl;
  3604. struct ssb_bus *bus = dev->dev->bus;
  3605. struct ssb_sprom *sprom = &bus->sprom;
  3606. struct b43_phy *phy = &dev->phy;
  3607. int err;
  3608. u64 hf;
  3609. u32 tmp;
  3610. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3611. err = ssb_bus_powerup(bus, 0);
  3612. if (err)
  3613. goto out;
  3614. if (!ssb_device_is_enabled(dev->dev)) {
  3615. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  3616. b43_wireless_core_reset(dev, tmp);
  3617. }
  3618. /* Reset all data structures. */
  3619. setup_struct_wldev_for_init(dev);
  3620. phy->ops->prepare_structs(dev);
  3621. /* Enable IRQ routing to this device. */
  3622. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  3623. b43_imcfglo_timeouts_workaround(dev);
  3624. b43_bluetooth_coext_disable(dev);
  3625. if (phy->ops->prepare_hardware) {
  3626. err = phy->ops->prepare_hardware(dev);
  3627. if (err)
  3628. goto err_busdown;
  3629. }
  3630. err = b43_chip_init(dev);
  3631. if (err)
  3632. goto err_busdown;
  3633. b43_shm_write16(dev, B43_SHM_SHARED,
  3634. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  3635. hf = b43_hf_read(dev);
  3636. if (phy->type == B43_PHYTYPE_G) {
  3637. hf |= B43_HF_SYMW;
  3638. if (phy->rev == 1)
  3639. hf |= B43_HF_GDCW;
  3640. if (sprom->boardflags_lo & B43_BFL_PACTRL)
  3641. hf |= B43_HF_OFDMPABOOST;
  3642. }
  3643. if (phy->radio_ver == 0x2050) {
  3644. if (phy->radio_rev == 6)
  3645. hf |= B43_HF_4318TSSI;
  3646. if (phy->radio_rev < 6)
  3647. hf |= B43_HF_VCORECALC;
  3648. }
  3649. if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
  3650. hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
  3651. #ifdef CONFIG_SSB_DRIVER_PCICORE
  3652. if ((bus->bustype == SSB_BUSTYPE_PCI) &&
  3653. (bus->pcicore.dev->id.revision <= 10))
  3654. hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
  3655. #endif
  3656. hf &= ~B43_HF_SKCFPUP;
  3657. b43_hf_write(dev, hf);
  3658. b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
  3659. B43_DEFAULT_LONG_RETRY_LIMIT);
  3660. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  3661. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  3662. /* Disable sending probe responses from firmware.
  3663. * Setting the MaxTime to one usec will always trigger
  3664. * a timeout, so we never send any probe resp.
  3665. * A timeout of zero is infinite. */
  3666. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  3667. b43_rate_memory_init(dev);
  3668. b43_set_phytxctl_defaults(dev);
  3669. /* Minimum Contention Window */
  3670. if (phy->type == B43_PHYTYPE_B) {
  3671. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  3672. } else {
  3673. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  3674. }
  3675. /* Maximum Contention Window */
  3676. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  3677. if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
  3678. dev->__using_pio_transfers = 1;
  3679. err = b43_pio_init(dev);
  3680. } else {
  3681. dev->__using_pio_transfers = 0;
  3682. err = b43_dma_init(dev);
  3683. }
  3684. if (err)
  3685. goto err_chip_exit;
  3686. b43_qos_init(dev);
  3687. b43_set_synth_pu_delay(dev, 1);
  3688. b43_bluetooth_coext_enable(dev);
  3689. ssb_bus_powerup(bus, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
  3690. b43_upload_card_macaddress(dev);
  3691. b43_security_init(dev);
  3692. if (!dev->suspend_in_progress)
  3693. b43_rng_init(wl);
  3694. b43_set_status(dev, B43_STAT_INITIALIZED);
  3695. if (!dev->suspend_in_progress)
  3696. b43_leds_init(dev);
  3697. out:
  3698. return err;
  3699. err_chip_exit:
  3700. b43_chip_exit(dev);
  3701. err_busdown:
  3702. ssb_bus_may_powerdown(bus);
  3703. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3704. return err;
  3705. }
  3706. static int b43_op_add_interface(struct ieee80211_hw *hw,
  3707. struct ieee80211_if_init_conf *conf)
  3708. {
  3709. struct b43_wl *wl = hw_to_b43_wl(hw);
  3710. struct b43_wldev *dev;
  3711. unsigned long flags;
  3712. int err = -EOPNOTSUPP;
  3713. /* TODO: allow WDS/AP devices to coexist */
  3714. if (conf->type != NL80211_IFTYPE_AP &&
  3715. conf->type != NL80211_IFTYPE_MESH_POINT &&
  3716. conf->type != NL80211_IFTYPE_STATION &&
  3717. conf->type != NL80211_IFTYPE_WDS &&
  3718. conf->type != NL80211_IFTYPE_ADHOC)
  3719. return -EOPNOTSUPP;
  3720. mutex_lock(&wl->mutex);
  3721. if (wl->operating)
  3722. goto out_mutex_unlock;
  3723. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  3724. dev = wl->current_dev;
  3725. wl->operating = 1;
  3726. wl->vif = conf->vif;
  3727. wl->if_type = conf->type;
  3728. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  3729. spin_lock_irqsave(&wl->irq_lock, flags);
  3730. b43_adjust_opmode(dev);
  3731. b43_set_pretbtt(dev);
  3732. b43_set_synth_pu_delay(dev, 0);
  3733. b43_upload_card_macaddress(dev);
  3734. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3735. err = 0;
  3736. out_mutex_unlock:
  3737. mutex_unlock(&wl->mutex);
  3738. return err;
  3739. }
  3740. static void b43_op_remove_interface(struct ieee80211_hw *hw,
  3741. struct ieee80211_if_init_conf *conf)
  3742. {
  3743. struct b43_wl *wl = hw_to_b43_wl(hw);
  3744. struct b43_wldev *dev = wl->current_dev;
  3745. unsigned long flags;
  3746. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3747. mutex_lock(&wl->mutex);
  3748. B43_WARN_ON(!wl->operating);
  3749. B43_WARN_ON(wl->vif != conf->vif);
  3750. wl->vif = NULL;
  3751. wl->operating = 0;
  3752. spin_lock_irqsave(&wl->irq_lock, flags);
  3753. b43_adjust_opmode(dev);
  3754. memset(wl->mac_addr, 0, ETH_ALEN);
  3755. b43_upload_card_macaddress(dev);
  3756. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3757. mutex_unlock(&wl->mutex);
  3758. }
  3759. static int b43_op_start(struct ieee80211_hw *hw)
  3760. {
  3761. struct b43_wl *wl = hw_to_b43_wl(hw);
  3762. struct b43_wldev *dev = wl->current_dev;
  3763. int did_init = 0;
  3764. int err = 0;
  3765. bool do_rfkill_exit = 0;
  3766. /* Kill all old instance specific information to make sure
  3767. * the card won't use it in the short timeframe between start
  3768. * and mac80211 reconfiguring it. */
  3769. memset(wl->bssid, 0, ETH_ALEN);
  3770. memset(wl->mac_addr, 0, ETH_ALEN);
  3771. wl->filter_flags = 0;
  3772. wl->radiotap_enabled = 0;
  3773. b43_qos_clear(wl);
  3774. wl->beacon0_uploaded = 0;
  3775. wl->beacon1_uploaded = 0;
  3776. wl->beacon_templates_virgin = 1;
  3777. /* First register RFkill.
  3778. * LEDs that are registered later depend on it. */
  3779. b43_rfkill_init(dev);
  3780. mutex_lock(&wl->mutex);
  3781. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3782. err = b43_wireless_core_init(dev);
  3783. if (err) {
  3784. do_rfkill_exit = 1;
  3785. goto out_mutex_unlock;
  3786. }
  3787. did_init = 1;
  3788. }
  3789. if (b43_status(dev) < B43_STAT_STARTED) {
  3790. err = b43_wireless_core_start(dev);
  3791. if (err) {
  3792. if (did_init)
  3793. b43_wireless_core_exit(dev);
  3794. do_rfkill_exit = 1;
  3795. goto out_mutex_unlock;
  3796. }
  3797. }
  3798. out_mutex_unlock:
  3799. mutex_unlock(&wl->mutex);
  3800. if (do_rfkill_exit)
  3801. b43_rfkill_exit(dev);
  3802. return err;
  3803. }
  3804. static void b43_op_stop(struct ieee80211_hw *hw)
  3805. {
  3806. struct b43_wl *wl = hw_to_b43_wl(hw);
  3807. struct b43_wldev *dev = wl->current_dev;
  3808. b43_rfkill_exit(dev);
  3809. cancel_work_sync(&(wl->beacon_update_trigger));
  3810. mutex_lock(&wl->mutex);
  3811. if (b43_status(dev) >= B43_STAT_STARTED)
  3812. b43_wireless_core_stop(dev);
  3813. b43_wireless_core_exit(dev);
  3814. mutex_unlock(&wl->mutex);
  3815. cancel_work_sync(&(wl->txpower_adjust_work));
  3816. }
  3817. static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
  3818. struct ieee80211_sta *sta, bool set)
  3819. {
  3820. struct b43_wl *wl = hw_to_b43_wl(hw);
  3821. unsigned long flags;
  3822. spin_lock_irqsave(&wl->irq_lock, flags);
  3823. b43_update_templates(wl);
  3824. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3825. return 0;
  3826. }
  3827. static void b43_op_sta_notify(struct ieee80211_hw *hw,
  3828. struct ieee80211_vif *vif,
  3829. enum sta_notify_cmd notify_cmd,
  3830. struct ieee80211_sta *sta)
  3831. {
  3832. struct b43_wl *wl = hw_to_b43_wl(hw);
  3833. B43_WARN_ON(!vif || wl->vif != vif);
  3834. }
  3835. static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
  3836. {
  3837. struct b43_wl *wl = hw_to_b43_wl(hw);
  3838. struct b43_wldev *dev;
  3839. mutex_lock(&wl->mutex);
  3840. dev = wl->current_dev;
  3841. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
  3842. /* Disable CFP update during scan on other channels. */
  3843. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
  3844. }
  3845. mutex_unlock(&wl->mutex);
  3846. }
  3847. static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
  3848. {
  3849. struct b43_wl *wl = hw_to_b43_wl(hw);
  3850. struct b43_wldev *dev;
  3851. mutex_lock(&wl->mutex);
  3852. dev = wl->current_dev;
  3853. if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
  3854. /* Re-enable CFP update. */
  3855. b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
  3856. }
  3857. mutex_unlock(&wl->mutex);
  3858. }
  3859. static const struct ieee80211_ops b43_hw_ops = {
  3860. .tx = b43_op_tx,
  3861. .conf_tx = b43_op_conf_tx,
  3862. .add_interface = b43_op_add_interface,
  3863. .remove_interface = b43_op_remove_interface,
  3864. .config = b43_op_config,
  3865. .bss_info_changed = b43_op_bss_info_changed,
  3866. .config_interface = b43_op_config_interface,
  3867. .configure_filter = b43_op_configure_filter,
  3868. .set_key = b43_op_set_key,
  3869. .get_stats = b43_op_get_stats,
  3870. .get_tx_stats = b43_op_get_tx_stats,
  3871. .get_tsf = b43_op_get_tsf,
  3872. .set_tsf = b43_op_set_tsf,
  3873. .start = b43_op_start,
  3874. .stop = b43_op_stop,
  3875. .set_tim = b43_op_beacon_set_tim,
  3876. .sta_notify = b43_op_sta_notify,
  3877. .sw_scan_start = b43_op_sw_scan_start_notifier,
  3878. .sw_scan_complete = b43_op_sw_scan_complete_notifier,
  3879. };
  3880. /* Hard-reset the chip. Do not call this directly.
  3881. * Use b43_controller_restart()
  3882. */
  3883. static void b43_chip_reset(struct work_struct *work)
  3884. {
  3885. struct b43_wldev *dev =
  3886. container_of(work, struct b43_wldev, restart_work);
  3887. struct b43_wl *wl = dev->wl;
  3888. int err = 0;
  3889. int prev_status;
  3890. mutex_lock(&wl->mutex);
  3891. prev_status = b43_status(dev);
  3892. /* Bring the device down... */
  3893. if (prev_status >= B43_STAT_STARTED)
  3894. b43_wireless_core_stop(dev);
  3895. if (prev_status >= B43_STAT_INITIALIZED)
  3896. b43_wireless_core_exit(dev);
  3897. /* ...and up again. */
  3898. if (prev_status >= B43_STAT_INITIALIZED) {
  3899. err = b43_wireless_core_init(dev);
  3900. if (err)
  3901. goto out;
  3902. }
  3903. if (prev_status >= B43_STAT_STARTED) {
  3904. err = b43_wireless_core_start(dev);
  3905. if (err) {
  3906. b43_wireless_core_exit(dev);
  3907. goto out;
  3908. }
  3909. }
  3910. out:
  3911. if (err)
  3912. wl->current_dev = NULL; /* Failed to init the dev. */
  3913. mutex_unlock(&wl->mutex);
  3914. if (err)
  3915. b43err(wl, "Controller restart FAILED\n");
  3916. else
  3917. b43info(wl, "Controller restarted\n");
  3918. }
  3919. static int b43_setup_bands(struct b43_wldev *dev,
  3920. bool have_2ghz_phy, bool have_5ghz_phy)
  3921. {
  3922. struct ieee80211_hw *hw = dev->wl->hw;
  3923. if (have_2ghz_phy)
  3924. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
  3925. if (dev->phy.type == B43_PHYTYPE_N) {
  3926. if (have_5ghz_phy)
  3927. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
  3928. } else {
  3929. if (have_5ghz_phy)
  3930. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
  3931. }
  3932. dev->phy.supports_2ghz = have_2ghz_phy;
  3933. dev->phy.supports_5ghz = have_5ghz_phy;
  3934. return 0;
  3935. }
  3936. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3937. {
  3938. /* We release firmware that late to not be required to re-request
  3939. * is all the time when we reinit the core. */
  3940. b43_release_firmware(dev);
  3941. b43_phy_free(dev);
  3942. }
  3943. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3944. {
  3945. struct b43_wl *wl = dev->wl;
  3946. struct ssb_bus *bus = dev->dev->bus;
  3947. struct pci_dev *pdev = bus->host_pci;
  3948. int err;
  3949. bool have_2ghz_phy = 0, have_5ghz_phy = 0;
  3950. u32 tmp;
  3951. /* Do NOT do any device initialization here.
  3952. * Do it in wireless_core_init() instead.
  3953. * This function is for gathering basic information about the HW, only.
  3954. * Also some structs may be set up here. But most likely you want to have
  3955. * that in core_init(), too.
  3956. */
  3957. err = ssb_bus_powerup(bus, 0);
  3958. if (err) {
  3959. b43err(wl, "Bus powerup failed\n");
  3960. goto out;
  3961. }
  3962. /* Get the PHY type. */
  3963. if (dev->dev->id.revision >= 5) {
  3964. u32 tmshigh;
  3965. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3966. have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
  3967. have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
  3968. } else
  3969. B43_WARN_ON(1);
  3970. dev->phy.gmode = have_2ghz_phy;
  3971. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3972. b43_wireless_core_reset(dev, tmp);
  3973. err = b43_phy_versioning(dev);
  3974. if (err)
  3975. goto err_powerdown;
  3976. /* Check if this device supports multiband. */
  3977. if (!pdev ||
  3978. (pdev->device != 0x4312 &&
  3979. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3980. /* No multiband support. */
  3981. have_2ghz_phy = 0;
  3982. have_5ghz_phy = 0;
  3983. switch (dev->phy.type) {
  3984. case B43_PHYTYPE_A:
  3985. have_5ghz_phy = 1;
  3986. break;
  3987. case B43_PHYTYPE_G:
  3988. case B43_PHYTYPE_N:
  3989. case B43_PHYTYPE_LP:
  3990. have_2ghz_phy = 1;
  3991. break;
  3992. default:
  3993. B43_WARN_ON(1);
  3994. }
  3995. }
  3996. if (dev->phy.type == B43_PHYTYPE_A) {
  3997. /* FIXME */
  3998. b43err(wl, "IEEE 802.11a devices are unsupported\n");
  3999. err = -EOPNOTSUPP;
  4000. goto err_powerdown;
  4001. }
  4002. if (1 /* disable A-PHY */) {
  4003. /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
  4004. if (dev->phy.type != B43_PHYTYPE_N) {
  4005. have_2ghz_phy = 1;
  4006. have_5ghz_phy = 0;
  4007. }
  4008. }
  4009. err = b43_phy_allocate(dev);
  4010. if (err)
  4011. goto err_powerdown;
  4012. dev->phy.gmode = have_2ghz_phy;
  4013. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  4014. b43_wireless_core_reset(dev, tmp);
  4015. err = b43_validate_chipaccess(dev);
  4016. if (err)
  4017. goto err_phy_free;
  4018. err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
  4019. if (err)
  4020. goto err_phy_free;
  4021. /* Now set some default "current_dev" */
  4022. if (!wl->current_dev)
  4023. wl->current_dev = dev;
  4024. INIT_WORK(&dev->restart_work, b43_chip_reset);
  4025. dev->phy.ops->switch_analog(dev, 0);
  4026. ssb_device_disable(dev->dev, 0);
  4027. ssb_bus_may_powerdown(bus);
  4028. out:
  4029. return err;
  4030. err_phy_free:
  4031. b43_phy_free(dev);
  4032. err_powerdown:
  4033. ssb_bus_may_powerdown(bus);
  4034. return err;
  4035. }
  4036. static void b43_one_core_detach(struct ssb_device *dev)
  4037. {
  4038. struct b43_wldev *wldev;
  4039. struct b43_wl *wl;
  4040. /* Do not cancel ieee80211-workqueue based work here.
  4041. * See comment in b43_remove(). */
  4042. wldev = ssb_get_drvdata(dev);
  4043. wl = wldev->wl;
  4044. b43_debugfs_remove_device(wldev);
  4045. b43_wireless_core_detach(wldev);
  4046. list_del(&wldev->list);
  4047. wl->nr_devs--;
  4048. ssb_set_drvdata(dev, NULL);
  4049. kfree(wldev);
  4050. }
  4051. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  4052. {
  4053. struct b43_wldev *wldev;
  4054. struct pci_dev *pdev;
  4055. int err = -ENOMEM;
  4056. if (!list_empty(&wl->devlist)) {
  4057. /* We are not the first core on this chip. */
  4058. pdev = dev->bus->host_pci;
  4059. /* Only special chips support more than one wireless
  4060. * core, although some of the other chips have more than
  4061. * one wireless core as well. Check for this and
  4062. * bail out early.
  4063. */
  4064. if (!pdev ||
  4065. ((pdev->device != 0x4321) &&
  4066. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  4067. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  4068. return -ENODEV;
  4069. }
  4070. }
  4071. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  4072. if (!wldev)
  4073. goto out;
  4074. wldev->dev = dev;
  4075. wldev->wl = wl;
  4076. b43_set_status(wldev, B43_STAT_UNINIT);
  4077. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  4078. tasklet_init(&wldev->isr_tasklet,
  4079. (void (*)(unsigned long))b43_interrupt_tasklet,
  4080. (unsigned long)wldev);
  4081. INIT_LIST_HEAD(&wldev->list);
  4082. err = b43_wireless_core_attach(wldev);
  4083. if (err)
  4084. goto err_kfree_wldev;
  4085. list_add(&wldev->list, &wl->devlist);
  4086. wl->nr_devs++;
  4087. ssb_set_drvdata(dev, wldev);
  4088. b43_debugfs_add_device(wldev);
  4089. out:
  4090. return err;
  4091. err_kfree_wldev:
  4092. kfree(wldev);
  4093. return err;
  4094. }
  4095. #define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
  4096. (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
  4097. (pdev->device == _device) && \
  4098. (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
  4099. (pdev->subsystem_device == _subdevice) )
  4100. static void b43_sprom_fixup(struct ssb_bus *bus)
  4101. {
  4102. struct pci_dev *pdev;
  4103. /* boardflags workarounds */
  4104. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  4105. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  4106. bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
  4107. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  4108. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  4109. bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
  4110. if (bus->bustype == SSB_BUSTYPE_PCI) {
  4111. pdev = bus->host_pci;
  4112. if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
  4113. IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
  4114. IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
  4115. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
  4116. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
  4117. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
  4118. IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
  4119. bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
  4120. }
  4121. }
  4122. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  4123. {
  4124. struct ieee80211_hw *hw = wl->hw;
  4125. ssb_set_devtypedata(dev, NULL);
  4126. ieee80211_free_hw(hw);
  4127. }
  4128. static int b43_wireless_init(struct ssb_device *dev)
  4129. {
  4130. struct ssb_sprom *sprom = &dev->bus->sprom;
  4131. struct ieee80211_hw *hw;
  4132. struct b43_wl *wl;
  4133. int err = -ENOMEM;
  4134. b43_sprom_fixup(dev->bus);
  4135. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  4136. if (!hw) {
  4137. b43err(NULL, "Could not allocate ieee80211 device\n");
  4138. goto out;
  4139. }
  4140. /* fill hw info */
  4141. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  4142. IEEE80211_HW_SIGNAL_DBM |
  4143. IEEE80211_HW_NOISE_DBM;
  4144. hw->wiphy->interface_modes =
  4145. BIT(NL80211_IFTYPE_AP) |
  4146. BIT(NL80211_IFTYPE_MESH_POINT) |
  4147. BIT(NL80211_IFTYPE_STATION) |
  4148. BIT(NL80211_IFTYPE_WDS) |
  4149. BIT(NL80211_IFTYPE_ADHOC);
  4150. hw->queues = b43_modparam_qos ? 4 : 1;
  4151. hw->max_rates = 2;
  4152. SET_IEEE80211_DEV(hw, dev->dev);
  4153. if (is_valid_ether_addr(sprom->et1mac))
  4154. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  4155. else
  4156. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  4157. /* Get and initialize struct b43_wl */
  4158. wl = hw_to_b43_wl(hw);
  4159. memset(wl, 0, sizeof(*wl));
  4160. wl->hw = hw;
  4161. spin_lock_init(&wl->irq_lock);
  4162. rwlock_init(&wl->tx_lock);
  4163. spin_lock_init(&wl->leds_lock);
  4164. spin_lock_init(&wl->shm_lock);
  4165. mutex_init(&wl->mutex);
  4166. INIT_LIST_HEAD(&wl->devlist);
  4167. INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
  4168. INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
  4169. ssb_set_devtypedata(dev, wl);
  4170. b43info(wl, "Broadcom %04X WLAN found (core revision %u)\n",
  4171. dev->bus->chip_id, dev->id.revision);
  4172. err = 0;
  4173. out:
  4174. return err;
  4175. }
  4176. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  4177. {
  4178. struct b43_wl *wl;
  4179. int err;
  4180. int first = 0;
  4181. wl = ssb_get_devtypedata(dev);
  4182. if (!wl) {
  4183. /* Probing the first core. Must setup common struct b43_wl */
  4184. first = 1;
  4185. err = b43_wireless_init(dev);
  4186. if (err)
  4187. goto out;
  4188. wl = ssb_get_devtypedata(dev);
  4189. B43_WARN_ON(!wl);
  4190. }
  4191. err = b43_one_core_attach(dev, wl);
  4192. if (err)
  4193. goto err_wireless_exit;
  4194. if (first) {
  4195. err = ieee80211_register_hw(wl->hw);
  4196. if (err)
  4197. goto err_one_core_detach;
  4198. }
  4199. out:
  4200. return err;
  4201. err_one_core_detach:
  4202. b43_one_core_detach(dev);
  4203. err_wireless_exit:
  4204. if (first)
  4205. b43_wireless_exit(dev, wl);
  4206. return err;
  4207. }
  4208. static void b43_remove(struct ssb_device *dev)
  4209. {
  4210. struct b43_wl *wl = ssb_get_devtypedata(dev);
  4211. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4212. /* We must cancel any work here before unregistering from ieee80211,
  4213. * as the ieee80211 unreg will destroy the workqueue. */
  4214. cancel_work_sync(&wldev->restart_work);
  4215. B43_WARN_ON(!wl);
  4216. if (wl->current_dev == wldev)
  4217. ieee80211_unregister_hw(wl->hw);
  4218. b43_one_core_detach(dev);
  4219. if (list_empty(&wl->devlist)) {
  4220. /* Last core on the chip unregistered.
  4221. * We can destroy common struct b43_wl.
  4222. */
  4223. b43_wireless_exit(dev, wl);
  4224. }
  4225. }
  4226. /* Perform a hardware reset. This can be called from any context. */
  4227. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  4228. {
  4229. /* Must avoid requeueing, if we are in shutdown. */
  4230. if (b43_status(dev) < B43_STAT_INITIALIZED)
  4231. return;
  4232. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  4233. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  4234. }
  4235. #ifdef CONFIG_PM
  4236. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  4237. {
  4238. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4239. struct b43_wl *wl = wldev->wl;
  4240. b43dbg(wl, "Suspending...\n");
  4241. mutex_lock(&wl->mutex);
  4242. wldev->suspend_in_progress = true;
  4243. wldev->suspend_init_status = b43_status(wldev);
  4244. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  4245. b43_wireless_core_stop(wldev);
  4246. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  4247. b43_wireless_core_exit(wldev);
  4248. mutex_unlock(&wl->mutex);
  4249. b43dbg(wl, "Device suspended.\n");
  4250. return 0;
  4251. }
  4252. static int b43_resume(struct ssb_device *dev)
  4253. {
  4254. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4255. struct b43_wl *wl = wldev->wl;
  4256. int err = 0;
  4257. b43dbg(wl, "Resuming...\n");
  4258. mutex_lock(&wl->mutex);
  4259. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  4260. err = b43_wireless_core_init(wldev);
  4261. if (err) {
  4262. b43err(wl, "Resume failed at core init\n");
  4263. goto out;
  4264. }
  4265. }
  4266. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  4267. err = b43_wireless_core_start(wldev);
  4268. if (err) {
  4269. b43_leds_exit(wldev);
  4270. b43_rng_exit(wldev->wl);
  4271. b43_wireless_core_exit(wldev);
  4272. b43err(wl, "Resume failed at core start\n");
  4273. goto out;
  4274. }
  4275. }
  4276. b43dbg(wl, "Device resumed.\n");
  4277. out:
  4278. wldev->suspend_in_progress = false;
  4279. mutex_unlock(&wl->mutex);
  4280. return err;
  4281. }
  4282. #else /* CONFIG_PM */
  4283. # define b43_suspend NULL
  4284. # define b43_resume NULL
  4285. #endif /* CONFIG_PM */
  4286. static struct ssb_driver b43_ssb_driver = {
  4287. .name = KBUILD_MODNAME,
  4288. .id_table = b43_ssb_tbl,
  4289. .probe = b43_probe,
  4290. .remove = b43_remove,
  4291. .suspend = b43_suspend,
  4292. .resume = b43_resume,
  4293. };
  4294. static void b43_print_driverinfo(void)
  4295. {
  4296. const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
  4297. *feat_leds = "", *feat_rfkill = "";
  4298. #ifdef CONFIG_B43_PCI_AUTOSELECT
  4299. feat_pci = "P";
  4300. #endif
  4301. #ifdef CONFIG_B43_PCMCIA
  4302. feat_pcmcia = "M";
  4303. #endif
  4304. #ifdef CONFIG_B43_NPHY
  4305. feat_nphy = "N";
  4306. #endif
  4307. #ifdef CONFIG_B43_LEDS
  4308. feat_leds = "L";
  4309. #endif
  4310. #ifdef CONFIG_B43_RFKILL
  4311. feat_rfkill = "R";
  4312. #endif
  4313. printk(KERN_INFO "Broadcom 43xx driver loaded "
  4314. "[ Features: %s%s%s%s%s, Firmware-ID: "
  4315. B43_SUPPORTED_FIRMWARE_ID " ]\n",
  4316. feat_pci, feat_pcmcia, feat_nphy,
  4317. feat_leds, feat_rfkill);
  4318. }
  4319. static int __init b43_init(void)
  4320. {
  4321. int err;
  4322. b43_debugfs_init();
  4323. err = b43_pcmcia_init();
  4324. if (err)
  4325. goto err_dfs_exit;
  4326. err = ssb_driver_register(&b43_ssb_driver);
  4327. if (err)
  4328. goto err_pcmcia_exit;
  4329. b43_print_driverinfo();
  4330. return err;
  4331. err_pcmcia_exit:
  4332. b43_pcmcia_exit();
  4333. err_dfs_exit:
  4334. b43_debugfs_exit();
  4335. return err;
  4336. }
  4337. static void __exit b43_exit(void)
  4338. {
  4339. ssb_driver_unregister(&b43_ssb_driver);
  4340. b43_pcmcia_exit();
  4341. b43_debugfs_exit();
  4342. }
  4343. module_init(b43_init)
  4344. module_exit(b43_exit)