main.c 132 KB

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