main.c 133 KB

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