iwl3945-base.c 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/wireless.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/ieee80211_radiotap.h>
  42. #include <net/mac80211.h>
  43. #include <asm/div64.h>
  44. #include "iwl-3945-core.h"
  45. #include "iwl-3945.h"
  46. #include "iwl-helpers.h"
  47. #ifdef CONFIG_IWL3945_DEBUG
  48. u32 iwl3945_debug_level;
  49. #endif
  50. static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
  51. struct iwl3945_tx_queue *txq);
  52. /******************************************************************************
  53. *
  54. * module boiler plate
  55. *
  56. ******************************************************************************/
  57. /* module parameters */
  58. static int iwl3945_param_disable_hw_scan; /* def: 0 = use 3945's h/w scan */
  59. static int iwl3945_param_debug; /* def: 0 = minimal debug log messages */
  60. static int iwl3945_param_disable; /* def: 0 = enable radio */
  61. static int iwl3945_param_antenna; /* def: 0 = both antennas (use diversity) */
  62. int iwl3945_param_hwcrypto; /* def: 0 = use software encryption */
  63. static int iwl3945_param_qos_enable = 1; /* def: 1 = use quality of service */
  64. int iwl3945_param_queues_num = IWL39_MAX_NUM_QUEUES; /* def: 8 Tx queues */
  65. /*
  66. * module name, copyright, version, etc.
  67. * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
  68. */
  69. #define DRV_DESCRIPTION \
  70. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  71. #ifdef CONFIG_IWL3945_DEBUG
  72. #define VD "d"
  73. #else
  74. #define VD
  75. #endif
  76. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  77. #define VS "s"
  78. #else
  79. #define VS
  80. #endif
  81. #define IWLWIFI_VERSION "1.2.26k" VD VS
  82. #define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
  83. #define DRV_VERSION IWLWIFI_VERSION
  84. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  85. MODULE_VERSION(DRV_VERSION);
  86. MODULE_AUTHOR(DRV_COPYRIGHT);
  87. MODULE_LICENSE("GPL");
  88. static const struct ieee80211_supported_band *iwl3945_get_band(
  89. struct iwl3945_priv *priv, enum ieee80211_band band)
  90. {
  91. return priv->hw->wiphy->bands[band];
  92. }
  93. static int iwl3945_is_empty_essid(const char *essid, int essid_len)
  94. {
  95. /* Single white space is for Linksys APs */
  96. if (essid_len == 1 && essid[0] == ' ')
  97. return 1;
  98. /* Otherwise, if the entire essid is 0, we assume it is hidden */
  99. while (essid_len) {
  100. essid_len--;
  101. if (essid[essid_len] != '\0')
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static const char *iwl3945_escape_essid(const char *essid, u8 essid_len)
  107. {
  108. static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
  109. const char *s = essid;
  110. char *d = escaped;
  111. if (iwl3945_is_empty_essid(essid, essid_len)) {
  112. memcpy(escaped, "<hidden>", sizeof("<hidden>"));
  113. return escaped;
  114. }
  115. essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
  116. while (essid_len--) {
  117. if (*s == '\0') {
  118. *d++ = '\\';
  119. *d++ = '0';
  120. s++;
  121. } else
  122. *d++ = *s++;
  123. }
  124. *d = '\0';
  125. return escaped;
  126. }
  127. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  128. * DMA services
  129. *
  130. * Theory of operation
  131. *
  132. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  133. * of buffer descriptors, each of which points to one or more data buffers for
  134. * the device to read from or fill. Driver and device exchange status of each
  135. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  136. * entries in each circular buffer, to protect against confusing empty and full
  137. * queue states.
  138. *
  139. * The device reads or writes the data in the queues via the device's several
  140. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  141. *
  142. * For Tx queue, there are low mark and high mark limits. If, after queuing
  143. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  144. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  145. * Tx queue resumed.
  146. *
  147. * The 3945 operates with six queues: One receive queue, one transmit queue
  148. * (#4) for sending commands to the device firmware, and four transmit queues
  149. * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
  150. ***************************************************/
  151. int iwl3945_queue_space(const struct iwl3945_queue *q)
  152. {
  153. int s = q->read_ptr - q->write_ptr;
  154. if (q->read_ptr > q->write_ptr)
  155. s -= q->n_bd;
  156. if (s <= 0)
  157. s += q->n_window;
  158. /* keep some reserve to not confuse empty and full situations */
  159. s -= 2;
  160. if (s < 0)
  161. s = 0;
  162. return s;
  163. }
  164. int iwl3945_x2_queue_used(const struct iwl3945_queue *q, int i)
  165. {
  166. return q->write_ptr > q->read_ptr ?
  167. (i >= q->read_ptr && i < q->write_ptr) :
  168. !(i < q->read_ptr && i >= q->write_ptr);
  169. }
  170. static inline u8 get_cmd_index(struct iwl3945_queue *q, u32 index, int is_huge)
  171. {
  172. /* This is for scan command, the big buffer at end of command array */
  173. if (is_huge)
  174. return q->n_window; /* must be power of 2 */
  175. /* Otherwise, use normal size buffers */
  176. return index & (q->n_window - 1);
  177. }
  178. /**
  179. * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
  180. */
  181. static int iwl3945_queue_init(struct iwl3945_priv *priv, struct iwl3945_queue *q,
  182. int count, int slots_num, u32 id)
  183. {
  184. q->n_bd = count;
  185. q->n_window = slots_num;
  186. q->id = id;
  187. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  188. * and iwl_queue_dec_wrap are broken. */
  189. BUG_ON(!is_power_of_2(count));
  190. /* slots_num must be power-of-two size, otherwise
  191. * get_cmd_index is broken. */
  192. BUG_ON(!is_power_of_2(slots_num));
  193. q->low_mark = q->n_window / 4;
  194. if (q->low_mark < 4)
  195. q->low_mark = 4;
  196. q->high_mark = q->n_window / 8;
  197. if (q->high_mark < 2)
  198. q->high_mark = 2;
  199. q->write_ptr = q->read_ptr = 0;
  200. return 0;
  201. }
  202. /**
  203. * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  204. */
  205. static int iwl3945_tx_queue_alloc(struct iwl3945_priv *priv,
  206. struct iwl3945_tx_queue *txq, u32 id)
  207. {
  208. struct pci_dev *dev = priv->pci_dev;
  209. /* Driver private data, only for Tx (not command) queues,
  210. * not shared with device. */
  211. if (id != IWL_CMD_QUEUE_NUM) {
  212. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  213. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  214. if (!txq->txb) {
  215. IWL_ERROR("kmalloc for auxiliary BD "
  216. "structures failed\n");
  217. goto error;
  218. }
  219. } else
  220. txq->txb = NULL;
  221. /* Circular buffer of transmit frame descriptors (TFDs),
  222. * shared with device */
  223. txq->bd = pci_alloc_consistent(dev,
  224. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  225. &txq->q.dma_addr);
  226. if (!txq->bd) {
  227. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  228. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  229. goto error;
  230. }
  231. txq->q.id = id;
  232. return 0;
  233. error:
  234. kfree(txq->txb);
  235. txq->txb = NULL;
  236. return -ENOMEM;
  237. }
  238. /**
  239. * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
  240. */
  241. int iwl3945_tx_queue_init(struct iwl3945_priv *priv,
  242. struct iwl3945_tx_queue *txq, int slots_num, u32 txq_id)
  243. {
  244. struct pci_dev *dev = priv->pci_dev;
  245. int len;
  246. int rc = 0;
  247. /*
  248. * Alloc buffer array for commands (Tx or other types of commands).
  249. * For the command queue (#4), allocate command space + one big
  250. * command for scan, since scan command is very huge; the system will
  251. * not have two scans at the same time, so only one is needed.
  252. * For data Tx queues (all other queues), no super-size command
  253. * space is needed.
  254. */
  255. len = sizeof(struct iwl3945_cmd) * slots_num;
  256. if (txq_id == IWL_CMD_QUEUE_NUM)
  257. len += IWL_MAX_SCAN_SIZE;
  258. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  259. if (!txq->cmd)
  260. return -ENOMEM;
  261. /* Alloc driver data array and TFD circular buffer */
  262. rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
  263. if (rc) {
  264. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  265. return -ENOMEM;
  266. }
  267. txq->need_update = 0;
  268. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  269. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  270. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  271. /* Initialize queue high/low-water, head/tail indexes */
  272. iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  273. /* Tell device where to find queue, enable DMA channel. */
  274. iwl3945_hw_tx_queue_init(priv, txq);
  275. return 0;
  276. }
  277. /**
  278. * iwl3945_tx_queue_free - Deallocate DMA queue.
  279. * @txq: Transmit queue to deallocate.
  280. *
  281. * Empty queue by removing and destroying all BD's.
  282. * Free all buffers.
  283. * 0-fill, but do not free "txq" descriptor structure.
  284. */
  285. void iwl3945_tx_queue_free(struct iwl3945_priv *priv, struct iwl3945_tx_queue *txq)
  286. {
  287. struct iwl3945_queue *q = &txq->q;
  288. struct pci_dev *dev = priv->pci_dev;
  289. int len;
  290. if (q->n_bd == 0)
  291. return;
  292. /* first, empty all BD's */
  293. for (; q->write_ptr != q->read_ptr;
  294. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  295. iwl3945_hw_txq_free_tfd(priv, txq);
  296. len = sizeof(struct iwl3945_cmd) * q->n_window;
  297. if (q->id == IWL_CMD_QUEUE_NUM)
  298. len += IWL_MAX_SCAN_SIZE;
  299. /* De-alloc array of command/tx buffers */
  300. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  301. /* De-alloc circular buffer of TFDs */
  302. if (txq->q.n_bd)
  303. pci_free_consistent(dev, sizeof(struct iwl3945_tfd_frame) *
  304. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  305. /* De-alloc array of per-TFD driver data */
  306. kfree(txq->txb);
  307. txq->txb = NULL;
  308. /* 0-fill queue descriptor structure */
  309. memset(txq, 0, sizeof(*txq));
  310. }
  311. const u8 iwl3945_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  312. /*************** STATION TABLE MANAGEMENT ****
  313. * mac80211 should be examined to determine if sta_info is duplicating
  314. * the functionality provided here
  315. */
  316. /**************************************************************/
  317. #if 0 /* temporary disable till we add real remove station */
  318. /**
  319. * iwl3945_remove_station - Remove driver's knowledge of station.
  320. *
  321. * NOTE: This does not remove station from device's station table.
  322. */
  323. static u8 iwl3945_remove_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap)
  324. {
  325. int index = IWL_INVALID_STATION;
  326. int i;
  327. unsigned long flags;
  328. spin_lock_irqsave(&priv->sta_lock, flags);
  329. if (is_ap)
  330. index = IWL_AP_ID;
  331. else if (is_broadcast_ether_addr(addr))
  332. index = priv->hw_setting.bcast_sta_id;
  333. else
  334. for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
  335. if (priv->stations[i].used &&
  336. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  337. addr)) {
  338. index = i;
  339. break;
  340. }
  341. if (unlikely(index == IWL_INVALID_STATION))
  342. goto out;
  343. if (priv->stations[index].used) {
  344. priv->stations[index].used = 0;
  345. priv->num_stations--;
  346. }
  347. BUG_ON(priv->num_stations < 0);
  348. out:
  349. spin_unlock_irqrestore(&priv->sta_lock, flags);
  350. return 0;
  351. }
  352. #endif
  353. /**
  354. * iwl3945_clear_stations_table - Clear the driver's station table
  355. *
  356. * NOTE: This does not clear or otherwise alter the device's station table.
  357. */
  358. static void iwl3945_clear_stations_table(struct iwl3945_priv *priv)
  359. {
  360. unsigned long flags;
  361. spin_lock_irqsave(&priv->sta_lock, flags);
  362. priv->num_stations = 0;
  363. memset(priv->stations, 0, sizeof(priv->stations));
  364. spin_unlock_irqrestore(&priv->sta_lock, flags);
  365. }
  366. /**
  367. * iwl3945_add_station - Add station to station tables in driver and device
  368. */
  369. u8 iwl3945_add_station(struct iwl3945_priv *priv, const u8 *addr, int is_ap, u8 flags)
  370. {
  371. int i;
  372. int index = IWL_INVALID_STATION;
  373. struct iwl3945_station_entry *station;
  374. unsigned long flags_spin;
  375. DECLARE_MAC_BUF(mac);
  376. u8 rate;
  377. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  378. if (is_ap)
  379. index = IWL_AP_ID;
  380. else if (is_broadcast_ether_addr(addr))
  381. index = priv->hw_setting.bcast_sta_id;
  382. else
  383. for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
  384. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  385. addr)) {
  386. index = i;
  387. break;
  388. }
  389. if (!priv->stations[i].used &&
  390. index == IWL_INVALID_STATION)
  391. index = i;
  392. }
  393. /* These two conditions has the same outcome but keep them separate
  394. since they have different meaning */
  395. if (unlikely(index == IWL_INVALID_STATION)) {
  396. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  397. return index;
  398. }
  399. if (priv->stations[index].used &&
  400. !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
  401. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  402. return index;
  403. }
  404. IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
  405. station = &priv->stations[index];
  406. station->used = 1;
  407. priv->num_stations++;
  408. /* Set up the REPLY_ADD_STA command to send to device */
  409. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  410. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  411. station->sta.mode = 0;
  412. station->sta.sta.sta_id = index;
  413. station->sta.station_flags = 0;
  414. if (priv->band == IEEE80211_BAND_5GHZ)
  415. rate = IWL_RATE_6M_PLCP;
  416. else
  417. rate = IWL_RATE_1M_PLCP;
  418. /* Turn on both antennas for the station... */
  419. station->sta.rate_n_flags =
  420. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  421. station->current_rate.rate_n_flags =
  422. le16_to_cpu(station->sta.rate_n_flags);
  423. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  424. /* Add station to device's station table */
  425. iwl3945_send_add_station(priv, &station->sta, flags);
  426. return index;
  427. }
  428. /*************** DRIVER STATUS FUNCTIONS *****/
  429. static inline int iwl3945_is_ready(struct iwl3945_priv *priv)
  430. {
  431. /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
  432. * set but EXIT_PENDING is not */
  433. return test_bit(STATUS_READY, &priv->status) &&
  434. test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
  435. !test_bit(STATUS_EXIT_PENDING, &priv->status);
  436. }
  437. static inline int iwl3945_is_alive(struct iwl3945_priv *priv)
  438. {
  439. return test_bit(STATUS_ALIVE, &priv->status);
  440. }
  441. static inline int iwl3945_is_init(struct iwl3945_priv *priv)
  442. {
  443. return test_bit(STATUS_INIT, &priv->status);
  444. }
  445. static inline int iwl3945_is_rfkill_sw(struct iwl3945_priv *priv)
  446. {
  447. return test_bit(STATUS_RF_KILL_SW, &priv->status);
  448. }
  449. static inline int iwl3945_is_rfkill_hw(struct iwl3945_priv *priv)
  450. {
  451. return test_bit(STATUS_RF_KILL_HW, &priv->status);
  452. }
  453. static inline int iwl3945_is_rfkill(struct iwl3945_priv *priv)
  454. {
  455. return iwl3945_is_rfkill_hw(priv) ||
  456. iwl3945_is_rfkill_sw(priv);
  457. }
  458. static inline int iwl3945_is_ready_rf(struct iwl3945_priv *priv)
  459. {
  460. if (iwl3945_is_rfkill(priv))
  461. return 0;
  462. return iwl3945_is_ready(priv);
  463. }
  464. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  465. #define IWL_CMD(x) case x : return #x
  466. static const char *get_cmd_string(u8 cmd)
  467. {
  468. switch (cmd) {
  469. IWL_CMD(REPLY_ALIVE);
  470. IWL_CMD(REPLY_ERROR);
  471. IWL_CMD(REPLY_RXON);
  472. IWL_CMD(REPLY_RXON_ASSOC);
  473. IWL_CMD(REPLY_QOS_PARAM);
  474. IWL_CMD(REPLY_RXON_TIMING);
  475. IWL_CMD(REPLY_ADD_STA);
  476. IWL_CMD(REPLY_REMOVE_STA);
  477. IWL_CMD(REPLY_REMOVE_ALL_STA);
  478. IWL_CMD(REPLY_3945_RX);
  479. IWL_CMD(REPLY_TX);
  480. IWL_CMD(REPLY_RATE_SCALE);
  481. IWL_CMD(REPLY_LEDS_CMD);
  482. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  483. IWL_CMD(RADAR_NOTIFICATION);
  484. IWL_CMD(REPLY_QUIET_CMD);
  485. IWL_CMD(REPLY_CHANNEL_SWITCH);
  486. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  487. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  488. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  489. IWL_CMD(POWER_TABLE_CMD);
  490. IWL_CMD(PM_SLEEP_NOTIFICATION);
  491. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  492. IWL_CMD(REPLY_SCAN_CMD);
  493. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  494. IWL_CMD(SCAN_START_NOTIFICATION);
  495. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  496. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  497. IWL_CMD(BEACON_NOTIFICATION);
  498. IWL_CMD(REPLY_TX_BEACON);
  499. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  500. IWL_CMD(QUIET_NOTIFICATION);
  501. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  502. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  503. IWL_CMD(REPLY_BT_CONFIG);
  504. IWL_CMD(REPLY_STATISTICS_CMD);
  505. IWL_CMD(STATISTICS_NOTIFICATION);
  506. IWL_CMD(REPLY_CARD_STATE_CMD);
  507. IWL_CMD(CARD_STATE_NOTIFICATION);
  508. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  509. default:
  510. return "UNKNOWN";
  511. }
  512. }
  513. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  514. /**
  515. * iwl3945_enqueue_hcmd - enqueue a uCode command
  516. * @priv: device private data point
  517. * @cmd: a point to the ucode command structure
  518. *
  519. * The function returns < 0 values to indicate the operation is
  520. * failed. On success, it turns the index (> 0) of command in the
  521. * command queue.
  522. */
  523. static int iwl3945_enqueue_hcmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
  524. {
  525. struct iwl3945_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  526. struct iwl3945_queue *q = &txq->q;
  527. struct iwl3945_tfd_frame *tfd;
  528. u32 *control_flags;
  529. struct iwl3945_cmd *out_cmd;
  530. u32 idx;
  531. u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  532. dma_addr_t phys_addr;
  533. int pad;
  534. u16 count;
  535. int ret;
  536. unsigned long flags;
  537. /* If any of the command structures end up being larger than
  538. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  539. * we will need to increase the size of the TFD entries */
  540. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  541. !(cmd->meta.flags & CMD_SIZE_HUGE));
  542. if (iwl3945_is_rfkill(priv)) {
  543. IWL_DEBUG_INFO("Not sending command - RF KILL");
  544. return -EIO;
  545. }
  546. if (iwl3945_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  547. IWL_ERROR("No space for Tx\n");
  548. return -ENOSPC;
  549. }
  550. spin_lock_irqsave(&priv->hcmd_lock, flags);
  551. tfd = &txq->bd[q->write_ptr];
  552. memset(tfd, 0, sizeof(*tfd));
  553. control_flags = (u32 *) tfd;
  554. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  555. out_cmd = &txq->cmd[idx];
  556. out_cmd->hdr.cmd = cmd->id;
  557. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  558. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  559. /* At this point, the out_cmd now has all of the incoming cmd
  560. * information */
  561. out_cmd->hdr.flags = 0;
  562. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  563. INDEX_TO_SEQ(q->write_ptr));
  564. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  565. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  566. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  567. offsetof(struct iwl3945_cmd, hdr);
  568. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  569. pad = U32_PAD(cmd->len);
  570. count = TFD_CTL_COUNT_GET(*control_flags);
  571. *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
  572. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  573. "%d bytes at %d[%d]:%d\n",
  574. get_cmd_string(out_cmd->hdr.cmd),
  575. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  576. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  577. txq->need_update = 1;
  578. /* Increment and update queue's write index */
  579. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  580. ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
  581. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  582. return ret ? ret : idx;
  583. }
  584. static int iwl3945_send_cmd_async(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
  585. {
  586. int ret;
  587. BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
  588. /* An asynchronous command can not expect an SKB to be set. */
  589. BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
  590. /* An asynchronous command MUST have a callback. */
  591. BUG_ON(!cmd->meta.u.callback);
  592. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  593. return -EBUSY;
  594. ret = iwl3945_enqueue_hcmd(priv, cmd);
  595. if (ret < 0) {
  596. IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
  597. get_cmd_string(cmd->id), ret);
  598. return ret;
  599. }
  600. return 0;
  601. }
  602. static int iwl3945_send_cmd_sync(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
  603. {
  604. int cmd_idx;
  605. int ret;
  606. BUG_ON(cmd->meta.flags & CMD_ASYNC);
  607. /* A synchronous command can not have a callback set. */
  608. BUG_ON(cmd->meta.u.callback != NULL);
  609. if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
  610. IWL_ERROR("Error sending %s: Already sending a host command\n",
  611. get_cmd_string(cmd->id));
  612. ret = -EBUSY;
  613. goto out;
  614. }
  615. set_bit(STATUS_HCMD_ACTIVE, &priv->status);
  616. if (cmd->meta.flags & CMD_WANT_SKB)
  617. cmd->meta.source = &cmd->meta;
  618. cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
  619. if (cmd_idx < 0) {
  620. ret = cmd_idx;
  621. IWL_ERROR("Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
  622. get_cmd_string(cmd->id), ret);
  623. goto out;
  624. }
  625. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  626. !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
  627. HOST_COMPLETE_TIMEOUT);
  628. if (!ret) {
  629. if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
  630. IWL_ERROR("Error sending %s: time out after %dms.\n",
  631. get_cmd_string(cmd->id),
  632. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  633. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  634. ret = -ETIMEDOUT;
  635. goto cancel;
  636. }
  637. }
  638. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  639. IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
  640. get_cmd_string(cmd->id));
  641. ret = -ECANCELED;
  642. goto fail;
  643. }
  644. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  645. IWL_DEBUG_INFO("Command %s failed: FW Error\n",
  646. get_cmd_string(cmd->id));
  647. ret = -EIO;
  648. goto fail;
  649. }
  650. if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
  651. IWL_ERROR("Error: Response NULL in '%s'\n",
  652. get_cmd_string(cmd->id));
  653. ret = -EIO;
  654. goto out;
  655. }
  656. ret = 0;
  657. goto out;
  658. cancel:
  659. if (cmd->meta.flags & CMD_WANT_SKB) {
  660. struct iwl3945_cmd *qcmd;
  661. /* Cancel the CMD_WANT_SKB flag for the cmd in the
  662. * TX cmd queue. Otherwise in case the cmd comes
  663. * in later, it will possibly set an invalid
  664. * address (cmd->meta.source). */
  665. qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
  666. qcmd->meta.flags &= ~CMD_WANT_SKB;
  667. }
  668. fail:
  669. if (cmd->meta.u.skb) {
  670. dev_kfree_skb_any(cmd->meta.u.skb);
  671. cmd->meta.u.skb = NULL;
  672. }
  673. out:
  674. clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
  675. return ret;
  676. }
  677. int iwl3945_send_cmd(struct iwl3945_priv *priv, struct iwl3945_host_cmd *cmd)
  678. {
  679. if (cmd->meta.flags & CMD_ASYNC)
  680. return iwl3945_send_cmd_async(priv, cmd);
  681. return iwl3945_send_cmd_sync(priv, cmd);
  682. }
  683. int iwl3945_send_cmd_pdu(struct iwl3945_priv *priv, u8 id, u16 len, const void *data)
  684. {
  685. struct iwl3945_host_cmd cmd = {
  686. .id = id,
  687. .len = len,
  688. .data = data,
  689. };
  690. return iwl3945_send_cmd_sync(priv, &cmd);
  691. }
  692. static int __must_check iwl3945_send_cmd_u32(struct iwl3945_priv *priv, u8 id, u32 val)
  693. {
  694. struct iwl3945_host_cmd cmd = {
  695. .id = id,
  696. .len = sizeof(val),
  697. .data = &val,
  698. };
  699. return iwl3945_send_cmd_sync(priv, &cmd);
  700. }
  701. int iwl3945_send_statistics_request(struct iwl3945_priv *priv)
  702. {
  703. return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
  704. }
  705. /**
  706. * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
  707. * @band: 2.4 or 5 GHz band
  708. * @channel: Any channel valid for the requested band
  709. * In addition to setting the staging RXON, priv->band is also set.
  710. *
  711. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  712. * in the staging RXON flag structure based on the band
  713. */
  714. static int iwl3945_set_rxon_channel(struct iwl3945_priv *priv,
  715. enum ieee80211_band band,
  716. u16 channel)
  717. {
  718. if (!iwl3945_get_channel_info(priv, band, channel)) {
  719. IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
  720. channel, band);
  721. return -EINVAL;
  722. }
  723. if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
  724. (priv->band == band))
  725. return 0;
  726. priv->staging_rxon.channel = cpu_to_le16(channel);
  727. if (band == IEEE80211_BAND_5GHZ)
  728. priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
  729. else
  730. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  731. priv->band = band;
  732. IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
  733. return 0;
  734. }
  735. /**
  736. * iwl3945_check_rxon_cmd - validate RXON structure is valid
  737. *
  738. * NOTE: This is really only useful during development and can eventually
  739. * be #ifdef'd out once the driver is stable and folks aren't actively
  740. * making changes
  741. */
  742. static int iwl3945_check_rxon_cmd(struct iwl3945_rxon_cmd *rxon)
  743. {
  744. int error = 0;
  745. int counter = 1;
  746. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  747. error |= le32_to_cpu(rxon->flags &
  748. (RXON_FLG_TGJ_NARROW_BAND_MSK |
  749. RXON_FLG_RADAR_DETECT_MSK));
  750. if (error)
  751. IWL_WARNING("check 24G fields %d | %d\n",
  752. counter++, error);
  753. } else {
  754. error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
  755. 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
  756. if (error)
  757. IWL_WARNING("check 52 fields %d | %d\n",
  758. counter++, error);
  759. error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
  760. if (error)
  761. IWL_WARNING("check 52 CCK %d | %d\n",
  762. counter++, error);
  763. }
  764. error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
  765. if (error)
  766. IWL_WARNING("check mac addr %d | %d\n", counter++, error);
  767. /* make sure basic rates 6Mbps and 1Mbps are supported */
  768. error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
  769. ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
  770. if (error)
  771. IWL_WARNING("check basic rate %d | %d\n", counter++, error);
  772. error |= (le16_to_cpu(rxon->assoc_id) > 2007);
  773. if (error)
  774. IWL_WARNING("check assoc id %d | %d\n", counter++, error);
  775. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  776. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
  777. if (error)
  778. IWL_WARNING("check CCK and short slot %d | %d\n",
  779. counter++, error);
  780. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  781. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
  782. if (error)
  783. IWL_WARNING("check CCK & auto detect %d | %d\n",
  784. counter++, error);
  785. error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  786. RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
  787. if (error)
  788. IWL_WARNING("check TGG and auto detect %d | %d\n",
  789. counter++, error);
  790. if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
  791. error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
  792. RXON_FLG_ANT_A_MSK)) == 0);
  793. if (error)
  794. IWL_WARNING("check antenna %d %d\n", counter++, error);
  795. if (error)
  796. IWL_WARNING("Tuning to channel %d\n",
  797. le16_to_cpu(rxon->channel));
  798. if (error) {
  799. IWL_ERROR("Not a valid iwl3945_rxon_assoc_cmd field values\n");
  800. return -1;
  801. }
  802. return 0;
  803. }
  804. /**
  805. * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  806. * @priv: staging_rxon is compared to active_rxon
  807. *
  808. * If the RXON structure is changing enough to require a new tune,
  809. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  810. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  811. */
  812. static int iwl3945_full_rxon_required(struct iwl3945_priv *priv)
  813. {
  814. /* These items are only settable from the full RXON command */
  815. if (!(iwl3945_is_associated(priv)) ||
  816. compare_ether_addr(priv->staging_rxon.bssid_addr,
  817. priv->active_rxon.bssid_addr) ||
  818. compare_ether_addr(priv->staging_rxon.node_addr,
  819. priv->active_rxon.node_addr) ||
  820. compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
  821. priv->active_rxon.wlap_bssid_addr) ||
  822. (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
  823. (priv->staging_rxon.channel != priv->active_rxon.channel) ||
  824. (priv->staging_rxon.air_propagation !=
  825. priv->active_rxon.air_propagation) ||
  826. (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
  827. return 1;
  828. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  829. * be updated with the RXON_ASSOC command -- however only some
  830. * flag transitions are allowed using RXON_ASSOC */
  831. /* Check if we are not switching bands */
  832. if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
  833. (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
  834. return 1;
  835. /* Check if we are switching association toggle */
  836. if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
  837. (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
  838. return 1;
  839. return 0;
  840. }
  841. static int iwl3945_send_rxon_assoc(struct iwl3945_priv *priv)
  842. {
  843. int rc = 0;
  844. struct iwl3945_rx_packet *res = NULL;
  845. struct iwl3945_rxon_assoc_cmd rxon_assoc;
  846. struct iwl3945_host_cmd cmd = {
  847. .id = REPLY_RXON_ASSOC,
  848. .len = sizeof(rxon_assoc),
  849. .meta.flags = CMD_WANT_SKB,
  850. .data = &rxon_assoc,
  851. };
  852. const struct iwl3945_rxon_cmd *rxon1 = &priv->staging_rxon;
  853. const struct iwl3945_rxon_cmd *rxon2 = &priv->active_rxon;
  854. if ((rxon1->flags == rxon2->flags) &&
  855. (rxon1->filter_flags == rxon2->filter_flags) &&
  856. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  857. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  858. IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
  859. return 0;
  860. }
  861. rxon_assoc.flags = priv->staging_rxon.flags;
  862. rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
  863. rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
  864. rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
  865. rxon_assoc.reserved = 0;
  866. rc = iwl3945_send_cmd_sync(priv, &cmd);
  867. if (rc)
  868. return rc;
  869. res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
  870. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  871. IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
  872. rc = -EIO;
  873. }
  874. priv->alloc_rxb_skb--;
  875. dev_kfree_skb_any(cmd.meta.u.skb);
  876. return rc;
  877. }
  878. /**
  879. * iwl3945_commit_rxon - commit staging_rxon to hardware
  880. *
  881. * The RXON command in staging_rxon is committed to the hardware and
  882. * the active_rxon structure is updated with the new data. This
  883. * function correctly transitions out of the RXON_ASSOC_MSK state if
  884. * a HW tune is required based on the RXON structure changes.
  885. */
  886. static int iwl3945_commit_rxon(struct iwl3945_priv *priv)
  887. {
  888. /* cast away the const for active_rxon in this function */
  889. struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
  890. int rc = 0;
  891. DECLARE_MAC_BUF(mac);
  892. if (!iwl3945_is_alive(priv))
  893. return -1;
  894. /* always get timestamp with Rx frame */
  895. priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
  896. /* select antenna */
  897. priv->staging_rxon.flags &=
  898. ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
  899. priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
  900. rc = iwl3945_check_rxon_cmd(&priv->staging_rxon);
  901. if (rc) {
  902. IWL_ERROR("Invalid RXON configuration. Not committing.\n");
  903. return -EINVAL;
  904. }
  905. /* If we don't need to send a full RXON, we can use
  906. * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
  907. * and other flags for the current radio configuration. */
  908. if (!iwl3945_full_rxon_required(priv)) {
  909. rc = iwl3945_send_rxon_assoc(priv);
  910. if (rc) {
  911. IWL_ERROR("Error setting RXON_ASSOC "
  912. "configuration (%d).\n", rc);
  913. return rc;
  914. }
  915. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  916. return 0;
  917. }
  918. /* If we are currently associated and the new config requires
  919. * an RXON_ASSOC and the new config wants the associated mask enabled,
  920. * we must clear the associated from the active configuration
  921. * before we apply the new config */
  922. if (iwl3945_is_associated(priv) &&
  923. (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
  924. IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
  925. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  926. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
  927. sizeof(struct iwl3945_rxon_cmd),
  928. &priv->active_rxon);
  929. /* If the mask clearing failed then we set
  930. * active_rxon back to what it was previously */
  931. if (rc) {
  932. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  933. IWL_ERROR("Error clearing ASSOC_MSK on current "
  934. "configuration (%d).\n", rc);
  935. return rc;
  936. }
  937. }
  938. IWL_DEBUG_INFO("Sending RXON\n"
  939. "* with%s RXON_FILTER_ASSOC_MSK\n"
  940. "* channel = %d\n"
  941. "* bssid = %s\n",
  942. ((priv->staging_rxon.filter_flags &
  943. RXON_FILTER_ASSOC_MSK) ? "" : "out"),
  944. le16_to_cpu(priv->staging_rxon.channel),
  945. print_mac(mac, priv->staging_rxon.bssid_addr));
  946. /* Apply the new configuration */
  947. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
  948. sizeof(struct iwl3945_rxon_cmd), &priv->staging_rxon);
  949. if (rc) {
  950. IWL_ERROR("Error setting new configuration (%d).\n", rc);
  951. return rc;
  952. }
  953. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  954. iwl3945_clear_stations_table(priv);
  955. /* If we issue a new RXON command which required a tune then we must
  956. * send a new TXPOWER command or we won't be able to Tx any frames */
  957. rc = iwl3945_hw_reg_send_txpower(priv);
  958. if (rc) {
  959. IWL_ERROR("Error setting Tx power (%d).\n", rc);
  960. return rc;
  961. }
  962. /* Add the broadcast address so we can send broadcast frames */
  963. if (iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0) ==
  964. IWL_INVALID_STATION) {
  965. IWL_ERROR("Error adding BROADCAST address for transmit.\n");
  966. return -EIO;
  967. }
  968. /* If we have set the ASSOC_MSK and we are in BSS mode then
  969. * add the IWL_AP_ID to the station rate table */
  970. if (iwl3945_is_associated(priv) &&
  971. (priv->iw_mode == IEEE80211_IF_TYPE_STA))
  972. if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
  973. == IWL_INVALID_STATION) {
  974. IWL_ERROR("Error adding AP address for transmit.\n");
  975. return -EIO;
  976. }
  977. /* Init the hardware's rate fallback order based on the band */
  978. rc = iwl3945_init_hw_rate_table(priv);
  979. if (rc) {
  980. IWL_ERROR("Error setting HW rate table: %02X\n", rc);
  981. return -EIO;
  982. }
  983. return 0;
  984. }
  985. static int iwl3945_send_bt_config(struct iwl3945_priv *priv)
  986. {
  987. struct iwl3945_bt_cmd bt_cmd = {
  988. .flags = 3,
  989. .lead_time = 0xAA,
  990. .max_kill = 1,
  991. .kill_ack_mask = 0,
  992. .kill_cts_mask = 0,
  993. };
  994. return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  995. sizeof(struct iwl3945_bt_cmd), &bt_cmd);
  996. }
  997. static int iwl3945_send_scan_abort(struct iwl3945_priv *priv)
  998. {
  999. int rc = 0;
  1000. struct iwl3945_rx_packet *res;
  1001. struct iwl3945_host_cmd cmd = {
  1002. .id = REPLY_SCAN_ABORT_CMD,
  1003. .meta.flags = CMD_WANT_SKB,
  1004. };
  1005. /* If there isn't a scan actively going on in the hardware
  1006. * then we are in between scan bands and not actually
  1007. * actively scanning, so don't send the abort command */
  1008. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1009. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1010. return 0;
  1011. }
  1012. rc = iwl3945_send_cmd_sync(priv, &cmd);
  1013. if (rc) {
  1014. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1015. return rc;
  1016. }
  1017. res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
  1018. if (res->u.status != CAN_ABORT_STATUS) {
  1019. /* The scan abort will return 1 for success or
  1020. * 2 for "failure". A failure condition can be
  1021. * due to simply not being in an active scan which
  1022. * can occur if we send the scan abort before we
  1023. * the microcode has notified us that a scan is
  1024. * completed. */
  1025. IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
  1026. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1027. clear_bit(STATUS_SCAN_HW, &priv->status);
  1028. }
  1029. dev_kfree_skb_any(cmd.meta.u.skb);
  1030. return rc;
  1031. }
  1032. static int iwl3945_card_state_sync_callback(struct iwl3945_priv *priv,
  1033. struct iwl3945_cmd *cmd,
  1034. struct sk_buff *skb)
  1035. {
  1036. return 1;
  1037. }
  1038. /*
  1039. * CARD_STATE_CMD
  1040. *
  1041. * Use: Sets the device's internal card state to enable, disable, or halt
  1042. *
  1043. * When in the 'enable' state the card operates as normal.
  1044. * When in the 'disable' state, the card enters into a low power mode.
  1045. * When in the 'halt' state, the card is shut down and must be fully
  1046. * restarted to come back on.
  1047. */
  1048. static int iwl3945_send_card_state(struct iwl3945_priv *priv, u32 flags, u8 meta_flag)
  1049. {
  1050. struct iwl3945_host_cmd cmd = {
  1051. .id = REPLY_CARD_STATE_CMD,
  1052. .len = sizeof(u32),
  1053. .data = &flags,
  1054. .meta.flags = meta_flag,
  1055. };
  1056. if (meta_flag & CMD_ASYNC)
  1057. cmd.meta.u.callback = iwl3945_card_state_sync_callback;
  1058. return iwl3945_send_cmd(priv, &cmd);
  1059. }
  1060. static int iwl3945_add_sta_sync_callback(struct iwl3945_priv *priv,
  1061. struct iwl3945_cmd *cmd, struct sk_buff *skb)
  1062. {
  1063. struct iwl3945_rx_packet *res = NULL;
  1064. if (!skb) {
  1065. IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
  1066. return 1;
  1067. }
  1068. res = (struct iwl3945_rx_packet *)skb->data;
  1069. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1070. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  1071. res->hdr.flags);
  1072. return 1;
  1073. }
  1074. switch (res->u.add_sta.status) {
  1075. case ADD_STA_SUCCESS_MSK:
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. /* We didn't cache the SKB; let the caller free it */
  1081. return 1;
  1082. }
  1083. int iwl3945_send_add_station(struct iwl3945_priv *priv,
  1084. struct iwl3945_addsta_cmd *sta, u8 flags)
  1085. {
  1086. struct iwl3945_rx_packet *res = NULL;
  1087. int rc = 0;
  1088. struct iwl3945_host_cmd cmd = {
  1089. .id = REPLY_ADD_STA,
  1090. .len = sizeof(struct iwl3945_addsta_cmd),
  1091. .meta.flags = flags,
  1092. .data = sta,
  1093. };
  1094. if (flags & CMD_ASYNC)
  1095. cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
  1096. else
  1097. cmd.meta.flags |= CMD_WANT_SKB;
  1098. rc = iwl3945_send_cmd(priv, &cmd);
  1099. if (rc || (flags & CMD_ASYNC))
  1100. return rc;
  1101. res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
  1102. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1103. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  1104. res->hdr.flags);
  1105. rc = -EIO;
  1106. }
  1107. if (rc == 0) {
  1108. switch (res->u.add_sta.status) {
  1109. case ADD_STA_SUCCESS_MSK:
  1110. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  1111. break;
  1112. default:
  1113. rc = -EIO;
  1114. IWL_WARNING("REPLY_ADD_STA failed\n");
  1115. break;
  1116. }
  1117. }
  1118. priv->alloc_rxb_skb--;
  1119. dev_kfree_skb_any(cmd.meta.u.skb);
  1120. return rc;
  1121. }
  1122. static int iwl3945_update_sta_key_info(struct iwl3945_priv *priv,
  1123. struct ieee80211_key_conf *keyconf,
  1124. u8 sta_id)
  1125. {
  1126. unsigned long flags;
  1127. __le16 key_flags = 0;
  1128. switch (keyconf->alg) {
  1129. case ALG_CCMP:
  1130. key_flags |= STA_KEY_FLG_CCMP;
  1131. key_flags |= cpu_to_le16(
  1132. keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1133. key_flags &= ~STA_KEY_FLG_INVALID;
  1134. break;
  1135. case ALG_TKIP:
  1136. case ALG_WEP:
  1137. default:
  1138. return -EINVAL;
  1139. }
  1140. spin_lock_irqsave(&priv->sta_lock, flags);
  1141. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  1142. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  1143. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  1144. keyconf->keylen);
  1145. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  1146. keyconf->keylen);
  1147. priv->stations[sta_id].sta.key.key_flags = key_flags;
  1148. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1149. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1150. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1151. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  1152. iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
  1153. return 0;
  1154. }
  1155. static int iwl3945_clear_sta_key_info(struct iwl3945_priv *priv, u8 sta_id)
  1156. {
  1157. unsigned long flags;
  1158. spin_lock_irqsave(&priv->sta_lock, flags);
  1159. memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  1160. memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl3945_keyinfo));
  1161. priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  1162. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1163. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1164. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1165. IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
  1166. iwl3945_send_add_station(priv, &priv->stations[sta_id].sta, 0);
  1167. return 0;
  1168. }
  1169. static void iwl3945_clear_free_frames(struct iwl3945_priv *priv)
  1170. {
  1171. struct list_head *element;
  1172. IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
  1173. priv->frames_count);
  1174. while (!list_empty(&priv->free_frames)) {
  1175. element = priv->free_frames.next;
  1176. list_del(element);
  1177. kfree(list_entry(element, struct iwl3945_frame, list));
  1178. priv->frames_count--;
  1179. }
  1180. if (priv->frames_count) {
  1181. IWL_WARNING("%d frames still in use. Did we lose one?\n",
  1182. priv->frames_count);
  1183. priv->frames_count = 0;
  1184. }
  1185. }
  1186. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl3945_priv *priv)
  1187. {
  1188. struct iwl3945_frame *frame;
  1189. struct list_head *element;
  1190. if (list_empty(&priv->free_frames)) {
  1191. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  1192. if (!frame) {
  1193. IWL_ERROR("Could not allocate frame!\n");
  1194. return NULL;
  1195. }
  1196. priv->frames_count++;
  1197. return frame;
  1198. }
  1199. element = priv->free_frames.next;
  1200. list_del(element);
  1201. return list_entry(element, struct iwl3945_frame, list);
  1202. }
  1203. static void iwl3945_free_frame(struct iwl3945_priv *priv, struct iwl3945_frame *frame)
  1204. {
  1205. memset(frame, 0, sizeof(*frame));
  1206. list_add(&frame->list, &priv->free_frames);
  1207. }
  1208. unsigned int iwl3945_fill_beacon_frame(struct iwl3945_priv *priv,
  1209. struct ieee80211_hdr *hdr,
  1210. const u8 *dest, int left)
  1211. {
  1212. if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
  1213. ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
  1214. (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
  1215. return 0;
  1216. if (priv->ibss_beacon->len > left)
  1217. return 0;
  1218. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  1219. return priv->ibss_beacon->len;
  1220. }
  1221. static u8 iwl3945_rate_get_lowest_plcp(int rate_mask)
  1222. {
  1223. u8 i;
  1224. for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
  1225. i = iwl3945_rates[i].next_ieee) {
  1226. if (rate_mask & (1 << i))
  1227. return iwl3945_rates[i].plcp;
  1228. }
  1229. return IWL_RATE_INVALID;
  1230. }
  1231. static int iwl3945_send_beacon_cmd(struct iwl3945_priv *priv)
  1232. {
  1233. struct iwl3945_frame *frame;
  1234. unsigned int frame_size;
  1235. int rc;
  1236. u8 rate;
  1237. frame = iwl3945_get_free_frame(priv);
  1238. if (!frame) {
  1239. IWL_ERROR("Could not obtain free frame buffer for beacon "
  1240. "command.\n");
  1241. return -ENOMEM;
  1242. }
  1243. if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
  1244. rate = iwl3945_rate_get_lowest_plcp(priv->active_rate_basic &
  1245. 0xFF0);
  1246. if (rate == IWL_INVALID_RATE)
  1247. rate = IWL_RATE_6M_PLCP;
  1248. } else {
  1249. rate = iwl3945_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
  1250. if (rate == IWL_INVALID_RATE)
  1251. rate = IWL_RATE_1M_PLCP;
  1252. }
  1253. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  1254. rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  1255. &frame->u.cmd[0]);
  1256. iwl3945_free_frame(priv, frame);
  1257. return rc;
  1258. }
  1259. /******************************************************************************
  1260. *
  1261. * EEPROM related functions
  1262. *
  1263. ******************************************************************************/
  1264. static void get_eeprom_mac(struct iwl3945_priv *priv, u8 *mac)
  1265. {
  1266. memcpy(mac, priv->eeprom.mac_address, 6);
  1267. }
  1268. /*
  1269. * Clear the OWNER_MSK, to establish driver (instead of uCode running on
  1270. * embedded controller) as EEPROM reader; each read is a series of pulses
  1271. * to/from the EEPROM chip, not a single event, so even reads could conflict
  1272. * if they weren't arbitrated by some ownership mechanism. Here, the driver
  1273. * simply claims ownership, which should be safe when this function is called
  1274. * (i.e. before loading uCode!).
  1275. */
  1276. static inline int iwl3945_eeprom_acquire_semaphore(struct iwl3945_priv *priv)
  1277. {
  1278. _iwl3945_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
  1279. return 0;
  1280. }
  1281. /**
  1282. * iwl3945_eeprom_init - read EEPROM contents
  1283. *
  1284. * Load the EEPROM contents from adapter into priv->eeprom
  1285. *
  1286. * NOTE: This routine uses the non-debug IO access functions.
  1287. */
  1288. int iwl3945_eeprom_init(struct iwl3945_priv *priv)
  1289. {
  1290. u16 *e = (u16 *)&priv->eeprom;
  1291. u32 gp = iwl3945_read32(priv, CSR_EEPROM_GP);
  1292. u32 r;
  1293. int sz = sizeof(priv->eeprom);
  1294. int rc;
  1295. int i;
  1296. u16 addr;
  1297. /* The EEPROM structure has several padding buffers within it
  1298. * and when adding new EEPROM maps is subject to programmer errors
  1299. * which may be very difficult to identify without explicitly
  1300. * checking the resulting size of the eeprom map. */
  1301. BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
  1302. if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
  1303. IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  1304. return -ENOENT;
  1305. }
  1306. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  1307. rc = iwl3945_eeprom_acquire_semaphore(priv);
  1308. if (rc < 0) {
  1309. IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
  1310. return -ENOENT;
  1311. }
  1312. /* eeprom is an array of 16bit values */
  1313. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  1314. _iwl3945_write32(priv, CSR_EEPROM_REG, addr << 1);
  1315. _iwl3945_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
  1316. for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
  1317. i += IWL_EEPROM_ACCESS_DELAY) {
  1318. r = _iwl3945_read_direct32(priv, CSR_EEPROM_REG);
  1319. if (r & CSR_EEPROM_REG_READ_VALID_MSK)
  1320. break;
  1321. udelay(IWL_EEPROM_ACCESS_DELAY);
  1322. }
  1323. if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
  1324. IWL_ERROR("Time out reading EEPROM[%d]\n", addr);
  1325. return -ETIMEDOUT;
  1326. }
  1327. e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
  1328. }
  1329. return 0;
  1330. }
  1331. static void iwl3945_unset_hw_setting(struct iwl3945_priv *priv)
  1332. {
  1333. if (priv->hw_setting.shared_virt)
  1334. pci_free_consistent(priv->pci_dev,
  1335. sizeof(struct iwl3945_shared),
  1336. priv->hw_setting.shared_virt,
  1337. priv->hw_setting.shared_phys);
  1338. }
  1339. /**
  1340. * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
  1341. *
  1342. * return : set the bit for each supported rate insert in ie
  1343. */
  1344. static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
  1345. u16 basic_rate, int *left)
  1346. {
  1347. u16 ret_rates = 0, bit;
  1348. int i;
  1349. u8 *cnt = ie;
  1350. u8 *rates = ie + 1;
  1351. for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
  1352. if (bit & supported_rate) {
  1353. ret_rates |= bit;
  1354. rates[*cnt] = iwl3945_rates[i].ieee |
  1355. ((bit & basic_rate) ? 0x80 : 0x00);
  1356. (*cnt)++;
  1357. (*left)--;
  1358. if ((*left <= 0) ||
  1359. (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
  1360. break;
  1361. }
  1362. }
  1363. return ret_rates;
  1364. }
  1365. /**
  1366. * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
  1367. */
  1368. static u16 iwl3945_fill_probe_req(struct iwl3945_priv *priv,
  1369. struct ieee80211_mgmt *frame,
  1370. int left, int is_direct)
  1371. {
  1372. int len = 0;
  1373. u8 *pos = NULL;
  1374. u16 active_rates, ret_rates, cck_rates;
  1375. /* Make sure there is enough space for the probe request,
  1376. * two mandatory IEs and the data */
  1377. left -= 24;
  1378. if (left < 0)
  1379. return 0;
  1380. len += 24;
  1381. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1382. memcpy(frame->da, iwl3945_broadcast_addr, ETH_ALEN);
  1383. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  1384. memcpy(frame->bssid, iwl3945_broadcast_addr, ETH_ALEN);
  1385. frame->seq_ctrl = 0;
  1386. /* fill in our indirect SSID IE */
  1387. /* ...next IE... */
  1388. left -= 2;
  1389. if (left < 0)
  1390. return 0;
  1391. len += 2;
  1392. pos = &(frame->u.probe_req.variable[0]);
  1393. *pos++ = WLAN_EID_SSID;
  1394. *pos++ = 0;
  1395. /* fill in our direct SSID IE... */
  1396. if (is_direct) {
  1397. /* ...next IE... */
  1398. left -= 2 + priv->essid_len;
  1399. if (left < 0)
  1400. return 0;
  1401. /* ... fill it in... */
  1402. *pos++ = WLAN_EID_SSID;
  1403. *pos++ = priv->essid_len;
  1404. memcpy(pos, priv->essid, priv->essid_len);
  1405. pos += priv->essid_len;
  1406. len += 2 + priv->essid_len;
  1407. }
  1408. /* fill in supported rate */
  1409. /* ...next IE... */
  1410. left -= 2;
  1411. if (left < 0)
  1412. return 0;
  1413. /* ... fill it in... */
  1414. *pos++ = WLAN_EID_SUPP_RATES;
  1415. *pos = 0;
  1416. priv->active_rate = priv->rates_mask;
  1417. active_rates = priv->active_rate;
  1418. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  1419. cck_rates = IWL_CCK_RATES_MASK & active_rates;
  1420. ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
  1421. priv->active_rate_basic, &left);
  1422. active_rates &= ~ret_rates;
  1423. ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
  1424. priv->active_rate_basic, &left);
  1425. active_rates &= ~ret_rates;
  1426. len += 2 + *pos;
  1427. pos += (*pos) + 1;
  1428. if (active_rates == 0)
  1429. goto fill_end;
  1430. /* fill in supported extended rate */
  1431. /* ...next IE... */
  1432. left -= 2;
  1433. if (left < 0)
  1434. return 0;
  1435. /* ... fill it in... */
  1436. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1437. *pos = 0;
  1438. iwl3945_supported_rate_to_ie(pos, active_rates,
  1439. priv->active_rate_basic, &left);
  1440. if (*pos > 0)
  1441. len += 2 + *pos;
  1442. fill_end:
  1443. return (u16)len;
  1444. }
  1445. /*
  1446. * QoS support
  1447. */
  1448. static int iwl3945_send_qos_params_command(struct iwl3945_priv *priv,
  1449. struct iwl3945_qosparam_cmd *qos)
  1450. {
  1451. return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
  1452. sizeof(struct iwl3945_qosparam_cmd), qos);
  1453. }
  1454. static void iwl3945_reset_qos(struct iwl3945_priv *priv)
  1455. {
  1456. u16 cw_min = 15;
  1457. u16 cw_max = 1023;
  1458. u8 aifs = 2;
  1459. u8 is_legacy = 0;
  1460. unsigned long flags;
  1461. int i;
  1462. spin_lock_irqsave(&priv->lock, flags);
  1463. priv->qos_data.qos_active = 0;
  1464. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
  1465. if (priv->qos_data.qos_enable)
  1466. priv->qos_data.qos_active = 1;
  1467. if (!(priv->active_rate & 0xfff0)) {
  1468. cw_min = 31;
  1469. is_legacy = 1;
  1470. }
  1471. } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1472. if (priv->qos_data.qos_enable)
  1473. priv->qos_data.qos_active = 1;
  1474. } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
  1475. cw_min = 31;
  1476. is_legacy = 1;
  1477. }
  1478. if (priv->qos_data.qos_active)
  1479. aifs = 3;
  1480. priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
  1481. priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
  1482. priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
  1483. priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
  1484. priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
  1485. if (priv->qos_data.qos_active) {
  1486. i = 1;
  1487. priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
  1488. priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
  1489. priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
  1490. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  1491. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1492. i = 2;
  1493. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1494. cpu_to_le16((cw_min + 1) / 2 - 1);
  1495. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1496. cpu_to_le16(cw_max);
  1497. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  1498. if (is_legacy)
  1499. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1500. cpu_to_le16(6016);
  1501. else
  1502. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1503. cpu_to_le16(3008);
  1504. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1505. i = 3;
  1506. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1507. cpu_to_le16((cw_min + 1) / 4 - 1);
  1508. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1509. cpu_to_le16((cw_max + 1) / 2 - 1);
  1510. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  1511. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1512. if (is_legacy)
  1513. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1514. cpu_to_le16(3264);
  1515. else
  1516. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1517. cpu_to_le16(1504);
  1518. } else {
  1519. for (i = 1; i < 4; i++) {
  1520. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1521. cpu_to_le16(cw_min);
  1522. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1523. cpu_to_le16(cw_max);
  1524. priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
  1525. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  1526. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1527. }
  1528. }
  1529. IWL_DEBUG_QOS("set QoS to default \n");
  1530. spin_unlock_irqrestore(&priv->lock, flags);
  1531. }
  1532. static void iwl3945_activate_qos(struct iwl3945_priv *priv, u8 force)
  1533. {
  1534. unsigned long flags;
  1535. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1536. return;
  1537. if (!priv->qos_data.qos_enable)
  1538. return;
  1539. spin_lock_irqsave(&priv->lock, flags);
  1540. priv->qos_data.def_qos_parm.qos_flags = 0;
  1541. if (priv->qos_data.qos_cap.q_AP.queue_request &&
  1542. !priv->qos_data.qos_cap.q_AP.txop_request)
  1543. priv->qos_data.def_qos_parm.qos_flags |=
  1544. QOS_PARAM_FLG_TXOP_TYPE_MSK;
  1545. if (priv->qos_data.qos_active)
  1546. priv->qos_data.def_qos_parm.qos_flags |=
  1547. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  1548. spin_unlock_irqrestore(&priv->lock, flags);
  1549. if (force || iwl3945_is_associated(priv)) {
  1550. IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
  1551. priv->qos_data.qos_active);
  1552. iwl3945_send_qos_params_command(priv,
  1553. &(priv->qos_data.def_qos_parm));
  1554. }
  1555. }
  1556. /*
  1557. * Power management (not Tx power!) functions
  1558. */
  1559. #define MSEC_TO_USEC 1024
  1560. #define NOSLP __constant_cpu_to_le32(0)
  1561. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
  1562. #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
  1563. #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
  1564. __constant_cpu_to_le32(X1), \
  1565. __constant_cpu_to_le32(X2), \
  1566. __constant_cpu_to_le32(X3), \
  1567. __constant_cpu_to_le32(X4)}
  1568. /* default power management (not Tx power) table values */
  1569. /* for tim 0-10 */
  1570. static struct iwl3945_power_vec_entry range_0[IWL_POWER_AC] = {
  1571. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1572. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  1573. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
  1574. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
  1575. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
  1576. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
  1577. };
  1578. /* for tim > 10 */
  1579. static struct iwl3945_power_vec_entry range_1[IWL_POWER_AC] = {
  1580. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1581. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
  1582. SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  1583. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
  1584. SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  1585. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
  1586. SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
  1587. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  1588. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
  1589. SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  1590. };
  1591. int iwl3945_power_init_handle(struct iwl3945_priv *priv)
  1592. {
  1593. int rc = 0, i;
  1594. struct iwl3945_power_mgr *pow_data;
  1595. int size = sizeof(struct iwl3945_power_vec_entry) * IWL_POWER_AC;
  1596. u16 pci_pm;
  1597. IWL_DEBUG_POWER("Initialize power \n");
  1598. pow_data = &(priv->power_data);
  1599. memset(pow_data, 0, sizeof(*pow_data));
  1600. pow_data->active_index = IWL_POWER_RANGE_0;
  1601. pow_data->dtim_val = 0xffff;
  1602. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  1603. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  1604. rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
  1605. if (rc != 0)
  1606. return 0;
  1607. else {
  1608. struct iwl3945_powertable_cmd *cmd;
  1609. IWL_DEBUG_POWER("adjust power command flags\n");
  1610. for (i = 0; i < IWL_POWER_AC; i++) {
  1611. cmd = &pow_data->pwr_range_0[i].cmd;
  1612. if (pci_pm & 0x1)
  1613. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  1614. else
  1615. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  1616. }
  1617. }
  1618. return rc;
  1619. }
  1620. static int iwl3945_update_power_cmd(struct iwl3945_priv *priv,
  1621. struct iwl3945_powertable_cmd *cmd, u32 mode)
  1622. {
  1623. int rc = 0, i;
  1624. u8 skip;
  1625. u32 max_sleep = 0;
  1626. struct iwl3945_power_vec_entry *range;
  1627. u8 period = 0;
  1628. struct iwl3945_power_mgr *pow_data;
  1629. if (mode > IWL_POWER_INDEX_5) {
  1630. IWL_DEBUG_POWER("Error invalid power mode \n");
  1631. return -1;
  1632. }
  1633. pow_data = &(priv->power_data);
  1634. if (pow_data->active_index == IWL_POWER_RANGE_0)
  1635. range = &pow_data->pwr_range_0[0];
  1636. else
  1637. range = &pow_data->pwr_range_1[1];
  1638. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
  1639. #ifdef IWL_MAC80211_DISABLE
  1640. if (priv->assoc_network != NULL) {
  1641. unsigned long flags;
  1642. period = priv->assoc_network->tim.tim_period;
  1643. }
  1644. #endif /*IWL_MAC80211_DISABLE */
  1645. skip = range[mode].no_dtim;
  1646. if (period == 0) {
  1647. period = 1;
  1648. skip = 0;
  1649. }
  1650. if (skip == 0) {
  1651. max_sleep = period;
  1652. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1653. } else {
  1654. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  1655. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  1656. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1657. }
  1658. for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
  1659. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  1660. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  1661. }
  1662. IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
  1663. IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  1664. IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  1665. IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  1666. le32_to_cpu(cmd->sleep_interval[0]),
  1667. le32_to_cpu(cmd->sleep_interval[1]),
  1668. le32_to_cpu(cmd->sleep_interval[2]),
  1669. le32_to_cpu(cmd->sleep_interval[3]),
  1670. le32_to_cpu(cmd->sleep_interval[4]));
  1671. return rc;
  1672. }
  1673. static int iwl3945_send_power_mode(struct iwl3945_priv *priv, u32 mode)
  1674. {
  1675. u32 uninitialized_var(final_mode);
  1676. int rc;
  1677. struct iwl3945_powertable_cmd cmd;
  1678. /* If on battery, set to 3,
  1679. * if plugged into AC power, set to CAM ("continuously aware mode"),
  1680. * else user level */
  1681. switch (mode) {
  1682. case IWL_POWER_BATTERY:
  1683. final_mode = IWL_POWER_INDEX_3;
  1684. break;
  1685. case IWL_POWER_AC:
  1686. final_mode = IWL_POWER_MODE_CAM;
  1687. break;
  1688. default:
  1689. final_mode = mode;
  1690. break;
  1691. }
  1692. iwl3945_update_power_cmd(priv, &cmd, final_mode);
  1693. rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
  1694. if (final_mode == IWL_POWER_MODE_CAM)
  1695. clear_bit(STATUS_POWER_PMI, &priv->status);
  1696. else
  1697. set_bit(STATUS_POWER_PMI, &priv->status);
  1698. return rc;
  1699. }
  1700. /**
  1701. * iwl3945_scan_cancel - Cancel any currently executing HW scan
  1702. *
  1703. * NOTE: priv->mutex is not required before calling this function
  1704. */
  1705. static int iwl3945_scan_cancel(struct iwl3945_priv *priv)
  1706. {
  1707. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1708. clear_bit(STATUS_SCANNING, &priv->status);
  1709. return 0;
  1710. }
  1711. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1712. if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1713. IWL_DEBUG_SCAN("Queuing scan abort.\n");
  1714. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  1715. queue_work(priv->workqueue, &priv->abort_scan);
  1716. } else
  1717. IWL_DEBUG_SCAN("Scan abort already in progress.\n");
  1718. return test_bit(STATUS_SCANNING, &priv->status);
  1719. }
  1720. return 0;
  1721. }
  1722. /**
  1723. * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
  1724. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1725. *
  1726. * NOTE: priv->mutex must be held before calling this function
  1727. */
  1728. static int iwl3945_scan_cancel_timeout(struct iwl3945_priv *priv, unsigned long ms)
  1729. {
  1730. unsigned long now = jiffies;
  1731. int ret;
  1732. ret = iwl3945_scan_cancel(priv);
  1733. if (ret && ms) {
  1734. mutex_unlock(&priv->mutex);
  1735. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  1736. test_bit(STATUS_SCANNING, &priv->status))
  1737. msleep(1);
  1738. mutex_lock(&priv->mutex);
  1739. return test_bit(STATUS_SCANNING, &priv->status);
  1740. }
  1741. return ret;
  1742. }
  1743. #define MAX_UCODE_BEACON_INTERVAL 1024
  1744. #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
  1745. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  1746. {
  1747. u16 new_val = 0;
  1748. u16 beacon_factor = 0;
  1749. beacon_factor =
  1750. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  1751. / MAX_UCODE_BEACON_INTERVAL;
  1752. new_val = beacon_val / beacon_factor;
  1753. return cpu_to_le16(new_val);
  1754. }
  1755. static void iwl3945_setup_rxon_timing(struct iwl3945_priv *priv)
  1756. {
  1757. u64 interval_tm_unit;
  1758. u64 tsf, result;
  1759. unsigned long flags;
  1760. struct ieee80211_conf *conf = NULL;
  1761. u16 beacon_int = 0;
  1762. conf = ieee80211_get_hw_conf(priv->hw);
  1763. spin_lock_irqsave(&priv->lock, flags);
  1764. priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
  1765. priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
  1766. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  1767. tsf = priv->timestamp1;
  1768. tsf = ((tsf << 32) | priv->timestamp0);
  1769. beacon_int = priv->beacon_int;
  1770. spin_unlock_irqrestore(&priv->lock, flags);
  1771. if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
  1772. if (beacon_int == 0) {
  1773. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  1774. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  1775. } else {
  1776. priv->rxon_timing.beacon_interval =
  1777. cpu_to_le16(beacon_int);
  1778. priv->rxon_timing.beacon_interval =
  1779. iwl3945_adjust_beacon_interval(
  1780. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1781. }
  1782. priv->rxon_timing.atim_window = 0;
  1783. } else {
  1784. priv->rxon_timing.beacon_interval =
  1785. iwl3945_adjust_beacon_interval(conf->beacon_int);
  1786. /* TODO: we need to get atim_window from upper stack
  1787. * for now we set to 0 */
  1788. priv->rxon_timing.atim_window = 0;
  1789. }
  1790. interval_tm_unit =
  1791. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  1792. result = do_div(tsf, interval_tm_unit);
  1793. priv->rxon_timing.beacon_init_val =
  1794. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  1795. IWL_DEBUG_ASSOC
  1796. ("beacon interval %d beacon timer %d beacon tim %d\n",
  1797. le16_to_cpu(priv->rxon_timing.beacon_interval),
  1798. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  1799. le16_to_cpu(priv->rxon_timing.atim_window));
  1800. }
  1801. static int iwl3945_scan_initiate(struct iwl3945_priv *priv)
  1802. {
  1803. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1804. IWL_ERROR("APs don't scan.\n");
  1805. return 0;
  1806. }
  1807. if (!iwl3945_is_ready_rf(priv)) {
  1808. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  1809. return -EIO;
  1810. }
  1811. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1812. IWL_DEBUG_SCAN("Scan already in progress.\n");
  1813. return -EAGAIN;
  1814. }
  1815. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1816. IWL_DEBUG_SCAN("Scan request while abort pending. "
  1817. "Queuing.\n");
  1818. return -EAGAIN;
  1819. }
  1820. IWL_DEBUG_INFO("Starting scan...\n");
  1821. if (priv->cfg->sku & IWL_SKU_G)
  1822. priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
  1823. if (priv->cfg->sku & IWL_SKU_A)
  1824. priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
  1825. set_bit(STATUS_SCANNING, &priv->status);
  1826. priv->scan_start = jiffies;
  1827. priv->scan_pass_start = priv->scan_start;
  1828. queue_work(priv->workqueue, &priv->request_scan);
  1829. return 0;
  1830. }
  1831. static int iwl3945_set_rxon_hwcrypto(struct iwl3945_priv *priv, int hw_decrypt)
  1832. {
  1833. struct iwl3945_rxon_cmd *rxon = &priv->staging_rxon;
  1834. if (hw_decrypt)
  1835. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  1836. else
  1837. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  1838. return 0;
  1839. }
  1840. static void iwl3945_set_flags_for_phymode(struct iwl3945_priv *priv,
  1841. enum ieee80211_band band)
  1842. {
  1843. if (band == IEEE80211_BAND_5GHZ) {
  1844. priv->staging_rxon.flags &=
  1845. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  1846. | RXON_FLG_CCK_MSK);
  1847. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1848. } else {
  1849. /* Copied from iwl3945_bg_post_associate() */
  1850. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  1851. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1852. else
  1853. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1854. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  1855. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1856. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  1857. priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
  1858. priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
  1859. }
  1860. }
  1861. /*
  1862. * initialize rxon structure with default values from eeprom
  1863. */
  1864. static void iwl3945_connection_init_rx_config(struct iwl3945_priv *priv)
  1865. {
  1866. const struct iwl3945_channel_info *ch_info;
  1867. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  1868. switch (priv->iw_mode) {
  1869. case IEEE80211_IF_TYPE_AP:
  1870. priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
  1871. break;
  1872. case IEEE80211_IF_TYPE_STA:
  1873. priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
  1874. priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  1875. break;
  1876. case IEEE80211_IF_TYPE_IBSS:
  1877. priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
  1878. priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  1879. priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  1880. RXON_FILTER_ACCEPT_GRP_MSK;
  1881. break;
  1882. case IEEE80211_IF_TYPE_MNTR:
  1883. priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
  1884. priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
  1885. RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  1886. break;
  1887. default:
  1888. IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
  1889. break;
  1890. }
  1891. #if 0
  1892. /* TODO: Figure out when short_preamble would be set and cache from
  1893. * that */
  1894. if (!hw_to_local(priv->hw)->short_preamble)
  1895. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1896. else
  1897. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1898. #endif
  1899. ch_info = iwl3945_get_channel_info(priv, priv->band,
  1900. le16_to_cpu(priv->active_rxon.channel));
  1901. if (!ch_info)
  1902. ch_info = &priv->channel_info[0];
  1903. /*
  1904. * in some case A channels are all non IBSS
  1905. * in this case force B/G channel
  1906. */
  1907. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  1908. !(is_channel_ibss(ch_info)))
  1909. ch_info = &priv->channel_info[0];
  1910. priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
  1911. if (is_channel_a_band(ch_info))
  1912. priv->band = IEEE80211_BAND_5GHZ;
  1913. else
  1914. priv->band = IEEE80211_BAND_2GHZ;
  1915. iwl3945_set_flags_for_phymode(priv, priv->band);
  1916. priv->staging_rxon.ofdm_basic_rates =
  1917. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  1918. priv->staging_rxon.cck_basic_rates =
  1919. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  1920. }
  1921. static int iwl3945_set_mode(struct iwl3945_priv *priv, int mode)
  1922. {
  1923. if (mode == IEEE80211_IF_TYPE_IBSS) {
  1924. const struct iwl3945_channel_info *ch_info;
  1925. ch_info = iwl3945_get_channel_info(priv,
  1926. priv->band,
  1927. le16_to_cpu(priv->staging_rxon.channel));
  1928. if (!ch_info || !is_channel_ibss(ch_info)) {
  1929. IWL_ERROR("channel %d not IBSS channel\n",
  1930. le16_to_cpu(priv->staging_rxon.channel));
  1931. return -EINVAL;
  1932. }
  1933. }
  1934. priv->iw_mode = mode;
  1935. iwl3945_connection_init_rx_config(priv);
  1936. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  1937. iwl3945_clear_stations_table(priv);
  1938. /* dont commit rxon if rf-kill is on*/
  1939. if (!iwl3945_is_ready_rf(priv))
  1940. return -EAGAIN;
  1941. cancel_delayed_work(&priv->scan_check);
  1942. if (iwl3945_scan_cancel_timeout(priv, 100)) {
  1943. IWL_WARNING("Aborted scan still in progress after 100ms\n");
  1944. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  1945. return -EAGAIN;
  1946. }
  1947. iwl3945_commit_rxon(priv);
  1948. return 0;
  1949. }
  1950. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl3945_priv *priv,
  1951. struct ieee80211_tx_info *info,
  1952. struct iwl3945_cmd *cmd,
  1953. struct sk_buff *skb_frag,
  1954. int last_frag)
  1955. {
  1956. struct iwl3945_hw_key *keyinfo =
  1957. &priv->stations[info->control.hw_key->hw_key_idx].keyinfo;
  1958. switch (keyinfo->alg) {
  1959. case ALG_CCMP:
  1960. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
  1961. memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
  1962. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  1963. break;
  1964. case ALG_TKIP:
  1965. #if 0
  1966. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
  1967. if (last_frag)
  1968. memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
  1969. 8);
  1970. else
  1971. memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
  1972. #endif
  1973. break;
  1974. case ALG_WEP:
  1975. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
  1976. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  1977. if (keyinfo->keylen == 13)
  1978. cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
  1979. memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
  1980. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  1981. "with key %d\n", info->control.hw_key->hw_key_idx);
  1982. break;
  1983. default:
  1984. printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
  1985. break;
  1986. }
  1987. }
  1988. /*
  1989. * handle build REPLY_TX command notification.
  1990. */
  1991. static void iwl3945_build_tx_cmd_basic(struct iwl3945_priv *priv,
  1992. struct iwl3945_cmd *cmd,
  1993. struct ieee80211_tx_info *info,
  1994. struct ieee80211_hdr *hdr,
  1995. int is_unicast, u8 std_id)
  1996. {
  1997. __le16 fc = hdr->frame_control;
  1998. __le32 tx_flags = cmd->cmd.tx.tx_flags;
  1999. cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2000. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  2001. tx_flags |= TX_CMD_FLG_ACK_MSK;
  2002. if (ieee80211_is_mgmt(fc))
  2003. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2004. if (ieee80211_is_probe_resp(fc) &&
  2005. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  2006. tx_flags |= TX_CMD_FLG_TSF_MSK;
  2007. } else {
  2008. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  2009. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2010. }
  2011. cmd->cmd.tx.sta_id = std_id;
  2012. if (ieee80211_has_morefrags(fc))
  2013. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  2014. if (ieee80211_is_data_qos(fc)) {
  2015. u8 *qc = ieee80211_get_qos_ctl(hdr);
  2016. cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
  2017. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  2018. } else {
  2019. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2020. }
  2021. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  2022. tx_flags |= TX_CMD_FLG_RTS_MSK;
  2023. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  2024. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  2025. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  2026. tx_flags |= TX_CMD_FLG_CTS_MSK;
  2027. }
  2028. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  2029. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  2030. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  2031. if (ieee80211_is_mgmt(fc)) {
  2032. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  2033. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
  2034. else
  2035. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
  2036. } else {
  2037. cmd->cmd.tx.timeout.pm_frame_timeout = 0;
  2038. #ifdef CONFIG_IWL3945_LEDS
  2039. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  2040. #endif
  2041. }
  2042. cmd->cmd.tx.driver_txop = 0;
  2043. cmd->cmd.tx.tx_flags = tx_flags;
  2044. cmd->cmd.tx.next_frame_len = 0;
  2045. }
  2046. /**
  2047. * iwl3945_get_sta_id - Find station's index within station table
  2048. */
  2049. static int iwl3945_get_sta_id(struct iwl3945_priv *priv, struct ieee80211_hdr *hdr)
  2050. {
  2051. int sta_id;
  2052. u16 fc = le16_to_cpu(hdr->frame_control);
  2053. /* If this frame is broadcast or management, use broadcast station id */
  2054. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  2055. is_multicast_ether_addr(hdr->addr1))
  2056. return priv->hw_setting.bcast_sta_id;
  2057. switch (priv->iw_mode) {
  2058. /* If we are a client station in a BSS network, use the special
  2059. * AP station entry (that's the only station we communicate with) */
  2060. case IEEE80211_IF_TYPE_STA:
  2061. return IWL_AP_ID;
  2062. /* If we are an AP, then find the station, or use BCAST */
  2063. case IEEE80211_IF_TYPE_AP:
  2064. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  2065. if (sta_id != IWL_INVALID_STATION)
  2066. return sta_id;
  2067. return priv->hw_setting.bcast_sta_id;
  2068. /* If this frame is going out to an IBSS network, find the station,
  2069. * or create a new station table entry */
  2070. case IEEE80211_IF_TYPE_IBSS: {
  2071. DECLARE_MAC_BUF(mac);
  2072. /* Create new station table entry */
  2073. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  2074. if (sta_id != IWL_INVALID_STATION)
  2075. return sta_id;
  2076. sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  2077. if (sta_id != IWL_INVALID_STATION)
  2078. return sta_id;
  2079. IWL_DEBUG_DROP("Station %s not in station map. "
  2080. "Defaulting to broadcast...\n",
  2081. print_mac(mac, hdr->addr1));
  2082. iwl3945_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  2083. return priv->hw_setting.bcast_sta_id;
  2084. }
  2085. /* If we are in monitor mode, use BCAST. This is required for
  2086. * packet injection. */
  2087. case IEEE80211_IF_TYPE_MNTR:
  2088. return priv->hw_setting.bcast_sta_id;
  2089. default:
  2090. IWL_WARNING("Unknown mode of operation: %d\n", priv->iw_mode);
  2091. return priv->hw_setting.bcast_sta_id;
  2092. }
  2093. }
  2094. /*
  2095. * start REPLY_TX command process
  2096. */
  2097. static int iwl3945_tx_skb(struct iwl3945_priv *priv, struct sk_buff *skb)
  2098. {
  2099. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2100. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2101. struct iwl3945_tfd_frame *tfd;
  2102. u32 *control_flags;
  2103. int txq_id = skb_get_queue_mapping(skb);
  2104. struct iwl3945_tx_queue *txq = NULL;
  2105. struct iwl3945_queue *q = NULL;
  2106. dma_addr_t phys_addr;
  2107. dma_addr_t txcmd_phys;
  2108. struct iwl3945_cmd *out_cmd = NULL;
  2109. u16 len, idx, len_org, hdr_len;
  2110. u8 id;
  2111. u8 unicast;
  2112. u8 sta_id;
  2113. u8 tid = 0;
  2114. u16 seq_number = 0;
  2115. __le16 fc;
  2116. u8 wait_write_ptr = 0;
  2117. u8 *qc = NULL;
  2118. unsigned long flags;
  2119. int rc;
  2120. spin_lock_irqsave(&priv->lock, flags);
  2121. if (iwl3945_is_rfkill(priv)) {
  2122. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  2123. goto drop_unlock;
  2124. }
  2125. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  2126. IWL_ERROR("ERROR: No TX rate available.\n");
  2127. goto drop_unlock;
  2128. }
  2129. unicast = !is_multicast_ether_addr(hdr->addr1);
  2130. id = 0;
  2131. fc = hdr->frame_control;
  2132. #ifdef CONFIG_IWL3945_DEBUG
  2133. if (ieee80211_is_auth(fc))
  2134. IWL_DEBUG_TX("Sending AUTH frame\n");
  2135. else if (ieee80211_is_assoc_req(fc))
  2136. IWL_DEBUG_TX("Sending ASSOC frame\n");
  2137. else if (ieee80211_is_reassoc_req(fc))
  2138. IWL_DEBUG_TX("Sending REASSOC frame\n");
  2139. #endif
  2140. /* drop all data frame if we are not associated */
  2141. if (ieee80211_is_data(fc) &&
  2142. (priv->iw_mode != IEEE80211_IF_TYPE_MNTR) && /* packet injection */
  2143. (!iwl3945_is_associated(priv) ||
  2144. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id))) {
  2145. IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
  2146. goto drop_unlock;
  2147. }
  2148. spin_unlock_irqrestore(&priv->lock, flags);
  2149. hdr_len = ieee80211_hdrlen(fc);
  2150. /* Find (or create) index into station table for destination station */
  2151. sta_id = iwl3945_get_sta_id(priv, hdr);
  2152. if (sta_id == IWL_INVALID_STATION) {
  2153. DECLARE_MAC_BUF(mac);
  2154. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  2155. print_mac(mac, hdr->addr1));
  2156. goto drop;
  2157. }
  2158. IWL_DEBUG_RATE("station Id %d\n", sta_id);
  2159. if (ieee80211_is_data_qos(fc)) {
  2160. qc = ieee80211_get_qos_ctl(hdr);
  2161. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  2162. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  2163. IEEE80211_SCTL_SEQ;
  2164. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  2165. (hdr->seq_ctrl &
  2166. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  2167. seq_number += 0x10;
  2168. }
  2169. /* Descriptor for chosen Tx queue */
  2170. txq = &priv->txq[txq_id];
  2171. q = &txq->q;
  2172. spin_lock_irqsave(&priv->lock, flags);
  2173. /* Set up first empty TFD within this queue's circular TFD buffer */
  2174. tfd = &txq->bd[q->write_ptr];
  2175. memset(tfd, 0, sizeof(*tfd));
  2176. control_flags = (u32 *) tfd;
  2177. idx = get_cmd_index(q, q->write_ptr, 0);
  2178. /* Set up driver data for this TFD */
  2179. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
  2180. txq->txb[q->write_ptr].skb[0] = skb;
  2181. /* Init first empty entry in queue's array of Tx/cmd buffers */
  2182. out_cmd = &txq->cmd[idx];
  2183. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  2184. memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
  2185. /*
  2186. * Set up the Tx-command (not MAC!) header.
  2187. * Store the chosen Tx queue and TFD index within the sequence field;
  2188. * after Tx, uCode's Tx response will return this value so driver can
  2189. * locate the frame within the tx queue and do post-tx processing.
  2190. */
  2191. out_cmd->hdr.cmd = REPLY_TX;
  2192. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  2193. INDEX_TO_SEQ(q->write_ptr)));
  2194. /* Copy MAC header from skb into command buffer */
  2195. memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
  2196. /*
  2197. * Use the first empty entry in this queue's command buffer array
  2198. * to contain the Tx command and MAC header concatenated together
  2199. * (payload data will be in another buffer).
  2200. * Size of this varies, due to varying MAC header length.
  2201. * If end is not dword aligned, we'll have 2 extra bytes at the end
  2202. * of the MAC header (device reads on dword boundaries).
  2203. * We'll tell device about this padding later.
  2204. */
  2205. len = priv->hw_setting.tx_cmd_len +
  2206. sizeof(struct iwl3945_cmd_header) + hdr_len;
  2207. len_org = len;
  2208. len = (len + 3) & ~3;
  2209. if (len_org != len)
  2210. len_org = 1;
  2211. else
  2212. len_org = 0;
  2213. /* Physical address of this Tx command's header (not MAC header!),
  2214. * within command buffer array. */
  2215. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl3945_cmd) * idx +
  2216. offsetof(struct iwl3945_cmd, hdr);
  2217. /* Add buffer containing Tx command and MAC(!) header to TFD's
  2218. * first entry */
  2219. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  2220. if (info->control.hw_key)
  2221. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
  2222. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  2223. * if any (802.11 null frames have no payload). */
  2224. len = skb->len - hdr_len;
  2225. if (len) {
  2226. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  2227. len, PCI_DMA_TODEVICE);
  2228. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  2229. }
  2230. if (!len)
  2231. /* If there is no payload, then we use only one Tx buffer */
  2232. *control_flags = TFD_CTL_COUNT_SET(1);
  2233. else
  2234. /* Else use 2 buffers.
  2235. * Tell 3945 about any padding after MAC header */
  2236. *control_flags = TFD_CTL_COUNT_SET(2) |
  2237. TFD_CTL_PAD_SET(U32_PAD(len));
  2238. /* Total # bytes to be transmitted */
  2239. len = (u16)skb->len;
  2240. out_cmd->cmd.tx.len = cpu_to_le16(len);
  2241. /* TODO need this for burst mode later on */
  2242. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
  2243. /* set is_hcca to 0; it probably will never be implemented */
  2244. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  2245. out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  2246. out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  2247. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  2248. txq->need_update = 1;
  2249. if (qc)
  2250. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  2251. } else {
  2252. wait_write_ptr = 1;
  2253. txq->need_update = 0;
  2254. }
  2255. iwl3945_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
  2256. sizeof(out_cmd->cmd.tx));
  2257. iwl3945_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
  2258. ieee80211_hdrlen(fc));
  2259. /* Tell device the write index *just past* this latest filled TFD */
  2260. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  2261. rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
  2262. spin_unlock_irqrestore(&priv->lock, flags);
  2263. if (rc)
  2264. return rc;
  2265. if ((iwl3945_queue_space(q) < q->high_mark)
  2266. && priv->mac80211_registered) {
  2267. if (wait_write_ptr) {
  2268. spin_lock_irqsave(&priv->lock, flags);
  2269. txq->need_update = 1;
  2270. iwl3945_tx_queue_update_write_ptr(priv, txq);
  2271. spin_unlock_irqrestore(&priv->lock, flags);
  2272. }
  2273. ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
  2274. }
  2275. return 0;
  2276. drop_unlock:
  2277. spin_unlock_irqrestore(&priv->lock, flags);
  2278. drop:
  2279. return -1;
  2280. }
  2281. static void iwl3945_set_rate(struct iwl3945_priv *priv)
  2282. {
  2283. const struct ieee80211_supported_band *sband = NULL;
  2284. struct ieee80211_rate *rate;
  2285. int i;
  2286. sband = iwl3945_get_band(priv, priv->band);
  2287. if (!sband) {
  2288. IWL_ERROR("Failed to set rate: unable to get hw mode\n");
  2289. return;
  2290. }
  2291. priv->active_rate = 0;
  2292. priv->active_rate_basic = 0;
  2293. IWL_DEBUG_RATE("Setting rates for %s GHz\n",
  2294. sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
  2295. for (i = 0; i < sband->n_bitrates; i++) {
  2296. rate = &sband->bitrates[i];
  2297. if ((rate->hw_value < IWL_RATE_COUNT) &&
  2298. !(rate->flags & IEEE80211_CHAN_DISABLED)) {
  2299. IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
  2300. rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
  2301. priv->active_rate |= (1 << rate->hw_value);
  2302. }
  2303. }
  2304. IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
  2305. priv->active_rate, priv->active_rate_basic);
  2306. /*
  2307. * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
  2308. * otherwise set it to the default of all CCK rates and 6, 12, 24 for
  2309. * OFDM
  2310. */
  2311. if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
  2312. priv->staging_rxon.cck_basic_rates =
  2313. ((priv->active_rate_basic &
  2314. IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
  2315. else
  2316. priv->staging_rxon.cck_basic_rates =
  2317. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  2318. if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
  2319. priv->staging_rxon.ofdm_basic_rates =
  2320. ((priv->active_rate_basic &
  2321. (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
  2322. IWL_FIRST_OFDM_RATE) & 0xFF;
  2323. else
  2324. priv->staging_rxon.ofdm_basic_rates =
  2325. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  2326. }
  2327. static void iwl3945_radio_kill_sw(struct iwl3945_priv *priv, int disable_radio)
  2328. {
  2329. unsigned long flags;
  2330. if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
  2331. return;
  2332. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
  2333. disable_radio ? "OFF" : "ON");
  2334. if (disable_radio) {
  2335. iwl3945_scan_cancel(priv);
  2336. /* FIXME: This is a workaround for AP */
  2337. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  2338. spin_lock_irqsave(&priv->lock, flags);
  2339. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2340. CSR_UCODE_SW_BIT_RFKILL);
  2341. spin_unlock_irqrestore(&priv->lock, flags);
  2342. iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
  2343. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2344. }
  2345. return;
  2346. }
  2347. spin_lock_irqsave(&priv->lock, flags);
  2348. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2349. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2350. spin_unlock_irqrestore(&priv->lock, flags);
  2351. /* wake up ucode */
  2352. msleep(10);
  2353. spin_lock_irqsave(&priv->lock, flags);
  2354. iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
  2355. if (!iwl3945_grab_nic_access(priv))
  2356. iwl3945_release_nic_access(priv);
  2357. spin_unlock_irqrestore(&priv->lock, flags);
  2358. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  2359. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  2360. "disabled by HW switch\n");
  2361. return;
  2362. }
  2363. if (priv->is_open)
  2364. queue_work(priv->workqueue, &priv->restart);
  2365. return;
  2366. }
  2367. void iwl3945_set_decrypted_flag(struct iwl3945_priv *priv, struct sk_buff *skb,
  2368. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2369. {
  2370. u16 fc =
  2371. le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
  2372. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2373. return;
  2374. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2375. return;
  2376. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  2377. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2378. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2379. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2380. RX_RES_STATUS_BAD_ICV_MIC)
  2381. stats->flag |= RX_FLAG_MMIC_ERROR;
  2382. case RX_RES_STATUS_SEC_TYPE_WEP:
  2383. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2384. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2385. RX_RES_STATUS_DECRYPT_OK) {
  2386. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  2387. stats->flag |= RX_FLAG_DECRYPTED;
  2388. }
  2389. break;
  2390. default:
  2391. break;
  2392. }
  2393. }
  2394. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  2395. #include "iwl-spectrum.h"
  2396. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  2397. #define BEACON_TIME_MASK_HIGH 0xFF000000
  2398. #define TIME_UNIT 1024
  2399. /*
  2400. * extended beacon time format
  2401. * time in usec will be changed into a 32-bit value in 8:24 format
  2402. * the high 1 byte is the beacon counts
  2403. * the lower 3 bytes is the time in usec within one beacon interval
  2404. */
  2405. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  2406. {
  2407. u32 quot;
  2408. u32 rem;
  2409. u32 interval = beacon_interval * 1024;
  2410. if (!interval || !usec)
  2411. return 0;
  2412. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  2413. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  2414. return (quot << 24) + rem;
  2415. }
  2416. /* base is usually what we get from ucode with each received frame,
  2417. * the same as HW timer counter counting down
  2418. */
  2419. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  2420. {
  2421. u32 base_low = base & BEACON_TIME_MASK_LOW;
  2422. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  2423. u32 interval = beacon_interval * TIME_UNIT;
  2424. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  2425. (addon & BEACON_TIME_MASK_HIGH);
  2426. if (base_low > addon_low)
  2427. res += base_low - addon_low;
  2428. else if (base_low < addon_low) {
  2429. res += interval + base_low - addon_low;
  2430. res += (1 << 24);
  2431. } else
  2432. res += (1 << 24);
  2433. return cpu_to_le32(res);
  2434. }
  2435. static int iwl3945_get_measurement(struct iwl3945_priv *priv,
  2436. struct ieee80211_measurement_params *params,
  2437. u8 type)
  2438. {
  2439. struct iwl3945_spectrum_cmd spectrum;
  2440. struct iwl3945_rx_packet *res;
  2441. struct iwl3945_host_cmd cmd = {
  2442. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  2443. .data = (void *)&spectrum,
  2444. .meta.flags = CMD_WANT_SKB,
  2445. };
  2446. u32 add_time = le64_to_cpu(params->start_time);
  2447. int rc;
  2448. int spectrum_resp_status;
  2449. int duration = le16_to_cpu(params->duration);
  2450. if (iwl3945_is_associated(priv))
  2451. add_time =
  2452. iwl3945_usecs_to_beacons(
  2453. le64_to_cpu(params->start_time) - priv->last_tsf,
  2454. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2455. memset(&spectrum, 0, sizeof(spectrum));
  2456. spectrum.channel_count = cpu_to_le16(1);
  2457. spectrum.flags =
  2458. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  2459. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  2460. cmd.len = sizeof(spectrum);
  2461. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  2462. if (iwl3945_is_associated(priv))
  2463. spectrum.start_time =
  2464. iwl3945_add_beacon_time(priv->last_beacon_time,
  2465. add_time,
  2466. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2467. else
  2468. spectrum.start_time = 0;
  2469. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  2470. spectrum.channels[0].channel = params->channel;
  2471. spectrum.channels[0].type = type;
  2472. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  2473. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  2474. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  2475. rc = iwl3945_send_cmd_sync(priv, &cmd);
  2476. if (rc)
  2477. return rc;
  2478. res = (struct iwl3945_rx_packet *)cmd.meta.u.skb->data;
  2479. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  2480. IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
  2481. rc = -EIO;
  2482. }
  2483. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  2484. switch (spectrum_resp_status) {
  2485. case 0: /* Command will be handled */
  2486. if (res->u.spectrum.id != 0xff) {
  2487. IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
  2488. res->u.spectrum.id);
  2489. priv->measurement_status &= ~MEASUREMENT_READY;
  2490. }
  2491. priv->measurement_status |= MEASUREMENT_ACTIVE;
  2492. rc = 0;
  2493. break;
  2494. case 1: /* Command will not be handled */
  2495. rc = -EAGAIN;
  2496. break;
  2497. }
  2498. dev_kfree_skb_any(cmd.meta.u.skb);
  2499. return rc;
  2500. }
  2501. #endif
  2502. static void iwl3945_rx_reply_alive(struct iwl3945_priv *priv,
  2503. struct iwl3945_rx_mem_buffer *rxb)
  2504. {
  2505. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2506. struct iwl3945_alive_resp *palive;
  2507. struct delayed_work *pwork;
  2508. palive = &pkt->u.alive_frame;
  2509. IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
  2510. "0x%01X 0x%01X\n",
  2511. palive->is_valid, palive->ver_type,
  2512. palive->ver_subtype);
  2513. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  2514. IWL_DEBUG_INFO("Initialization Alive received.\n");
  2515. memcpy(&priv->card_alive_init,
  2516. &pkt->u.alive_frame,
  2517. sizeof(struct iwl3945_init_alive_resp));
  2518. pwork = &priv->init_alive_start;
  2519. } else {
  2520. IWL_DEBUG_INFO("Runtime Alive received.\n");
  2521. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  2522. sizeof(struct iwl3945_alive_resp));
  2523. pwork = &priv->alive_start;
  2524. iwl3945_disable_events(priv);
  2525. }
  2526. /* We delay the ALIVE response by 5ms to
  2527. * give the HW RF Kill time to activate... */
  2528. if (palive->is_valid == UCODE_VALID_OK)
  2529. queue_delayed_work(priv->workqueue, pwork,
  2530. msecs_to_jiffies(5));
  2531. else
  2532. IWL_WARNING("uCode did not respond OK.\n");
  2533. }
  2534. static void iwl3945_rx_reply_add_sta(struct iwl3945_priv *priv,
  2535. struct iwl3945_rx_mem_buffer *rxb)
  2536. {
  2537. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2538. IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  2539. return;
  2540. }
  2541. static void iwl3945_rx_reply_error(struct iwl3945_priv *priv,
  2542. struct iwl3945_rx_mem_buffer *rxb)
  2543. {
  2544. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2545. IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
  2546. "seq 0x%04X ser 0x%08X\n",
  2547. le32_to_cpu(pkt->u.err_resp.error_type),
  2548. get_cmd_string(pkt->u.err_resp.cmd_id),
  2549. pkt->u.err_resp.cmd_id,
  2550. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  2551. le32_to_cpu(pkt->u.err_resp.error_info));
  2552. }
  2553. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  2554. static void iwl3945_rx_csa(struct iwl3945_priv *priv, struct iwl3945_rx_mem_buffer *rxb)
  2555. {
  2556. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2557. struct iwl3945_rxon_cmd *rxon = (void *)&priv->active_rxon;
  2558. struct iwl3945_csa_notification *csa = &(pkt->u.csa_notif);
  2559. IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
  2560. le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
  2561. rxon->channel = csa->channel;
  2562. priv->staging_rxon.channel = csa->channel;
  2563. }
  2564. static void iwl3945_rx_spectrum_measure_notif(struct iwl3945_priv *priv,
  2565. struct iwl3945_rx_mem_buffer *rxb)
  2566. {
  2567. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  2568. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2569. struct iwl3945_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2570. if (!report->state) {
  2571. IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
  2572. "Spectrum Measure Notification: Start\n");
  2573. return;
  2574. }
  2575. memcpy(&priv->measure_report, report, sizeof(*report));
  2576. priv->measurement_status |= MEASUREMENT_READY;
  2577. #endif
  2578. }
  2579. static void iwl3945_rx_pm_sleep_notif(struct iwl3945_priv *priv,
  2580. struct iwl3945_rx_mem_buffer *rxb)
  2581. {
  2582. #ifdef CONFIG_IWL3945_DEBUG
  2583. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2584. struct iwl3945_sleep_notification *sleep = &(pkt->u.sleep_notif);
  2585. IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
  2586. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  2587. #endif
  2588. }
  2589. static void iwl3945_rx_pm_debug_statistics_notif(struct iwl3945_priv *priv,
  2590. struct iwl3945_rx_mem_buffer *rxb)
  2591. {
  2592. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2593. IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
  2594. "notification for %s:\n",
  2595. le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
  2596. iwl3945_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
  2597. }
  2598. static void iwl3945_bg_beacon_update(struct work_struct *work)
  2599. {
  2600. struct iwl3945_priv *priv =
  2601. container_of(work, struct iwl3945_priv, beacon_update);
  2602. struct sk_buff *beacon;
  2603. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  2604. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  2605. if (!beacon) {
  2606. IWL_ERROR("update beacon failed\n");
  2607. return;
  2608. }
  2609. mutex_lock(&priv->mutex);
  2610. /* new beacon skb is allocated every time; dispose previous.*/
  2611. if (priv->ibss_beacon)
  2612. dev_kfree_skb(priv->ibss_beacon);
  2613. priv->ibss_beacon = beacon;
  2614. mutex_unlock(&priv->mutex);
  2615. iwl3945_send_beacon_cmd(priv);
  2616. }
  2617. static void iwl3945_rx_beacon_notif(struct iwl3945_priv *priv,
  2618. struct iwl3945_rx_mem_buffer *rxb)
  2619. {
  2620. #ifdef CONFIG_IWL3945_DEBUG
  2621. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2622. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  2623. u8 rate = beacon->beacon_notify_hdr.rate;
  2624. IWL_DEBUG_RX("beacon status %x retries %d iss %d "
  2625. "tsf %d %d rate %d\n",
  2626. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  2627. beacon->beacon_notify_hdr.failure_frame,
  2628. le32_to_cpu(beacon->ibss_mgr_status),
  2629. le32_to_cpu(beacon->high_tsf),
  2630. le32_to_cpu(beacon->low_tsf), rate);
  2631. #endif
  2632. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  2633. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  2634. queue_work(priv->workqueue, &priv->beacon_update);
  2635. }
  2636. /* Service response to REPLY_SCAN_CMD (0x80) */
  2637. static void iwl3945_rx_reply_scan(struct iwl3945_priv *priv,
  2638. struct iwl3945_rx_mem_buffer *rxb)
  2639. {
  2640. #ifdef CONFIG_IWL3945_DEBUG
  2641. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2642. struct iwl3945_scanreq_notification *notif =
  2643. (struct iwl3945_scanreq_notification *)pkt->u.raw;
  2644. IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
  2645. #endif
  2646. }
  2647. /* Service SCAN_START_NOTIFICATION (0x82) */
  2648. static void iwl3945_rx_scan_start_notif(struct iwl3945_priv *priv,
  2649. struct iwl3945_rx_mem_buffer *rxb)
  2650. {
  2651. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2652. struct iwl3945_scanstart_notification *notif =
  2653. (struct iwl3945_scanstart_notification *)pkt->u.raw;
  2654. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  2655. IWL_DEBUG_SCAN("Scan start: "
  2656. "%d [802.11%s] "
  2657. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  2658. notif->channel,
  2659. notif->band ? "bg" : "a",
  2660. notif->tsf_high,
  2661. notif->tsf_low, notif->status, notif->beacon_timer);
  2662. }
  2663. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  2664. static void iwl3945_rx_scan_results_notif(struct iwl3945_priv *priv,
  2665. struct iwl3945_rx_mem_buffer *rxb)
  2666. {
  2667. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2668. struct iwl3945_scanresults_notification *notif =
  2669. (struct iwl3945_scanresults_notification *)pkt->u.raw;
  2670. IWL_DEBUG_SCAN("Scan ch.res: "
  2671. "%d [802.11%s] "
  2672. "(TSF: 0x%08X:%08X) - %d "
  2673. "elapsed=%lu usec (%dms since last)\n",
  2674. notif->channel,
  2675. notif->band ? "bg" : "a",
  2676. le32_to_cpu(notif->tsf_high),
  2677. le32_to_cpu(notif->tsf_low),
  2678. le32_to_cpu(notif->statistics[0]),
  2679. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
  2680. jiffies_to_msecs(elapsed_jiffies
  2681. (priv->last_scan_jiffies, jiffies)));
  2682. priv->last_scan_jiffies = jiffies;
  2683. priv->next_scan_jiffies = 0;
  2684. }
  2685. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  2686. static void iwl3945_rx_scan_complete_notif(struct iwl3945_priv *priv,
  2687. struct iwl3945_rx_mem_buffer *rxb)
  2688. {
  2689. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2690. struct iwl3945_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  2691. IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  2692. scan_notif->scanned_channels,
  2693. scan_notif->tsf_low,
  2694. scan_notif->tsf_high, scan_notif->status);
  2695. /* The HW is no longer scanning */
  2696. clear_bit(STATUS_SCAN_HW, &priv->status);
  2697. /* The scan completion notification came in, so kill that timer... */
  2698. cancel_delayed_work(&priv->scan_check);
  2699. IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
  2700. (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
  2701. "2.4" : "5.2",
  2702. jiffies_to_msecs(elapsed_jiffies
  2703. (priv->scan_pass_start, jiffies)));
  2704. /* Remove this scanned band from the list of pending
  2705. * bands to scan, band G precedes A in order of scanning
  2706. * as seen in iwl3945_bg_request_scan */
  2707. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
  2708. priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
  2709. else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
  2710. priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
  2711. /* If a request to abort was given, or the scan did not succeed
  2712. * then we reset the scan state machine and terminate,
  2713. * re-queuing another scan if one has been requested */
  2714. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2715. IWL_DEBUG_INFO("Aborted scan completed.\n");
  2716. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  2717. } else {
  2718. /* If there are more bands on this scan pass reschedule */
  2719. if (priv->scan_bands > 0)
  2720. goto reschedule;
  2721. }
  2722. priv->last_scan_jiffies = jiffies;
  2723. priv->next_scan_jiffies = 0;
  2724. IWL_DEBUG_INFO("Setting scan to off\n");
  2725. clear_bit(STATUS_SCANNING, &priv->status);
  2726. IWL_DEBUG_INFO("Scan took %dms\n",
  2727. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  2728. queue_work(priv->workqueue, &priv->scan_completed);
  2729. return;
  2730. reschedule:
  2731. priv->scan_pass_start = jiffies;
  2732. queue_work(priv->workqueue, &priv->request_scan);
  2733. }
  2734. /* Handle notification from uCode that card's power state is changing
  2735. * due to software, hardware, or critical temperature RFKILL */
  2736. static void iwl3945_rx_card_state_notif(struct iwl3945_priv *priv,
  2737. struct iwl3945_rx_mem_buffer *rxb)
  2738. {
  2739. struct iwl3945_rx_packet *pkt = (void *)rxb->skb->data;
  2740. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  2741. unsigned long status = priv->status;
  2742. IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
  2743. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  2744. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  2745. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2746. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2747. if (flags & HW_CARD_DISABLED)
  2748. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2749. else
  2750. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2751. if (flags & SW_CARD_DISABLED)
  2752. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2753. else
  2754. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2755. iwl3945_scan_cancel(priv);
  2756. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  2757. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  2758. (test_bit(STATUS_RF_KILL_SW, &status) !=
  2759. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  2760. queue_work(priv->workqueue, &priv->rf_kill);
  2761. else
  2762. wake_up_interruptible(&priv->wait_command_queue);
  2763. }
  2764. /**
  2765. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  2766. *
  2767. * Setup the RX handlers for each of the reply types sent from the uCode
  2768. * to the host.
  2769. *
  2770. * This function chains into the hardware specific files for them to setup
  2771. * any hardware specific handlers as well.
  2772. */
  2773. static void iwl3945_setup_rx_handlers(struct iwl3945_priv *priv)
  2774. {
  2775. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  2776. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  2777. priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
  2778. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
  2779. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  2780. iwl3945_rx_spectrum_measure_notif;
  2781. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
  2782. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  2783. iwl3945_rx_pm_debug_statistics_notif;
  2784. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  2785. /*
  2786. * The same handler is used for both the REPLY to a discrete
  2787. * statistics request from the host as well as for the periodic
  2788. * statistics notifications (after received beacons) from the uCode.
  2789. */
  2790. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  2791. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  2792. priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
  2793. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
  2794. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  2795. iwl3945_rx_scan_results_notif;
  2796. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  2797. iwl3945_rx_scan_complete_notif;
  2798. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  2799. /* Set up hardware specific Rx handlers */
  2800. iwl3945_hw_rx_handler_setup(priv);
  2801. }
  2802. /**
  2803. * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
  2804. * When FW advances 'R' index, all entries between old and new 'R' index
  2805. * need to be reclaimed.
  2806. */
  2807. static void iwl3945_cmd_queue_reclaim(struct iwl3945_priv *priv,
  2808. int txq_id, int index)
  2809. {
  2810. struct iwl3945_tx_queue *txq = &priv->txq[txq_id];
  2811. struct iwl3945_queue *q = &txq->q;
  2812. int nfreed = 0;
  2813. if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
  2814. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  2815. "is out of range [0-%d] %d %d.\n", txq_id,
  2816. index, q->n_bd, q->write_ptr, q->read_ptr);
  2817. return;
  2818. }
  2819. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  2820. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2821. if (nfreed > 1) {
  2822. IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
  2823. q->write_ptr, q->read_ptr);
  2824. queue_work(priv->workqueue, &priv->restart);
  2825. break;
  2826. }
  2827. nfreed++;
  2828. }
  2829. }
  2830. /**
  2831. * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2832. * @rxb: Rx buffer to reclaim
  2833. *
  2834. * If an Rx buffer has an async callback associated with it the callback
  2835. * will be executed. The attached skb (if present) will only be freed
  2836. * if the callback returns 1
  2837. */
  2838. static void iwl3945_tx_cmd_complete(struct iwl3945_priv *priv,
  2839. struct iwl3945_rx_mem_buffer *rxb)
  2840. {
  2841. struct iwl3945_rx_packet *pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
  2842. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2843. int txq_id = SEQ_TO_QUEUE(sequence);
  2844. int index = SEQ_TO_INDEX(sequence);
  2845. int huge = sequence & SEQ_HUGE_FRAME;
  2846. int cmd_index;
  2847. struct iwl3945_cmd *cmd;
  2848. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  2849. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  2850. cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  2851. /* Input error checking is done when commands are added to queue. */
  2852. if (cmd->meta.flags & CMD_WANT_SKB) {
  2853. cmd->meta.source->u.skb = rxb->skb;
  2854. rxb->skb = NULL;
  2855. } else if (cmd->meta.u.callback &&
  2856. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  2857. rxb->skb = NULL;
  2858. iwl3945_cmd_queue_reclaim(priv, txq_id, index);
  2859. if (!(cmd->meta.flags & CMD_ASYNC)) {
  2860. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  2861. wake_up_interruptible(&priv->wait_command_queue);
  2862. }
  2863. }
  2864. /************************** RX-FUNCTIONS ****************************/
  2865. /*
  2866. * Rx theory of operation
  2867. *
  2868. * The host allocates 32 DMA target addresses and passes the host address
  2869. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  2870. * 0 to 31
  2871. *
  2872. * Rx Queue Indexes
  2873. * The host/firmware share two index registers for managing the Rx buffers.
  2874. *
  2875. * The READ index maps to the first position that the firmware may be writing
  2876. * to -- the driver can read up to (but not including) this position and get
  2877. * good data.
  2878. * The READ index is managed by the firmware once the card is enabled.
  2879. *
  2880. * The WRITE index maps to the last position the driver has read from -- the
  2881. * position preceding WRITE is the last slot the firmware can place a packet.
  2882. *
  2883. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2884. * WRITE = READ.
  2885. *
  2886. * During initialization, the host sets up the READ queue position to the first
  2887. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  2888. *
  2889. * When the firmware places a packet in a buffer, it will advance the READ index
  2890. * and fire the RX interrupt. The driver can then query the READ index and
  2891. * process as many packets as possible, moving the WRITE index forward as it
  2892. * resets the Rx queue buffers with new memory.
  2893. *
  2894. * The management in the driver is as follows:
  2895. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2896. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2897. * to replenish the iwl->rxq->rx_free.
  2898. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  2899. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  2900. * 'processed' and 'read' driver indexes as well)
  2901. * + A received packet is processed and handed to the kernel network stack,
  2902. * detached from the iwl->rxq. The driver 'processed' index is updated.
  2903. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2904. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2905. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  2906. * were enough free buffers and RX_STALLED is set it is cleared.
  2907. *
  2908. *
  2909. * Driver sequence:
  2910. *
  2911. * iwl3945_rx_queue_alloc() Allocates rx_free
  2912. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2913. * iwl3945_rx_queue_restock
  2914. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  2915. * queue, updates firmware pointers, and updates
  2916. * the WRITE index. If insufficient rx_free buffers
  2917. * are available, schedules iwl3945_rx_replenish
  2918. *
  2919. * -- enable interrupts --
  2920. * ISR - iwl3945_rx() Detach iwl3945_rx_mem_buffers from pool up to the
  2921. * READ INDEX, detaching the SKB from the pool.
  2922. * Moves the packet buffer from queue to rx_used.
  2923. * Calls iwl3945_rx_queue_restock to refill any empty
  2924. * slots.
  2925. * ...
  2926. *
  2927. */
  2928. /**
  2929. * iwl3945_rx_queue_space - Return number of free slots available in queue.
  2930. */
  2931. static int iwl3945_rx_queue_space(const struct iwl3945_rx_queue *q)
  2932. {
  2933. int s = q->read - q->write;
  2934. if (s <= 0)
  2935. s += RX_QUEUE_SIZE;
  2936. /* keep some buffer to not confuse full and empty queue */
  2937. s -= 2;
  2938. if (s < 0)
  2939. s = 0;
  2940. return s;
  2941. }
  2942. /**
  2943. * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  2944. */
  2945. int iwl3945_rx_queue_update_write_ptr(struct iwl3945_priv *priv, struct iwl3945_rx_queue *q)
  2946. {
  2947. u32 reg = 0;
  2948. int rc = 0;
  2949. unsigned long flags;
  2950. spin_lock_irqsave(&q->lock, flags);
  2951. if (q->need_update == 0)
  2952. goto exit_unlock;
  2953. /* If power-saving is in use, make sure device is awake */
  2954. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  2955. reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
  2956. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2957. iwl3945_set_bit(priv, CSR_GP_CNTRL,
  2958. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2959. goto exit_unlock;
  2960. }
  2961. rc = iwl3945_grab_nic_access(priv);
  2962. if (rc)
  2963. goto exit_unlock;
  2964. /* Device expects a multiple of 8 */
  2965. iwl3945_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  2966. q->write & ~0x7);
  2967. iwl3945_release_nic_access(priv);
  2968. /* Else device is assumed to be awake */
  2969. } else
  2970. /* Device expects a multiple of 8 */
  2971. iwl3945_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
  2972. q->need_update = 0;
  2973. exit_unlock:
  2974. spin_unlock_irqrestore(&q->lock, flags);
  2975. return rc;
  2976. }
  2977. /**
  2978. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  2979. */
  2980. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl3945_priv *priv,
  2981. dma_addr_t dma_addr)
  2982. {
  2983. return cpu_to_le32((u32)dma_addr);
  2984. }
  2985. /**
  2986. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  2987. *
  2988. * If there are slots in the RX queue that need to be restocked,
  2989. * and we have free pre-allocated buffers, fill the ranks as much
  2990. * as we can, pulling from rx_free.
  2991. *
  2992. * This moves the 'write' index forward to catch up with 'processed', and
  2993. * also updates the memory address in the firmware to reference the new
  2994. * target buffer.
  2995. */
  2996. static int iwl3945_rx_queue_restock(struct iwl3945_priv *priv)
  2997. {
  2998. struct iwl3945_rx_queue *rxq = &priv->rxq;
  2999. struct list_head *element;
  3000. struct iwl3945_rx_mem_buffer *rxb;
  3001. unsigned long flags;
  3002. int write, rc;
  3003. spin_lock_irqsave(&rxq->lock, flags);
  3004. write = rxq->write & ~0x7;
  3005. while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  3006. /* Get next free Rx buffer, remove from free list */
  3007. element = rxq->rx_free.next;
  3008. rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
  3009. list_del(element);
  3010. /* Point to Rx buffer via next RBD in circular buffer */
  3011. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->dma_addr);
  3012. rxq->queue[rxq->write] = rxb;
  3013. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  3014. rxq->free_count--;
  3015. }
  3016. spin_unlock_irqrestore(&rxq->lock, flags);
  3017. /* If the pre-allocated buffer pool is dropping low, schedule to
  3018. * refill it */
  3019. if (rxq->free_count <= RX_LOW_WATERMARK)
  3020. queue_work(priv->workqueue, &priv->rx_replenish);
  3021. /* If we've added more space for the firmware to place data, tell it.
  3022. * Increment device's write pointer in multiples of 8. */
  3023. if ((write != (rxq->write & ~0x7))
  3024. || (abs(rxq->write - rxq->read) > 7)) {
  3025. spin_lock_irqsave(&rxq->lock, flags);
  3026. rxq->need_update = 1;
  3027. spin_unlock_irqrestore(&rxq->lock, flags);
  3028. rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
  3029. if (rc)
  3030. return rc;
  3031. }
  3032. return 0;
  3033. }
  3034. /**
  3035. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  3036. *
  3037. * When moving to rx_free an SKB is allocated for the slot.
  3038. *
  3039. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  3040. * This is called as a scheduled work item (except for during initialization)
  3041. */
  3042. static void iwl3945_rx_allocate(struct iwl3945_priv *priv)
  3043. {
  3044. struct iwl3945_rx_queue *rxq = &priv->rxq;
  3045. struct list_head *element;
  3046. struct iwl3945_rx_mem_buffer *rxb;
  3047. unsigned long flags;
  3048. spin_lock_irqsave(&rxq->lock, flags);
  3049. while (!list_empty(&rxq->rx_used)) {
  3050. element = rxq->rx_used.next;
  3051. rxb = list_entry(element, struct iwl3945_rx_mem_buffer, list);
  3052. /* Alloc a new receive buffer */
  3053. rxb->skb =
  3054. alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
  3055. if (!rxb->skb) {
  3056. if (net_ratelimit())
  3057. printk(KERN_CRIT DRV_NAME
  3058. ": Can not allocate SKB buffers\n");
  3059. /* We don't reschedule replenish work here -- we will
  3060. * call the restock method and if it still needs
  3061. * more buffers it will schedule replenish */
  3062. break;
  3063. }
  3064. /* If radiotap head is required, reserve some headroom here.
  3065. * The physical head count is a variable rx_stats->phy_count.
  3066. * We reserve 4 bytes here. Plus these extra bytes, the
  3067. * headroom of the physical head should be enough for the
  3068. * radiotap head that iwl3945 supported. See iwl3945_rt.
  3069. */
  3070. skb_reserve(rxb->skb, 4);
  3071. priv->alloc_rxb_skb++;
  3072. list_del(element);
  3073. /* Get physical address of RB/SKB */
  3074. rxb->dma_addr =
  3075. pci_map_single(priv->pci_dev, rxb->skb->data,
  3076. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3077. list_add_tail(&rxb->list, &rxq->rx_free);
  3078. rxq->free_count++;
  3079. }
  3080. spin_unlock_irqrestore(&rxq->lock, flags);
  3081. }
  3082. /*
  3083. * this should be called while priv->lock is locked
  3084. */
  3085. static void __iwl3945_rx_replenish(void *data)
  3086. {
  3087. struct iwl3945_priv *priv = data;
  3088. iwl3945_rx_allocate(priv);
  3089. iwl3945_rx_queue_restock(priv);
  3090. }
  3091. void iwl3945_rx_replenish(void *data)
  3092. {
  3093. struct iwl3945_priv *priv = data;
  3094. unsigned long flags;
  3095. iwl3945_rx_allocate(priv);
  3096. spin_lock_irqsave(&priv->lock, flags);
  3097. iwl3945_rx_queue_restock(priv);
  3098. spin_unlock_irqrestore(&priv->lock, flags);
  3099. }
  3100. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  3101. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  3102. * This free routine walks the list of POOL entries and if SKB is set to
  3103. * non NULL it is unmapped and freed
  3104. */
  3105. static void iwl3945_rx_queue_free(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
  3106. {
  3107. int i;
  3108. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  3109. if (rxq->pool[i].skb != NULL) {
  3110. pci_unmap_single(priv->pci_dev,
  3111. rxq->pool[i].dma_addr,
  3112. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3113. dev_kfree_skb(rxq->pool[i].skb);
  3114. }
  3115. }
  3116. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  3117. rxq->dma_addr);
  3118. rxq->bd = NULL;
  3119. }
  3120. int iwl3945_rx_queue_alloc(struct iwl3945_priv *priv)
  3121. {
  3122. struct iwl3945_rx_queue *rxq = &priv->rxq;
  3123. struct pci_dev *dev = priv->pci_dev;
  3124. int i;
  3125. spin_lock_init(&rxq->lock);
  3126. INIT_LIST_HEAD(&rxq->rx_free);
  3127. INIT_LIST_HEAD(&rxq->rx_used);
  3128. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  3129. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  3130. if (!rxq->bd)
  3131. return -ENOMEM;
  3132. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3133. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  3134. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3135. /* Set us so that we have processed and used all buffers, but have
  3136. * not restocked the Rx queue with fresh buffers */
  3137. rxq->read = rxq->write = 0;
  3138. rxq->free_count = 0;
  3139. rxq->need_update = 0;
  3140. return 0;
  3141. }
  3142. void iwl3945_rx_queue_reset(struct iwl3945_priv *priv, struct iwl3945_rx_queue *rxq)
  3143. {
  3144. unsigned long flags;
  3145. int i;
  3146. spin_lock_irqsave(&rxq->lock, flags);
  3147. INIT_LIST_HEAD(&rxq->rx_free);
  3148. INIT_LIST_HEAD(&rxq->rx_used);
  3149. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3150. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  3151. /* In the reset function, these buffers may have been allocated
  3152. * to an SKB, so we need to unmap and free potential storage */
  3153. if (rxq->pool[i].skb != NULL) {
  3154. pci_unmap_single(priv->pci_dev,
  3155. rxq->pool[i].dma_addr,
  3156. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3157. priv->alloc_rxb_skb--;
  3158. dev_kfree_skb(rxq->pool[i].skb);
  3159. rxq->pool[i].skb = NULL;
  3160. }
  3161. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3162. }
  3163. /* Set us so that we have processed and used all buffers, but have
  3164. * not restocked the Rx queue with fresh buffers */
  3165. rxq->read = rxq->write = 0;
  3166. rxq->free_count = 0;
  3167. spin_unlock_irqrestore(&rxq->lock, flags);
  3168. }
  3169. /* Convert linear signal-to-noise ratio into dB */
  3170. static u8 ratio2dB[100] = {
  3171. /* 0 1 2 3 4 5 6 7 8 9 */
  3172. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  3173. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  3174. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  3175. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  3176. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  3177. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  3178. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  3179. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  3180. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  3181. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  3182. };
  3183. /* Calculates a relative dB value from a ratio of linear
  3184. * (i.e. not dB) signal levels.
  3185. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  3186. int iwl3945_calc_db_from_ratio(int sig_ratio)
  3187. {
  3188. /* 1000:1 or higher just report as 60 dB */
  3189. if (sig_ratio >= 1000)
  3190. return 60;
  3191. /* 100:1 or higher, divide by 10 and use table,
  3192. * add 20 dB to make up for divide by 10 */
  3193. if (sig_ratio >= 100)
  3194. return 20 + (int)ratio2dB[sig_ratio/10];
  3195. /* We shouldn't see this */
  3196. if (sig_ratio < 1)
  3197. return 0;
  3198. /* Use table for ratios 1:1 - 99:1 */
  3199. return (int)ratio2dB[sig_ratio];
  3200. }
  3201. #define PERFECT_RSSI (-20) /* dBm */
  3202. #define WORST_RSSI (-95) /* dBm */
  3203. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  3204. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  3205. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  3206. * about formulas used below. */
  3207. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  3208. {
  3209. int sig_qual;
  3210. int degradation = PERFECT_RSSI - rssi_dbm;
  3211. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  3212. * as indicator; formula is (signal dbm - noise dbm).
  3213. * SNR at or above 40 is a great signal (100%).
  3214. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  3215. * Weakest usable signal is usually 10 - 15 dB SNR. */
  3216. if (noise_dbm) {
  3217. if (rssi_dbm - noise_dbm >= 40)
  3218. return 100;
  3219. else if (rssi_dbm < noise_dbm)
  3220. return 0;
  3221. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  3222. /* Else use just the signal level.
  3223. * This formula is a least squares fit of data points collected and
  3224. * compared with a reference system that had a percentage (%) display
  3225. * for signal quality. */
  3226. } else
  3227. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  3228. (15 * RSSI_RANGE + 62 * degradation)) /
  3229. (RSSI_RANGE * RSSI_RANGE);
  3230. if (sig_qual > 100)
  3231. sig_qual = 100;
  3232. else if (sig_qual < 1)
  3233. sig_qual = 0;
  3234. return sig_qual;
  3235. }
  3236. /**
  3237. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  3238. *
  3239. * Uses the priv->rx_handlers callback function array to invoke
  3240. * the appropriate handlers, including command responses,
  3241. * frame-received notifications, and other notifications.
  3242. */
  3243. static void iwl3945_rx_handle(struct iwl3945_priv *priv)
  3244. {
  3245. struct iwl3945_rx_mem_buffer *rxb;
  3246. struct iwl3945_rx_packet *pkt;
  3247. struct iwl3945_rx_queue *rxq = &priv->rxq;
  3248. u32 r, i;
  3249. int reclaim;
  3250. unsigned long flags;
  3251. u8 fill_rx = 0;
  3252. u32 count = 8;
  3253. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  3254. * buffer that the driver may process (last buffer filled by ucode). */
  3255. r = iwl3945_hw_get_rx_read(priv);
  3256. i = rxq->read;
  3257. if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  3258. fill_rx = 1;
  3259. /* Rx interrupt, but nothing sent from uCode */
  3260. if (i == r)
  3261. IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  3262. while (i != r) {
  3263. rxb = rxq->queue[i];
  3264. /* If an RXB doesn't have a Rx queue slot associated with it,
  3265. * then a bug has been introduced in the queue refilling
  3266. * routines -- catch it here */
  3267. BUG_ON(rxb == NULL);
  3268. rxq->queue[i] = NULL;
  3269. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
  3270. IWL_RX_BUF_SIZE,
  3271. PCI_DMA_FROMDEVICE);
  3272. pkt = (struct iwl3945_rx_packet *)rxb->skb->data;
  3273. /* Reclaim a command buffer only if this packet is a response
  3274. * to a (driver-originated) command.
  3275. * If the packet (e.g. Rx frame) originated from uCode,
  3276. * there is no command buffer to reclaim.
  3277. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  3278. * but apparently a few don't get set; catch them here. */
  3279. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  3280. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  3281. (pkt->hdr.cmd != REPLY_TX);
  3282. /* Based on type of command response or notification,
  3283. * handle those that need handling via function in
  3284. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  3285. if (priv->rx_handlers[pkt->hdr.cmd]) {
  3286. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3287. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  3288. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3289. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  3290. } else {
  3291. /* No handling needed */
  3292. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3293. "r %d i %d No handler needed for %s, 0x%02x\n",
  3294. r, i, get_cmd_string(pkt->hdr.cmd),
  3295. pkt->hdr.cmd);
  3296. }
  3297. if (reclaim) {
  3298. /* Invoke any callbacks, transfer the skb to caller, and
  3299. * fire off the (possibly) blocking iwl3945_send_cmd()
  3300. * as we reclaim the driver command queue */
  3301. if (rxb && rxb->skb)
  3302. iwl3945_tx_cmd_complete(priv, rxb);
  3303. else
  3304. IWL_WARNING("Claim null rxb?\n");
  3305. }
  3306. /* For now we just don't re-use anything. We can tweak this
  3307. * later to try and re-use notification packets and SKBs that
  3308. * fail to Rx correctly */
  3309. if (rxb->skb != NULL) {
  3310. priv->alloc_rxb_skb--;
  3311. dev_kfree_skb_any(rxb->skb);
  3312. rxb->skb = NULL;
  3313. }
  3314. pci_unmap_single(priv->pci_dev, rxb->dma_addr,
  3315. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3316. spin_lock_irqsave(&rxq->lock, flags);
  3317. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  3318. spin_unlock_irqrestore(&rxq->lock, flags);
  3319. i = (i + 1) & RX_QUEUE_MASK;
  3320. /* If there are a lot of unused frames,
  3321. * restock the Rx queue so ucode won't assert. */
  3322. if (fill_rx) {
  3323. count++;
  3324. if (count >= 8) {
  3325. priv->rxq.read = i;
  3326. __iwl3945_rx_replenish(priv);
  3327. count = 0;
  3328. }
  3329. }
  3330. }
  3331. /* Backtrack one entry */
  3332. priv->rxq.read = i;
  3333. iwl3945_rx_queue_restock(priv);
  3334. }
  3335. /**
  3336. * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
  3337. */
  3338. static int iwl3945_tx_queue_update_write_ptr(struct iwl3945_priv *priv,
  3339. struct iwl3945_tx_queue *txq)
  3340. {
  3341. u32 reg = 0;
  3342. int rc = 0;
  3343. int txq_id = txq->q.id;
  3344. if (txq->need_update == 0)
  3345. return rc;
  3346. /* if we're trying to save power */
  3347. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  3348. /* wake up nic if it's powered down ...
  3349. * uCode will wake up, and interrupt us again, so next
  3350. * time we'll skip this part. */
  3351. reg = iwl3945_read32(priv, CSR_UCODE_DRV_GP1);
  3352. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  3353. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  3354. iwl3945_set_bit(priv, CSR_GP_CNTRL,
  3355. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  3356. return rc;
  3357. }
  3358. /* restore this queue's parameters in nic hardware. */
  3359. rc = iwl3945_grab_nic_access(priv);
  3360. if (rc)
  3361. return rc;
  3362. iwl3945_write_direct32(priv, HBUS_TARG_WRPTR,
  3363. txq->q.write_ptr | (txq_id << 8));
  3364. iwl3945_release_nic_access(priv);
  3365. /* else not in power-save mode, uCode will never sleep when we're
  3366. * trying to tx (during RFKILL, we're not trying to tx). */
  3367. } else
  3368. iwl3945_write32(priv, HBUS_TARG_WRPTR,
  3369. txq->q.write_ptr | (txq_id << 8));
  3370. txq->need_update = 0;
  3371. return rc;
  3372. }
  3373. #ifdef CONFIG_IWL3945_DEBUG
  3374. static void iwl3945_print_rx_config_cmd(struct iwl3945_rxon_cmd *rxon)
  3375. {
  3376. DECLARE_MAC_BUF(mac);
  3377. IWL_DEBUG_RADIO("RX CONFIG:\n");
  3378. iwl3945_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3379. IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3380. IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3381. IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
  3382. le32_to_cpu(rxon->filter_flags));
  3383. IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3384. IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
  3385. rxon->ofdm_basic_rates);
  3386. IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3387. IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
  3388. print_mac(mac, rxon->node_addr));
  3389. IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
  3390. print_mac(mac, rxon->bssid_addr));
  3391. IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3392. }
  3393. #endif
  3394. static void iwl3945_enable_interrupts(struct iwl3945_priv *priv)
  3395. {
  3396. IWL_DEBUG_ISR("Enabling interrupts\n");
  3397. set_bit(STATUS_INT_ENABLED, &priv->status);
  3398. iwl3945_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
  3399. }
  3400. /* call this function to flush any scheduled tasklet */
  3401. static inline void iwl_synchronize_irq(struct iwl3945_priv *priv)
  3402. {
  3403. /* wait to make sure we flush pedding tasklet*/
  3404. synchronize_irq(priv->pci_dev->irq);
  3405. tasklet_kill(&priv->irq_tasklet);
  3406. }
  3407. static inline void iwl3945_disable_interrupts(struct iwl3945_priv *priv)
  3408. {
  3409. clear_bit(STATUS_INT_ENABLED, &priv->status);
  3410. /* disable interrupts from uCode/NIC to host */
  3411. iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
  3412. /* acknowledge/clear/reset any interrupts still pending
  3413. * from uCode or flow handler (Rx/Tx DMA) */
  3414. iwl3945_write32(priv, CSR_INT, 0xffffffff);
  3415. iwl3945_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
  3416. IWL_DEBUG_ISR("Disabled interrupts\n");
  3417. }
  3418. static const char *desc_lookup(int i)
  3419. {
  3420. switch (i) {
  3421. case 1:
  3422. return "FAIL";
  3423. case 2:
  3424. return "BAD_PARAM";
  3425. case 3:
  3426. return "BAD_CHECKSUM";
  3427. case 4:
  3428. return "NMI_INTERRUPT";
  3429. case 5:
  3430. return "SYSASSERT";
  3431. case 6:
  3432. return "FATAL_ERROR";
  3433. }
  3434. return "UNKNOWN";
  3435. }
  3436. #define ERROR_START_OFFSET (1 * sizeof(u32))
  3437. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  3438. static void iwl3945_dump_nic_error_log(struct iwl3945_priv *priv)
  3439. {
  3440. u32 i;
  3441. u32 desc, time, count, base, data1;
  3442. u32 blink1, blink2, ilink1, ilink2;
  3443. int rc;
  3444. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  3445. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  3446. IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
  3447. return;
  3448. }
  3449. rc = iwl3945_grab_nic_access(priv);
  3450. if (rc) {
  3451. IWL_WARNING("Can not read from adapter at this time.\n");
  3452. return;
  3453. }
  3454. count = iwl3945_read_targ_mem(priv, base);
  3455. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  3456. IWL_ERROR("Start IWL Error Log Dump:\n");
  3457. IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
  3458. }
  3459. IWL_ERROR("Desc Time asrtPC blink2 "
  3460. "ilink1 nmiPC Line\n");
  3461. for (i = ERROR_START_OFFSET;
  3462. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  3463. i += ERROR_ELEM_SIZE) {
  3464. desc = iwl3945_read_targ_mem(priv, base + i);
  3465. time =
  3466. iwl3945_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  3467. blink1 =
  3468. iwl3945_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  3469. blink2 =
  3470. iwl3945_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  3471. ilink1 =
  3472. iwl3945_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  3473. ilink2 =
  3474. iwl3945_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  3475. data1 =
  3476. iwl3945_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  3477. IWL_ERROR
  3478. ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  3479. desc_lookup(desc), desc, time, blink1, blink2,
  3480. ilink1, ilink2, data1);
  3481. }
  3482. iwl3945_release_nic_access(priv);
  3483. }
  3484. #define EVENT_START_OFFSET (6 * sizeof(u32))
  3485. /**
  3486. * iwl3945_print_event_log - Dump error event log to syslog
  3487. *
  3488. * NOTE: Must be called with iwl3945_grab_nic_access() already obtained!
  3489. */
  3490. static void iwl3945_print_event_log(struct iwl3945_priv *priv, u32 start_idx,
  3491. u32 num_events, u32 mode)
  3492. {
  3493. u32 i;
  3494. u32 base; /* SRAM byte address of event log header */
  3495. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  3496. u32 ptr; /* SRAM byte address of log data */
  3497. u32 ev, time, data; /* event log data */
  3498. if (num_events == 0)
  3499. return;
  3500. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3501. if (mode == 0)
  3502. event_size = 2 * sizeof(u32);
  3503. else
  3504. event_size = 3 * sizeof(u32);
  3505. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  3506. /* "time" is actually "data" for mode 0 (no timestamp).
  3507. * place event id # at far right for easier visual parsing. */
  3508. for (i = 0; i < num_events; i++) {
  3509. ev = iwl3945_read_targ_mem(priv, ptr);
  3510. ptr += sizeof(u32);
  3511. time = iwl3945_read_targ_mem(priv, ptr);
  3512. ptr += sizeof(u32);
  3513. if (mode == 0)
  3514. IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
  3515. else {
  3516. data = iwl3945_read_targ_mem(priv, ptr);
  3517. ptr += sizeof(u32);
  3518. IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
  3519. }
  3520. }
  3521. }
  3522. static void iwl3945_dump_nic_event_log(struct iwl3945_priv *priv)
  3523. {
  3524. int rc;
  3525. u32 base; /* SRAM byte address of event log header */
  3526. u32 capacity; /* event log capacity in # entries */
  3527. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  3528. u32 num_wraps; /* # times uCode wrapped to top of log */
  3529. u32 next_entry; /* index of next entry to be written by uCode */
  3530. u32 size; /* # entries that we'll print */
  3531. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3532. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  3533. IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
  3534. return;
  3535. }
  3536. rc = iwl3945_grab_nic_access(priv);
  3537. if (rc) {
  3538. IWL_WARNING("Can not read from adapter at this time.\n");
  3539. return;
  3540. }
  3541. /* event log header */
  3542. capacity = iwl3945_read_targ_mem(priv, base);
  3543. mode = iwl3945_read_targ_mem(priv, base + (1 * sizeof(u32)));
  3544. num_wraps = iwl3945_read_targ_mem(priv, base + (2 * sizeof(u32)));
  3545. next_entry = iwl3945_read_targ_mem(priv, base + (3 * sizeof(u32)));
  3546. size = num_wraps ? capacity : next_entry;
  3547. /* bail out if nothing in log */
  3548. if (size == 0) {
  3549. IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
  3550. iwl3945_release_nic_access(priv);
  3551. return;
  3552. }
  3553. IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
  3554. size, num_wraps);
  3555. /* if uCode has wrapped back to top of log, start at the oldest entry,
  3556. * i.e the next one that uCode would fill. */
  3557. if (num_wraps)
  3558. iwl3945_print_event_log(priv, next_entry,
  3559. capacity - next_entry, mode);
  3560. /* (then/else) start at top of log */
  3561. iwl3945_print_event_log(priv, 0, next_entry, mode);
  3562. iwl3945_release_nic_access(priv);
  3563. }
  3564. /**
  3565. * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
  3566. */
  3567. static void iwl3945_irq_handle_error(struct iwl3945_priv *priv)
  3568. {
  3569. /* Set the FW error flag -- cleared on iwl3945_down */
  3570. set_bit(STATUS_FW_ERROR, &priv->status);
  3571. /* Cancel currently queued command. */
  3572. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  3573. #ifdef CONFIG_IWL3945_DEBUG
  3574. if (iwl3945_debug_level & IWL_DL_FW_ERRORS) {
  3575. iwl3945_dump_nic_error_log(priv);
  3576. iwl3945_dump_nic_event_log(priv);
  3577. iwl3945_print_rx_config_cmd(&priv->staging_rxon);
  3578. }
  3579. #endif
  3580. wake_up_interruptible(&priv->wait_command_queue);
  3581. /* Keep the restart process from trying to send host
  3582. * commands by clearing the INIT status bit */
  3583. clear_bit(STATUS_READY, &priv->status);
  3584. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3585. IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
  3586. "Restarting adapter due to uCode error.\n");
  3587. if (iwl3945_is_associated(priv)) {
  3588. memcpy(&priv->recovery_rxon, &priv->active_rxon,
  3589. sizeof(priv->recovery_rxon));
  3590. priv->error_recovering = 1;
  3591. }
  3592. queue_work(priv->workqueue, &priv->restart);
  3593. }
  3594. }
  3595. static void iwl3945_error_recovery(struct iwl3945_priv *priv)
  3596. {
  3597. unsigned long flags;
  3598. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  3599. sizeof(priv->staging_rxon));
  3600. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3601. iwl3945_commit_rxon(priv);
  3602. iwl3945_add_station(priv, priv->bssid, 1, 0);
  3603. spin_lock_irqsave(&priv->lock, flags);
  3604. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  3605. priv->error_recovering = 0;
  3606. spin_unlock_irqrestore(&priv->lock, flags);
  3607. }
  3608. static void iwl3945_irq_tasklet(struct iwl3945_priv *priv)
  3609. {
  3610. u32 inta, handled = 0;
  3611. u32 inta_fh;
  3612. unsigned long flags;
  3613. #ifdef CONFIG_IWL3945_DEBUG
  3614. u32 inta_mask;
  3615. #endif
  3616. spin_lock_irqsave(&priv->lock, flags);
  3617. /* Ack/clear/reset pending uCode interrupts.
  3618. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3619. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3620. inta = iwl3945_read32(priv, CSR_INT);
  3621. iwl3945_write32(priv, CSR_INT, inta);
  3622. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3623. * Any new interrupts that happen after this, either while we're
  3624. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3625. inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
  3626. iwl3945_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  3627. #ifdef CONFIG_IWL3945_DEBUG
  3628. if (iwl3945_debug_level & IWL_DL_ISR) {
  3629. /* just for debug */
  3630. inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
  3631. IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3632. inta, inta_mask, inta_fh);
  3633. }
  3634. #endif
  3635. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3636. * atomic, make sure that inta covers all the interrupts that
  3637. * we've discovered, even if FH interrupt came in just after
  3638. * reading CSR_INT. */
  3639. if (inta_fh & CSR39_FH_INT_RX_MASK)
  3640. inta |= CSR_INT_BIT_FH_RX;
  3641. if (inta_fh & CSR39_FH_INT_TX_MASK)
  3642. inta |= CSR_INT_BIT_FH_TX;
  3643. /* Now service all interrupt bits discovered above. */
  3644. if (inta & CSR_INT_BIT_HW_ERR) {
  3645. IWL_ERROR("Microcode HW error detected. Restarting.\n");
  3646. /* Tell the device to stop sending interrupts */
  3647. iwl3945_disable_interrupts(priv);
  3648. iwl3945_irq_handle_error(priv);
  3649. handled |= CSR_INT_BIT_HW_ERR;
  3650. spin_unlock_irqrestore(&priv->lock, flags);
  3651. return;
  3652. }
  3653. #ifdef CONFIG_IWL3945_DEBUG
  3654. if (iwl3945_debug_level & (IWL_DL_ISR)) {
  3655. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3656. if (inta & CSR_INT_BIT_SCD)
  3657. IWL_DEBUG_ISR("Scheduler finished to transmit "
  3658. "the frame/frames.\n");
  3659. /* Alive notification via Rx interrupt will do the real work */
  3660. if (inta & CSR_INT_BIT_ALIVE)
  3661. IWL_DEBUG_ISR("Alive interrupt\n");
  3662. }
  3663. #endif
  3664. /* Safely ignore these bits for debug checks below */
  3665. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  3666. /* HW RF KILL switch toggled (4965 only) */
  3667. if (inta & CSR_INT_BIT_RF_KILL) {
  3668. int hw_rf_kill = 0;
  3669. if (!(iwl3945_read32(priv, CSR_GP_CNTRL) &
  3670. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  3671. hw_rf_kill = 1;
  3672. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
  3673. "RF_KILL bit toggled to %s.\n",
  3674. hw_rf_kill ? "disable radio":"enable radio");
  3675. /* Queue restart only if RF_KILL switch was set to "kill"
  3676. * when we loaded driver, and is now set to "enable".
  3677. * After we're Alive, RF_KILL gets handled by
  3678. * iwl3945_rx_card_state_notif() */
  3679. if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
  3680. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3681. queue_work(priv->workqueue, &priv->restart);
  3682. }
  3683. handled |= CSR_INT_BIT_RF_KILL;
  3684. }
  3685. /* Chip got too hot and stopped itself (4965 only) */
  3686. if (inta & CSR_INT_BIT_CT_KILL) {
  3687. IWL_ERROR("Microcode CT kill error detected.\n");
  3688. handled |= CSR_INT_BIT_CT_KILL;
  3689. }
  3690. /* Error detected by uCode */
  3691. if (inta & CSR_INT_BIT_SW_ERR) {
  3692. IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
  3693. inta);
  3694. iwl3945_irq_handle_error(priv);
  3695. handled |= CSR_INT_BIT_SW_ERR;
  3696. }
  3697. /* uCode wakes up after power-down sleep */
  3698. if (inta & CSR_INT_BIT_WAKEUP) {
  3699. IWL_DEBUG_ISR("Wakeup interrupt\n");
  3700. iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
  3701. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
  3702. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
  3703. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
  3704. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
  3705. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
  3706. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
  3707. handled |= CSR_INT_BIT_WAKEUP;
  3708. }
  3709. /* All uCode command responses, including Tx command responses,
  3710. * Rx "responses" (frame-received notification), and other
  3711. * notifications from uCode come through here*/
  3712. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  3713. iwl3945_rx_handle(priv);
  3714. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  3715. }
  3716. if (inta & CSR_INT_BIT_FH_TX) {
  3717. IWL_DEBUG_ISR("Tx interrupt\n");
  3718. iwl3945_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  3719. if (!iwl3945_grab_nic_access(priv)) {
  3720. iwl3945_write_direct32(priv,
  3721. FH_TCSR_CREDIT
  3722. (ALM_FH_SRVC_CHNL), 0x0);
  3723. iwl3945_release_nic_access(priv);
  3724. }
  3725. handled |= CSR_INT_BIT_FH_TX;
  3726. }
  3727. if (inta & ~handled)
  3728. IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  3729. if (inta & ~CSR_INI_SET_MASK) {
  3730. IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
  3731. inta & ~CSR_INI_SET_MASK);
  3732. IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
  3733. }
  3734. /* Re-enable all interrupts */
  3735. /* only Re-enable if disabled by irq */
  3736. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3737. iwl3945_enable_interrupts(priv);
  3738. #ifdef CONFIG_IWL3945_DEBUG
  3739. if (iwl3945_debug_level & (IWL_DL_ISR)) {
  3740. inta = iwl3945_read32(priv, CSR_INT);
  3741. inta_mask = iwl3945_read32(priv, CSR_INT_MASK);
  3742. inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
  3743. IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  3744. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  3745. }
  3746. #endif
  3747. spin_unlock_irqrestore(&priv->lock, flags);
  3748. }
  3749. static irqreturn_t iwl3945_isr(int irq, void *data)
  3750. {
  3751. struct iwl3945_priv *priv = data;
  3752. u32 inta, inta_mask;
  3753. u32 inta_fh;
  3754. if (!priv)
  3755. return IRQ_NONE;
  3756. spin_lock(&priv->lock);
  3757. /* Disable (but don't clear!) interrupts here to avoid
  3758. * back-to-back ISRs and sporadic interrupts from our NIC.
  3759. * If we have something to service, the tasklet will re-enable ints.
  3760. * If we *don't* have something, we'll re-enable before leaving here. */
  3761. inta_mask = iwl3945_read32(priv, CSR_INT_MASK); /* just for debug */
  3762. iwl3945_write32(priv, CSR_INT_MASK, 0x00000000);
  3763. /* Discover which interrupts are active/pending */
  3764. inta = iwl3945_read32(priv, CSR_INT);
  3765. inta_fh = iwl3945_read32(priv, CSR_FH_INT_STATUS);
  3766. /* Ignore interrupt if there's nothing in NIC to service.
  3767. * This may be due to IRQ shared with another device,
  3768. * or due to sporadic interrupts thrown from our NIC. */
  3769. if (!inta && !inta_fh) {
  3770. IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  3771. goto none;
  3772. }
  3773. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  3774. /* Hardware disappeared */
  3775. IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
  3776. goto unplugged;
  3777. }
  3778. IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3779. inta, inta_mask, inta_fh);
  3780. inta &= ~CSR_INT_BIT_SCD;
  3781. /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
  3782. if (likely(inta || inta_fh))
  3783. tasklet_schedule(&priv->irq_tasklet);
  3784. unplugged:
  3785. spin_unlock(&priv->lock);
  3786. return IRQ_HANDLED;
  3787. none:
  3788. /* re-enable interrupts here since we don't have anything to service. */
  3789. /* only Re-enable if disabled by irq */
  3790. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3791. iwl3945_enable_interrupts(priv);
  3792. spin_unlock(&priv->lock);
  3793. return IRQ_NONE;
  3794. }
  3795. /************************** EEPROM BANDS ****************************
  3796. *
  3797. * The iwl3945_eeprom_band definitions below provide the mapping from the
  3798. * EEPROM contents to the specific channel number supported for each
  3799. * band.
  3800. *
  3801. * For example, iwl3945_priv->eeprom.band_3_channels[4] from the band_3
  3802. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  3803. * The specific geography and calibration information for that channel
  3804. * is contained in the eeprom map itself.
  3805. *
  3806. * During init, we copy the eeprom information and channel map
  3807. * information into priv->channel_info_24/52 and priv->channel_map_24/52
  3808. *
  3809. * channel_map_24/52 provides the index in the channel_info array for a
  3810. * given channel. We have to have two separate maps as there is channel
  3811. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  3812. * band_2
  3813. *
  3814. * A value of 0xff stored in the channel_map indicates that the channel
  3815. * is not supported by the hardware at all.
  3816. *
  3817. * A value of 0xfe in the channel_map indicates that the channel is not
  3818. * valid for Tx with the current hardware. This means that
  3819. * while the system can tune and receive on a given channel, it may not
  3820. * be able to associate or transmit any frames on that
  3821. * channel. There is no corresponding channel information for that
  3822. * entry.
  3823. *
  3824. *********************************************************************/
  3825. /* 2.4 GHz */
  3826. static const u8 iwl3945_eeprom_band_1[14] = {
  3827. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  3828. };
  3829. /* 5.2 GHz bands */
  3830. static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
  3831. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  3832. };
  3833. static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
  3834. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  3835. };
  3836. static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
  3837. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  3838. };
  3839. static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
  3840. 145, 149, 153, 157, 161, 165
  3841. };
  3842. static void iwl3945_init_band_reference(const struct iwl3945_priv *priv, int band,
  3843. int *eeprom_ch_count,
  3844. const struct iwl3945_eeprom_channel
  3845. **eeprom_ch_info,
  3846. const u8 **eeprom_ch_index)
  3847. {
  3848. switch (band) {
  3849. case 1: /* 2.4GHz band */
  3850. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
  3851. *eeprom_ch_info = priv->eeprom.band_1_channels;
  3852. *eeprom_ch_index = iwl3945_eeprom_band_1;
  3853. break;
  3854. case 2: /* 4.9GHz band */
  3855. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
  3856. *eeprom_ch_info = priv->eeprom.band_2_channels;
  3857. *eeprom_ch_index = iwl3945_eeprom_band_2;
  3858. break;
  3859. case 3: /* 5.2GHz band */
  3860. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
  3861. *eeprom_ch_info = priv->eeprom.band_3_channels;
  3862. *eeprom_ch_index = iwl3945_eeprom_band_3;
  3863. break;
  3864. case 4: /* 5.5GHz band */
  3865. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
  3866. *eeprom_ch_info = priv->eeprom.band_4_channels;
  3867. *eeprom_ch_index = iwl3945_eeprom_band_4;
  3868. break;
  3869. case 5: /* 5.7GHz band */
  3870. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
  3871. *eeprom_ch_info = priv->eeprom.band_5_channels;
  3872. *eeprom_ch_index = iwl3945_eeprom_band_5;
  3873. break;
  3874. default:
  3875. BUG();
  3876. return;
  3877. }
  3878. }
  3879. /**
  3880. * iwl3945_get_channel_info - Find driver's private channel info
  3881. *
  3882. * Based on band and channel number.
  3883. */
  3884. const struct iwl3945_channel_info *iwl3945_get_channel_info(const struct iwl3945_priv *priv,
  3885. enum ieee80211_band band, u16 channel)
  3886. {
  3887. int i;
  3888. switch (band) {
  3889. case IEEE80211_BAND_5GHZ:
  3890. for (i = 14; i < priv->channel_count; i++) {
  3891. if (priv->channel_info[i].channel == channel)
  3892. return &priv->channel_info[i];
  3893. }
  3894. break;
  3895. case IEEE80211_BAND_2GHZ:
  3896. if (channel >= 1 && channel <= 14)
  3897. return &priv->channel_info[channel - 1];
  3898. break;
  3899. case IEEE80211_NUM_BANDS:
  3900. WARN_ON(1);
  3901. }
  3902. return NULL;
  3903. }
  3904. #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  3905. ? # x " " : "")
  3906. /**
  3907. * iwl3945_init_channel_map - Set up driver's info for all possible channels
  3908. */
  3909. static int iwl3945_init_channel_map(struct iwl3945_priv *priv)
  3910. {
  3911. int eeprom_ch_count = 0;
  3912. const u8 *eeprom_ch_index = NULL;
  3913. const struct iwl3945_eeprom_channel *eeprom_ch_info = NULL;
  3914. int band, ch;
  3915. struct iwl3945_channel_info *ch_info;
  3916. if (priv->channel_count) {
  3917. IWL_DEBUG_INFO("Channel map already initialized.\n");
  3918. return 0;
  3919. }
  3920. if (priv->eeprom.version < 0x2f) {
  3921. IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
  3922. priv->eeprom.version);
  3923. return -EINVAL;
  3924. }
  3925. IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
  3926. priv->channel_count =
  3927. ARRAY_SIZE(iwl3945_eeprom_band_1) +
  3928. ARRAY_SIZE(iwl3945_eeprom_band_2) +
  3929. ARRAY_SIZE(iwl3945_eeprom_band_3) +
  3930. ARRAY_SIZE(iwl3945_eeprom_band_4) +
  3931. ARRAY_SIZE(iwl3945_eeprom_band_5);
  3932. IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
  3933. priv->channel_info = kzalloc(sizeof(struct iwl3945_channel_info) *
  3934. priv->channel_count, GFP_KERNEL);
  3935. if (!priv->channel_info) {
  3936. IWL_ERROR("Could not allocate channel_info\n");
  3937. priv->channel_count = 0;
  3938. return -ENOMEM;
  3939. }
  3940. ch_info = priv->channel_info;
  3941. /* Loop through the 5 EEPROM bands adding them in order to the
  3942. * channel map we maintain (that contains additional information than
  3943. * what just in the EEPROM) */
  3944. for (band = 1; band <= 5; band++) {
  3945. iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
  3946. &eeprom_ch_info, &eeprom_ch_index);
  3947. /* Loop through each band adding each of the channels */
  3948. for (ch = 0; ch < eeprom_ch_count; ch++) {
  3949. ch_info->channel = eeprom_ch_index[ch];
  3950. ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
  3951. IEEE80211_BAND_5GHZ;
  3952. /* permanently store EEPROM's channel regulatory flags
  3953. * and max power in channel info database. */
  3954. ch_info->eeprom = eeprom_ch_info[ch];
  3955. /* Copy the run-time flags so they are there even on
  3956. * invalid channels */
  3957. ch_info->flags = eeprom_ch_info[ch].flags;
  3958. if (!(is_channel_valid(ch_info))) {
  3959. IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
  3960. "No traffic\n",
  3961. ch_info->channel,
  3962. ch_info->flags,
  3963. is_channel_a_band(ch_info) ?
  3964. "5.2" : "2.4");
  3965. ch_info++;
  3966. continue;
  3967. }
  3968. /* Initialize regulatory-based run-time data */
  3969. ch_info->max_power_avg = ch_info->curr_txpow =
  3970. eeprom_ch_info[ch].max_power_avg;
  3971. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  3972. ch_info->min_power = 0;
  3973. IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
  3974. " %ddBm): Ad-Hoc %ssupported\n",
  3975. ch_info->channel,
  3976. is_channel_a_band(ch_info) ?
  3977. "5.2" : "2.4",
  3978. CHECK_AND_PRINT(VALID),
  3979. CHECK_AND_PRINT(IBSS),
  3980. CHECK_AND_PRINT(ACTIVE),
  3981. CHECK_AND_PRINT(RADAR),
  3982. CHECK_AND_PRINT(WIDE),
  3983. CHECK_AND_PRINT(DFS),
  3984. eeprom_ch_info[ch].flags,
  3985. eeprom_ch_info[ch].max_power_avg,
  3986. ((eeprom_ch_info[ch].
  3987. flags & EEPROM_CHANNEL_IBSS)
  3988. && !(eeprom_ch_info[ch].
  3989. flags & EEPROM_CHANNEL_RADAR))
  3990. ? "" : "not ");
  3991. /* Set the user_txpower_limit to the highest power
  3992. * supported by any channel */
  3993. if (eeprom_ch_info[ch].max_power_avg >
  3994. priv->user_txpower_limit)
  3995. priv->user_txpower_limit =
  3996. eeprom_ch_info[ch].max_power_avg;
  3997. ch_info++;
  3998. }
  3999. }
  4000. /* Set up txpower settings in driver for all channels */
  4001. if (iwl3945_txpower_set_from_eeprom(priv))
  4002. return -EIO;
  4003. return 0;
  4004. }
  4005. /*
  4006. * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
  4007. */
  4008. static void iwl3945_free_channel_map(struct iwl3945_priv *priv)
  4009. {
  4010. kfree(priv->channel_info);
  4011. priv->channel_count = 0;
  4012. }
  4013. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  4014. * sending probe req. This should be set long enough to hear probe responses
  4015. * from more than one AP. */
  4016. #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
  4017. #define IWL_ACTIVE_DWELL_TIME_52 (10)
  4018. /* For faster active scanning, scan will move to the next channel if fewer than
  4019. * PLCP_QUIET_THRESH packets are heard on this channel within
  4020. * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
  4021. * time if it's a quiet channel (nothing responded to our probe, and there's
  4022. * no other traffic).
  4023. * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
  4024. #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
  4025. #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
  4026. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  4027. * Must be set longer than active dwell time.
  4028. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  4029. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  4030. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  4031. #define IWL_PASSIVE_DWELL_BASE (100)
  4032. #define IWL_CHANNEL_TUNE_TIME 5
  4033. static inline u16 iwl3945_get_active_dwell_time(struct iwl3945_priv *priv,
  4034. enum ieee80211_band band)
  4035. {
  4036. if (band == IEEE80211_BAND_5GHZ)
  4037. return IWL_ACTIVE_DWELL_TIME_52;
  4038. else
  4039. return IWL_ACTIVE_DWELL_TIME_24;
  4040. }
  4041. static u16 iwl3945_get_passive_dwell_time(struct iwl3945_priv *priv,
  4042. enum ieee80211_band band)
  4043. {
  4044. u16 active = iwl3945_get_active_dwell_time(priv, band);
  4045. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  4046. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  4047. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  4048. if (iwl3945_is_associated(priv)) {
  4049. /* If we're associated, we clamp the maximum passive
  4050. * dwell time to be 98% of the beacon interval (minus
  4051. * 2 * channel tune time) */
  4052. passive = priv->beacon_int;
  4053. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  4054. passive = IWL_PASSIVE_DWELL_BASE;
  4055. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  4056. }
  4057. if (passive <= active)
  4058. passive = active + 1;
  4059. return passive;
  4060. }
  4061. static int iwl3945_get_channels_for_scan(struct iwl3945_priv *priv,
  4062. enum ieee80211_band band,
  4063. u8 is_active, u8 direct_mask,
  4064. struct iwl3945_scan_channel *scan_ch)
  4065. {
  4066. const struct ieee80211_channel *channels = NULL;
  4067. const struct ieee80211_supported_band *sband;
  4068. const struct iwl3945_channel_info *ch_info;
  4069. u16 passive_dwell = 0;
  4070. u16 active_dwell = 0;
  4071. int added, i;
  4072. sband = iwl3945_get_band(priv, band);
  4073. if (!sband)
  4074. return 0;
  4075. channels = sband->channels;
  4076. active_dwell = iwl3945_get_active_dwell_time(priv, band);
  4077. passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
  4078. for (i = 0, added = 0; i < sband->n_channels; i++) {
  4079. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  4080. continue;
  4081. scan_ch->channel = channels[i].hw_value;
  4082. ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
  4083. if (!is_channel_valid(ch_info)) {
  4084. IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
  4085. scan_ch->channel);
  4086. continue;
  4087. }
  4088. if (!is_active || is_channel_passive(ch_info) ||
  4089. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
  4090. scan_ch->type = 0; /* passive */
  4091. else
  4092. scan_ch->type = 1; /* active */
  4093. if (scan_ch->type & 1)
  4094. scan_ch->type |= (direct_mask << 1);
  4095. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  4096. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  4097. /* Set txpower levels to defaults */
  4098. scan_ch->tpc.dsp_atten = 110;
  4099. /* scan_pwr_info->tpc.dsp_atten; */
  4100. /*scan_pwr_info->tpc.tx_gain; */
  4101. if (band == IEEE80211_BAND_5GHZ)
  4102. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  4103. else {
  4104. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  4105. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  4106. * power level:
  4107. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  4108. */
  4109. }
  4110. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  4111. scan_ch->channel,
  4112. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  4113. (scan_ch->type & 1) ?
  4114. active_dwell : passive_dwell);
  4115. scan_ch++;
  4116. added++;
  4117. }
  4118. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  4119. return added;
  4120. }
  4121. static void iwl3945_init_hw_rates(struct iwl3945_priv *priv,
  4122. struct ieee80211_rate *rates)
  4123. {
  4124. int i;
  4125. for (i = 0; i < IWL_RATE_COUNT; i++) {
  4126. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  4127. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  4128. rates[i].hw_value_short = i;
  4129. rates[i].flags = 0;
  4130. if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  4131. /*
  4132. * If CCK != 1M then set short preamble rate flag.
  4133. */
  4134. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  4135. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  4136. }
  4137. }
  4138. }
  4139. /**
  4140. * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
  4141. */
  4142. static int iwl3945_init_geos(struct iwl3945_priv *priv)
  4143. {
  4144. struct iwl3945_channel_info *ch;
  4145. struct ieee80211_supported_band *sband;
  4146. struct ieee80211_channel *channels;
  4147. struct ieee80211_channel *geo_ch;
  4148. struct ieee80211_rate *rates;
  4149. int i = 0;
  4150. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  4151. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  4152. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  4153. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  4154. return 0;
  4155. }
  4156. channels = kzalloc(sizeof(struct ieee80211_channel) *
  4157. priv->channel_count, GFP_KERNEL);
  4158. if (!channels)
  4159. return -ENOMEM;
  4160. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
  4161. GFP_KERNEL);
  4162. if (!rates) {
  4163. kfree(channels);
  4164. return -ENOMEM;
  4165. }
  4166. /* 5.2GHz channels start after the 2.4GHz channels */
  4167. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  4168. sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
  4169. /* just OFDM */
  4170. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  4171. sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
  4172. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  4173. sband->channels = channels;
  4174. /* OFDM & CCK */
  4175. sband->bitrates = rates;
  4176. sband->n_bitrates = IWL_RATE_COUNT;
  4177. priv->ieee_channels = channels;
  4178. priv->ieee_rates = rates;
  4179. iwl3945_init_hw_rates(priv, rates);
  4180. for (i = 0; i < priv->channel_count; i++) {
  4181. ch = &priv->channel_info[i];
  4182. /* FIXME: might be removed if scan is OK*/
  4183. if (!is_channel_valid(ch))
  4184. continue;
  4185. if (is_channel_a_band(ch))
  4186. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  4187. else
  4188. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  4189. geo_ch = &sband->channels[sband->n_channels++];
  4190. geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
  4191. geo_ch->max_power = ch->max_power_avg;
  4192. geo_ch->max_antenna_gain = 0xff;
  4193. geo_ch->hw_value = ch->channel;
  4194. if (is_channel_valid(ch)) {
  4195. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  4196. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  4197. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  4198. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  4199. if (ch->flags & EEPROM_CHANNEL_RADAR)
  4200. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  4201. if (ch->max_power_avg > priv->max_channel_txpower_limit)
  4202. priv->max_channel_txpower_limit =
  4203. ch->max_power_avg;
  4204. } else {
  4205. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  4206. }
  4207. /* Save flags for reg domain usage */
  4208. geo_ch->orig_flags = geo_ch->flags;
  4209. IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
  4210. ch->channel, geo_ch->center_freq,
  4211. is_channel_a_band(ch) ? "5.2" : "2.4",
  4212. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  4213. "restricted" : "valid",
  4214. geo_ch->flags);
  4215. }
  4216. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  4217. priv->cfg->sku & IWL_SKU_A) {
  4218. printk(KERN_INFO DRV_NAME
  4219. ": Incorrectly detected BG card as ABG. Please send "
  4220. "your PCI ID 0x%04X:0x%04X to maintainer.\n",
  4221. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  4222. priv->cfg->sku &= ~IWL_SKU_A;
  4223. }
  4224. printk(KERN_INFO DRV_NAME
  4225. ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  4226. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  4227. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  4228. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  4229. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  4230. &priv->bands[IEEE80211_BAND_2GHZ];
  4231. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  4232. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  4233. &priv->bands[IEEE80211_BAND_5GHZ];
  4234. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  4235. return 0;
  4236. }
  4237. /*
  4238. * iwl3945_free_geos - undo allocations in iwl3945_init_geos
  4239. */
  4240. static void iwl3945_free_geos(struct iwl3945_priv *priv)
  4241. {
  4242. kfree(priv->ieee_channels);
  4243. kfree(priv->ieee_rates);
  4244. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  4245. }
  4246. /******************************************************************************
  4247. *
  4248. * uCode download functions
  4249. *
  4250. ******************************************************************************/
  4251. static void iwl3945_dealloc_ucode_pci(struct iwl3945_priv *priv)
  4252. {
  4253. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  4254. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  4255. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  4256. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  4257. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  4258. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  4259. }
  4260. /**
  4261. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  4262. * looking at all data.
  4263. */
  4264. static int iwl3945_verify_inst_full(struct iwl3945_priv *priv, __le32 *image, u32 len)
  4265. {
  4266. u32 val;
  4267. u32 save_len = len;
  4268. int rc = 0;
  4269. u32 errcnt;
  4270. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  4271. rc = iwl3945_grab_nic_access(priv);
  4272. if (rc)
  4273. return rc;
  4274. iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
  4275. errcnt = 0;
  4276. for (; len > 0; len -= sizeof(u32), image++) {
  4277. /* read data comes through single port, auto-incr addr */
  4278. /* NOTE: Use the debugless read so we don't flood kernel log
  4279. * if IWL_DL_IO is set */
  4280. val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  4281. if (val != le32_to_cpu(*image)) {
  4282. IWL_ERROR("uCode INST section is invalid at "
  4283. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  4284. save_len - len, val, le32_to_cpu(*image));
  4285. rc = -EIO;
  4286. errcnt++;
  4287. if (errcnt >= 20)
  4288. break;
  4289. }
  4290. }
  4291. iwl3945_release_nic_access(priv);
  4292. if (!errcnt)
  4293. IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
  4294. return rc;
  4295. }
  4296. /**
  4297. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  4298. * using sample data 100 bytes apart. If these sample points are good,
  4299. * it's a pretty good bet that everything between them is good, too.
  4300. */
  4301. static int iwl3945_verify_inst_sparse(struct iwl3945_priv *priv, __le32 *image, u32 len)
  4302. {
  4303. u32 val;
  4304. int rc = 0;
  4305. u32 errcnt = 0;
  4306. u32 i;
  4307. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  4308. rc = iwl3945_grab_nic_access(priv);
  4309. if (rc)
  4310. return rc;
  4311. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  4312. /* read data comes through single port, auto-incr addr */
  4313. /* NOTE: Use the debugless read so we don't flood kernel log
  4314. * if IWL_DL_IO is set */
  4315. iwl3945_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  4316. i + RTC_INST_LOWER_BOUND);
  4317. val = _iwl3945_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  4318. if (val != le32_to_cpu(*image)) {
  4319. #if 0 /* Enable this if you want to see details */
  4320. IWL_ERROR("uCode INST section is invalid at "
  4321. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  4322. i, val, *image);
  4323. #endif
  4324. rc = -EIO;
  4325. errcnt++;
  4326. if (errcnt >= 3)
  4327. break;
  4328. }
  4329. }
  4330. iwl3945_release_nic_access(priv);
  4331. return rc;
  4332. }
  4333. /**
  4334. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  4335. * and verify its contents
  4336. */
  4337. static int iwl3945_verify_ucode(struct iwl3945_priv *priv)
  4338. {
  4339. __le32 *image;
  4340. u32 len;
  4341. int rc = 0;
  4342. /* Try bootstrap */
  4343. image = (__le32 *)priv->ucode_boot.v_addr;
  4344. len = priv->ucode_boot.len;
  4345. rc = iwl3945_verify_inst_sparse(priv, image, len);
  4346. if (rc == 0) {
  4347. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  4348. return 0;
  4349. }
  4350. /* Try initialize */
  4351. image = (__le32 *)priv->ucode_init.v_addr;
  4352. len = priv->ucode_init.len;
  4353. rc = iwl3945_verify_inst_sparse(priv, image, len);
  4354. if (rc == 0) {
  4355. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  4356. return 0;
  4357. }
  4358. /* Try runtime/protocol */
  4359. image = (__le32 *)priv->ucode_code.v_addr;
  4360. len = priv->ucode_code.len;
  4361. rc = iwl3945_verify_inst_sparse(priv, image, len);
  4362. if (rc == 0) {
  4363. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  4364. return 0;
  4365. }
  4366. IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  4367. /* Since nothing seems to match, show first several data entries in
  4368. * instruction SRAM, so maybe visual inspection will give a clue.
  4369. * Selection of bootstrap image (vs. other images) is arbitrary. */
  4370. image = (__le32 *)priv->ucode_boot.v_addr;
  4371. len = priv->ucode_boot.len;
  4372. rc = iwl3945_verify_inst_full(priv, image, len);
  4373. return rc;
  4374. }
  4375. /* check contents of special bootstrap uCode SRAM */
  4376. static int iwl3945_verify_bsm(struct iwl3945_priv *priv)
  4377. {
  4378. __le32 *image = priv->ucode_boot.v_addr;
  4379. u32 len = priv->ucode_boot.len;
  4380. u32 reg;
  4381. u32 val;
  4382. IWL_DEBUG_INFO("Begin verify bsm\n");
  4383. /* verify BSM SRAM contents */
  4384. val = iwl3945_read_prph(priv, BSM_WR_DWCOUNT_REG);
  4385. for (reg = BSM_SRAM_LOWER_BOUND;
  4386. reg < BSM_SRAM_LOWER_BOUND + len;
  4387. reg += sizeof(u32), image++) {
  4388. val = iwl3945_read_prph(priv, reg);
  4389. if (val != le32_to_cpu(*image)) {
  4390. IWL_ERROR("BSM uCode verification failed at "
  4391. "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
  4392. BSM_SRAM_LOWER_BOUND,
  4393. reg - BSM_SRAM_LOWER_BOUND, len,
  4394. val, le32_to_cpu(*image));
  4395. return -EIO;
  4396. }
  4397. }
  4398. IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
  4399. return 0;
  4400. }
  4401. /**
  4402. * iwl3945_load_bsm - Load bootstrap instructions
  4403. *
  4404. * BSM operation:
  4405. *
  4406. * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
  4407. * in special SRAM that does not power down during RFKILL. When powering back
  4408. * up after power-saving sleeps (or during initial uCode load), the BSM loads
  4409. * the bootstrap program into the on-board processor, and starts it.
  4410. *
  4411. * The bootstrap program loads (via DMA) instructions and data for a new
  4412. * program from host DRAM locations indicated by the host driver in the
  4413. * BSM_DRAM_* registers. Once the new program is loaded, it starts
  4414. * automatically.
  4415. *
  4416. * When initializing the NIC, the host driver points the BSM to the
  4417. * "initialize" uCode image. This uCode sets up some internal data, then
  4418. * notifies host via "initialize alive" that it is complete.
  4419. *
  4420. * The host then replaces the BSM_DRAM_* pointer values to point to the
  4421. * normal runtime uCode instructions and a backup uCode data cache buffer
  4422. * (filled initially with starting data values for the on-board processor),
  4423. * then triggers the "initialize" uCode to load and launch the runtime uCode,
  4424. * which begins normal operation.
  4425. *
  4426. * When doing a power-save shutdown, runtime uCode saves data SRAM into
  4427. * the backup data cache in DRAM before SRAM is powered down.
  4428. *
  4429. * When powering back up, the BSM loads the bootstrap program. This reloads
  4430. * the runtime uCode instructions and the backup data cache into SRAM,
  4431. * and re-launches the runtime uCode from where it left off.
  4432. */
  4433. static int iwl3945_load_bsm(struct iwl3945_priv *priv)
  4434. {
  4435. __le32 *image = priv->ucode_boot.v_addr;
  4436. u32 len = priv->ucode_boot.len;
  4437. dma_addr_t pinst;
  4438. dma_addr_t pdata;
  4439. u32 inst_len;
  4440. u32 data_len;
  4441. int rc;
  4442. int i;
  4443. u32 done;
  4444. u32 reg_offset;
  4445. IWL_DEBUG_INFO("Begin load bsm\n");
  4446. /* make sure bootstrap program is no larger than BSM's SRAM size */
  4447. if (len > IWL_MAX_BSM_SIZE)
  4448. return -EINVAL;
  4449. /* Tell bootstrap uCode where to find the "Initialize" uCode
  4450. * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
  4451. * NOTE: iwl3945_initialize_alive_start() will replace these values,
  4452. * after the "initialize" uCode has run, to point to
  4453. * runtime/protocol instructions and backup data cache. */
  4454. pinst = priv->ucode_init.p_addr;
  4455. pdata = priv->ucode_init_data.p_addr;
  4456. inst_len = priv->ucode_init.len;
  4457. data_len = priv->ucode_init_data.len;
  4458. rc = iwl3945_grab_nic_access(priv);
  4459. if (rc)
  4460. return rc;
  4461. iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  4462. iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  4463. iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
  4464. iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
  4465. /* Fill BSM memory with bootstrap instructions */
  4466. for (reg_offset = BSM_SRAM_LOWER_BOUND;
  4467. reg_offset < BSM_SRAM_LOWER_BOUND + len;
  4468. reg_offset += sizeof(u32), image++)
  4469. _iwl3945_write_prph(priv, reg_offset,
  4470. le32_to_cpu(*image));
  4471. rc = iwl3945_verify_bsm(priv);
  4472. if (rc) {
  4473. iwl3945_release_nic_access(priv);
  4474. return rc;
  4475. }
  4476. /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
  4477. iwl3945_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
  4478. iwl3945_write_prph(priv, BSM_WR_MEM_DST_REG,
  4479. RTC_INST_LOWER_BOUND);
  4480. iwl3945_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
  4481. /* Load bootstrap code into instruction SRAM now,
  4482. * to prepare to load "initialize" uCode */
  4483. iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
  4484. BSM_WR_CTRL_REG_BIT_START);
  4485. /* Wait for load of bootstrap uCode to finish */
  4486. for (i = 0; i < 100; i++) {
  4487. done = iwl3945_read_prph(priv, BSM_WR_CTRL_REG);
  4488. if (!(done & BSM_WR_CTRL_REG_BIT_START))
  4489. break;
  4490. udelay(10);
  4491. }
  4492. if (i < 100)
  4493. IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
  4494. else {
  4495. IWL_ERROR("BSM write did not complete!\n");
  4496. return -EIO;
  4497. }
  4498. /* Enable future boot loads whenever power management unit triggers it
  4499. * (e.g. when powering back up after power-save shutdown) */
  4500. iwl3945_write_prph(priv, BSM_WR_CTRL_REG,
  4501. BSM_WR_CTRL_REG_BIT_START_EN);
  4502. iwl3945_release_nic_access(priv);
  4503. return 0;
  4504. }
  4505. static void iwl3945_nic_start(struct iwl3945_priv *priv)
  4506. {
  4507. /* Remove all resets to allow NIC to operate */
  4508. iwl3945_write32(priv, CSR_RESET, 0);
  4509. }
  4510. /**
  4511. * iwl3945_read_ucode - Read uCode images from disk file.
  4512. *
  4513. * Copy into buffers for card to fetch via bus-mastering
  4514. */
  4515. static int iwl3945_read_ucode(struct iwl3945_priv *priv)
  4516. {
  4517. struct iwl3945_ucode *ucode;
  4518. int ret = 0;
  4519. const struct firmware *ucode_raw;
  4520. /* firmware file name contains uCode/driver compatibility version */
  4521. const char *name = priv->cfg->fw_name;
  4522. u8 *src;
  4523. size_t len;
  4524. u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
  4525. /* Ask kernel firmware_class module to get the boot firmware off disk.
  4526. * request_firmware() is synchronous, file is in memory on return. */
  4527. ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
  4528. if (ret < 0) {
  4529. IWL_ERROR("%s firmware file req failed: Reason %d\n",
  4530. name, ret);
  4531. goto error;
  4532. }
  4533. IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
  4534. name, ucode_raw->size);
  4535. /* Make sure that we got at least our header! */
  4536. if (ucode_raw->size < sizeof(*ucode)) {
  4537. IWL_ERROR("File size way too small!\n");
  4538. ret = -EINVAL;
  4539. goto err_release;
  4540. }
  4541. /* Data from ucode file: header followed by uCode images */
  4542. ucode = (void *)ucode_raw->data;
  4543. ver = le32_to_cpu(ucode->ver);
  4544. inst_size = le32_to_cpu(ucode->inst_size);
  4545. data_size = le32_to_cpu(ucode->data_size);
  4546. init_size = le32_to_cpu(ucode->init_size);
  4547. init_data_size = le32_to_cpu(ucode->init_data_size);
  4548. boot_size = le32_to_cpu(ucode->boot_size);
  4549. IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
  4550. IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
  4551. IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
  4552. IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
  4553. IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
  4554. IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
  4555. /* Verify size of file vs. image size info in file's header */
  4556. if (ucode_raw->size < sizeof(*ucode) +
  4557. inst_size + data_size + init_size +
  4558. init_data_size + boot_size) {
  4559. IWL_DEBUG_INFO("uCode file size %d too small\n",
  4560. (int)ucode_raw->size);
  4561. ret = -EINVAL;
  4562. goto err_release;
  4563. }
  4564. /* Verify that uCode images will fit in card's SRAM */
  4565. if (inst_size > IWL_MAX_INST_SIZE) {
  4566. IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
  4567. inst_size);
  4568. ret = -EINVAL;
  4569. goto err_release;
  4570. }
  4571. if (data_size > IWL_MAX_DATA_SIZE) {
  4572. IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
  4573. data_size);
  4574. ret = -EINVAL;
  4575. goto err_release;
  4576. }
  4577. if (init_size > IWL_MAX_INST_SIZE) {
  4578. IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
  4579. init_size);
  4580. ret = -EINVAL;
  4581. goto err_release;
  4582. }
  4583. if (init_data_size > IWL_MAX_DATA_SIZE) {
  4584. IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
  4585. init_data_size);
  4586. ret = -EINVAL;
  4587. goto err_release;
  4588. }
  4589. if (boot_size > IWL_MAX_BSM_SIZE) {
  4590. IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
  4591. boot_size);
  4592. ret = -EINVAL;
  4593. goto err_release;
  4594. }
  4595. /* Allocate ucode buffers for card's bus-master loading ... */
  4596. /* Runtime instructions and 2 copies of data:
  4597. * 1) unmodified from disk
  4598. * 2) backup cache for save/restore during power-downs */
  4599. priv->ucode_code.len = inst_size;
  4600. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  4601. priv->ucode_data.len = data_size;
  4602. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  4603. priv->ucode_data_backup.len = data_size;
  4604. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  4605. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  4606. !priv->ucode_data_backup.v_addr)
  4607. goto err_pci_alloc;
  4608. /* Initialization instructions and data */
  4609. if (init_size && init_data_size) {
  4610. priv->ucode_init.len = init_size;
  4611. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  4612. priv->ucode_init_data.len = init_data_size;
  4613. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  4614. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  4615. goto err_pci_alloc;
  4616. }
  4617. /* Bootstrap (instructions only, no data) */
  4618. if (boot_size) {
  4619. priv->ucode_boot.len = boot_size;
  4620. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  4621. if (!priv->ucode_boot.v_addr)
  4622. goto err_pci_alloc;
  4623. }
  4624. /* Copy images into buffers for card's bus-master reads ... */
  4625. /* Runtime instructions (first block of data in file) */
  4626. src = &ucode->data[0];
  4627. len = priv->ucode_code.len;
  4628. IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
  4629. memcpy(priv->ucode_code.v_addr, src, len);
  4630. IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  4631. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  4632. /* Runtime data (2nd block)
  4633. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  4634. src = &ucode->data[inst_size];
  4635. len = priv->ucode_data.len;
  4636. IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
  4637. memcpy(priv->ucode_data.v_addr, src, len);
  4638. memcpy(priv->ucode_data_backup.v_addr, src, len);
  4639. /* Initialization instructions (3rd block) */
  4640. if (init_size) {
  4641. src = &ucode->data[inst_size + data_size];
  4642. len = priv->ucode_init.len;
  4643. IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
  4644. len);
  4645. memcpy(priv->ucode_init.v_addr, src, len);
  4646. }
  4647. /* Initialization data (4th block) */
  4648. if (init_data_size) {
  4649. src = &ucode->data[inst_size + data_size + init_size];
  4650. len = priv->ucode_init_data.len;
  4651. IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
  4652. (int)len);
  4653. memcpy(priv->ucode_init_data.v_addr, src, len);
  4654. }
  4655. /* Bootstrap instructions (5th block) */
  4656. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  4657. len = priv->ucode_boot.len;
  4658. IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
  4659. (int)len);
  4660. memcpy(priv->ucode_boot.v_addr, src, len);
  4661. /* We have our copies now, allow OS release its copies */
  4662. release_firmware(ucode_raw);
  4663. return 0;
  4664. err_pci_alloc:
  4665. IWL_ERROR("failed to allocate pci memory\n");
  4666. ret = -ENOMEM;
  4667. iwl3945_dealloc_ucode_pci(priv);
  4668. err_release:
  4669. release_firmware(ucode_raw);
  4670. error:
  4671. return ret;
  4672. }
  4673. /**
  4674. * iwl3945_set_ucode_ptrs - Set uCode address location
  4675. *
  4676. * Tell initialization uCode where to find runtime uCode.
  4677. *
  4678. * BSM registers initially contain pointers to initialization uCode.
  4679. * We need to replace them to load runtime uCode inst and data,
  4680. * and to save runtime data when powering down.
  4681. */
  4682. static int iwl3945_set_ucode_ptrs(struct iwl3945_priv *priv)
  4683. {
  4684. dma_addr_t pinst;
  4685. dma_addr_t pdata;
  4686. int rc = 0;
  4687. unsigned long flags;
  4688. /* bits 31:0 for 3945 */
  4689. pinst = priv->ucode_code.p_addr;
  4690. pdata = priv->ucode_data_backup.p_addr;
  4691. spin_lock_irqsave(&priv->lock, flags);
  4692. rc = iwl3945_grab_nic_access(priv);
  4693. if (rc) {
  4694. spin_unlock_irqrestore(&priv->lock, flags);
  4695. return rc;
  4696. }
  4697. /* Tell bootstrap uCode where to find image to load */
  4698. iwl3945_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  4699. iwl3945_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  4700. iwl3945_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  4701. priv->ucode_data.len);
  4702. /* Inst bytecount must be last to set up, bit 31 signals uCode
  4703. * that all new ptr/size info is in place */
  4704. iwl3945_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  4705. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  4706. iwl3945_release_nic_access(priv);
  4707. spin_unlock_irqrestore(&priv->lock, flags);
  4708. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  4709. return rc;
  4710. }
  4711. /**
  4712. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  4713. *
  4714. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  4715. *
  4716. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  4717. */
  4718. static void iwl3945_init_alive_start(struct iwl3945_priv *priv)
  4719. {
  4720. /* Check alive response for "valid" sign from uCode */
  4721. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  4722. /* We had an error bringing up the hardware, so take it
  4723. * all the way back down so we can try again */
  4724. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  4725. goto restart;
  4726. }
  4727. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  4728. * This is a paranoid check, because we would not have gotten the
  4729. * "initialize" alive if code weren't properly loaded. */
  4730. if (iwl3945_verify_ucode(priv)) {
  4731. /* Runtime instruction load was bad;
  4732. * take it all the way back down so we can try again */
  4733. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  4734. goto restart;
  4735. }
  4736. /* Send pointers to protocol/runtime uCode image ... init code will
  4737. * load and launch runtime uCode, which will send us another "Alive"
  4738. * notification. */
  4739. IWL_DEBUG_INFO("Initialization Alive received.\n");
  4740. if (iwl3945_set_ucode_ptrs(priv)) {
  4741. /* Runtime instruction load won't happen;
  4742. * take it all the way back down so we can try again */
  4743. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  4744. goto restart;
  4745. }
  4746. return;
  4747. restart:
  4748. queue_work(priv->workqueue, &priv->restart);
  4749. }
  4750. /**
  4751. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  4752. * from protocol/runtime uCode (initialization uCode's
  4753. * Alive gets handled by iwl3945_init_alive_start()).
  4754. */
  4755. static void iwl3945_alive_start(struct iwl3945_priv *priv)
  4756. {
  4757. int rc = 0;
  4758. int thermal_spin = 0;
  4759. u32 rfkill;
  4760. IWL_DEBUG_INFO("Runtime Alive received.\n");
  4761. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  4762. /* We had an error bringing up the hardware, so take it
  4763. * all the way back down so we can try again */
  4764. IWL_DEBUG_INFO("Alive failed.\n");
  4765. goto restart;
  4766. }
  4767. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  4768. * This is a paranoid check, because we would not have gotten the
  4769. * "runtime" alive if code weren't properly loaded. */
  4770. if (iwl3945_verify_ucode(priv)) {
  4771. /* Runtime instruction load was bad;
  4772. * take it all the way back down so we can try again */
  4773. IWL_DEBUG_INFO("Bad runtime uCode load.\n");
  4774. goto restart;
  4775. }
  4776. iwl3945_clear_stations_table(priv);
  4777. rc = iwl3945_grab_nic_access(priv);
  4778. if (rc) {
  4779. IWL_WARNING("Can not read rfkill status from adapter\n");
  4780. return;
  4781. }
  4782. rfkill = iwl3945_read_prph(priv, APMG_RFKILL_REG);
  4783. IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
  4784. iwl3945_release_nic_access(priv);
  4785. if (rfkill & 0x1) {
  4786. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  4787. /* if rfkill is not on, then wait for thermal
  4788. * sensor in adapter to kick in */
  4789. while (iwl3945_hw_get_temperature(priv) == 0) {
  4790. thermal_spin++;
  4791. udelay(10);
  4792. }
  4793. if (thermal_spin)
  4794. IWL_DEBUG_INFO("Thermal calibration took %dus\n",
  4795. thermal_spin * 10);
  4796. } else
  4797. set_bit(STATUS_RF_KILL_HW, &priv->status);
  4798. /* After the ALIVE response, we can send commands to 3945 uCode */
  4799. set_bit(STATUS_ALIVE, &priv->status);
  4800. /* Clear out the uCode error bit if it is set */
  4801. clear_bit(STATUS_FW_ERROR, &priv->status);
  4802. if (iwl3945_is_rfkill(priv))
  4803. return;
  4804. ieee80211_wake_queues(priv->hw);
  4805. priv->active_rate = priv->rates_mask;
  4806. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  4807. iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
  4808. if (iwl3945_is_associated(priv)) {
  4809. struct iwl3945_rxon_cmd *active_rxon =
  4810. (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
  4811. memcpy(&priv->staging_rxon, &priv->active_rxon,
  4812. sizeof(priv->staging_rxon));
  4813. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4814. } else {
  4815. /* Initialize our rx_config data */
  4816. iwl3945_connection_init_rx_config(priv);
  4817. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  4818. }
  4819. /* Configure Bluetooth device coexistence support */
  4820. iwl3945_send_bt_config(priv);
  4821. /* Configure the adapter for unassociated operation */
  4822. iwl3945_commit_rxon(priv);
  4823. iwl3945_reg_txpower_periodic(priv);
  4824. iwl3945_led_register(priv);
  4825. IWL_DEBUG_INFO("ALIVE processing complete.\n");
  4826. set_bit(STATUS_READY, &priv->status);
  4827. wake_up_interruptible(&priv->wait_command_queue);
  4828. if (priv->error_recovering)
  4829. iwl3945_error_recovery(priv);
  4830. ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
  4831. return;
  4832. restart:
  4833. queue_work(priv->workqueue, &priv->restart);
  4834. }
  4835. static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv);
  4836. static void __iwl3945_down(struct iwl3945_priv *priv)
  4837. {
  4838. unsigned long flags;
  4839. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  4840. struct ieee80211_conf *conf = NULL;
  4841. IWL_DEBUG_INFO(DRV_NAME " is going down\n");
  4842. conf = ieee80211_get_hw_conf(priv->hw);
  4843. if (!exit_pending)
  4844. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4845. iwl3945_led_unregister(priv);
  4846. iwl3945_clear_stations_table(priv);
  4847. /* Unblock any waiting calls */
  4848. wake_up_interruptible_all(&priv->wait_command_queue);
  4849. /* Wipe out the EXIT_PENDING status bit if we are not actually
  4850. * exiting the module */
  4851. if (!exit_pending)
  4852. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  4853. /* stop and reset the on-board processor */
  4854. iwl3945_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4855. /* tell the device to stop sending interrupts */
  4856. spin_lock_irqsave(&priv->lock, flags);
  4857. iwl3945_disable_interrupts(priv);
  4858. spin_unlock_irqrestore(&priv->lock, flags);
  4859. iwl_synchronize_irq(priv);
  4860. if (priv->mac80211_registered)
  4861. ieee80211_stop_queues(priv->hw);
  4862. /* If we have not previously called iwl3945_init() then
  4863. * clear all bits but the RF Kill and SUSPEND bits and return */
  4864. if (!iwl3945_is_init(priv)) {
  4865. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4866. STATUS_RF_KILL_HW |
  4867. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4868. STATUS_RF_KILL_SW |
  4869. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4870. STATUS_GEO_CONFIGURED |
  4871. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4872. STATUS_IN_SUSPEND |
  4873. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  4874. STATUS_EXIT_PENDING;
  4875. goto exit;
  4876. }
  4877. /* ...otherwise clear out all the status bits but the RF Kill and
  4878. * SUSPEND bits and continue taking the NIC down. */
  4879. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4880. STATUS_RF_KILL_HW |
  4881. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4882. STATUS_RF_KILL_SW |
  4883. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4884. STATUS_GEO_CONFIGURED |
  4885. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4886. STATUS_IN_SUSPEND |
  4887. test_bit(STATUS_FW_ERROR, &priv->status) <<
  4888. STATUS_FW_ERROR |
  4889. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  4890. STATUS_EXIT_PENDING;
  4891. spin_lock_irqsave(&priv->lock, flags);
  4892. iwl3945_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  4893. spin_unlock_irqrestore(&priv->lock, flags);
  4894. iwl3945_hw_txq_ctx_stop(priv);
  4895. iwl3945_hw_rxq_stop(priv);
  4896. spin_lock_irqsave(&priv->lock, flags);
  4897. if (!iwl3945_grab_nic_access(priv)) {
  4898. iwl3945_write_prph(priv, APMG_CLK_DIS_REG,
  4899. APMG_CLK_VAL_DMA_CLK_RQT);
  4900. iwl3945_release_nic_access(priv);
  4901. }
  4902. spin_unlock_irqrestore(&priv->lock, flags);
  4903. udelay(5);
  4904. iwl3945_hw_nic_stop_master(priv);
  4905. iwl3945_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  4906. iwl3945_hw_nic_reset(priv);
  4907. exit:
  4908. memset(&priv->card_alive, 0, sizeof(struct iwl3945_alive_resp));
  4909. if (priv->ibss_beacon)
  4910. dev_kfree_skb(priv->ibss_beacon);
  4911. priv->ibss_beacon = NULL;
  4912. /* clear out any free frames */
  4913. iwl3945_clear_free_frames(priv);
  4914. }
  4915. static void iwl3945_down(struct iwl3945_priv *priv)
  4916. {
  4917. mutex_lock(&priv->mutex);
  4918. __iwl3945_down(priv);
  4919. mutex_unlock(&priv->mutex);
  4920. iwl3945_cancel_deferred_work(priv);
  4921. }
  4922. #define MAX_HW_RESTARTS 5
  4923. static int __iwl3945_up(struct iwl3945_priv *priv)
  4924. {
  4925. int rc, i;
  4926. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  4927. IWL_WARNING("Exit pending; will not bring the NIC up\n");
  4928. return -EIO;
  4929. }
  4930. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  4931. IWL_WARNING("Radio disabled by SW RF kill (module "
  4932. "parameter)\n");
  4933. return -ENODEV;
  4934. }
  4935. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  4936. IWL_ERROR("ucode not available for device bringup\n");
  4937. return -EIO;
  4938. }
  4939. /* If platform's RF_KILL switch is NOT set to KILL */
  4940. if (iwl3945_read32(priv, CSR_GP_CNTRL) &
  4941. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4942. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  4943. else {
  4944. set_bit(STATUS_RF_KILL_HW, &priv->status);
  4945. if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
  4946. IWL_WARNING("Radio disabled by HW RF Kill switch\n");
  4947. return -ENODEV;
  4948. }
  4949. }
  4950. iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
  4951. rc = iwl3945_hw_nic_init(priv);
  4952. if (rc) {
  4953. IWL_ERROR("Unable to int nic\n");
  4954. return rc;
  4955. }
  4956. /* make sure rfkill handshake bits are cleared */
  4957. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4958. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  4959. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  4960. /* clear (again), then enable host interrupts */
  4961. iwl3945_write32(priv, CSR_INT, 0xFFFFFFFF);
  4962. iwl3945_enable_interrupts(priv);
  4963. /* really make sure rfkill handshake bits are cleared */
  4964. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4965. iwl3945_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4966. /* Copy original ucode data image from disk into backup cache.
  4967. * This will be used to initialize the on-board processor's
  4968. * data SRAM for a clean start when the runtime program first loads. */
  4969. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  4970. priv->ucode_data.len);
  4971. /* We return success when we resume from suspend and rf_kill is on. */
  4972. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  4973. return 0;
  4974. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  4975. iwl3945_clear_stations_table(priv);
  4976. /* load bootstrap state machine,
  4977. * load bootstrap program into processor's memory,
  4978. * prepare to load the "initialize" uCode */
  4979. rc = iwl3945_load_bsm(priv);
  4980. if (rc) {
  4981. IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
  4982. continue;
  4983. }
  4984. /* start card; "initialize" will load runtime ucode */
  4985. iwl3945_nic_start(priv);
  4986. IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
  4987. return 0;
  4988. }
  4989. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4990. __iwl3945_down(priv);
  4991. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  4992. /* tried to restart and config the device for as long as our
  4993. * patience could withstand */
  4994. IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
  4995. return -EIO;
  4996. }
  4997. /*****************************************************************************
  4998. *
  4999. * Workqueue callbacks
  5000. *
  5001. *****************************************************************************/
  5002. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  5003. {
  5004. struct iwl3945_priv *priv =
  5005. container_of(data, struct iwl3945_priv, init_alive_start.work);
  5006. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5007. return;
  5008. mutex_lock(&priv->mutex);
  5009. iwl3945_init_alive_start(priv);
  5010. mutex_unlock(&priv->mutex);
  5011. }
  5012. static void iwl3945_bg_alive_start(struct work_struct *data)
  5013. {
  5014. struct iwl3945_priv *priv =
  5015. container_of(data, struct iwl3945_priv, alive_start.work);
  5016. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5017. return;
  5018. mutex_lock(&priv->mutex);
  5019. iwl3945_alive_start(priv);
  5020. mutex_unlock(&priv->mutex);
  5021. }
  5022. static void iwl3945_bg_rf_kill(struct work_struct *work)
  5023. {
  5024. struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, rf_kill);
  5025. wake_up_interruptible(&priv->wait_command_queue);
  5026. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5027. return;
  5028. mutex_lock(&priv->mutex);
  5029. if (!iwl3945_is_rfkill(priv)) {
  5030. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
  5031. "HW and/or SW RF Kill no longer active, restarting "
  5032. "device\n");
  5033. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  5034. queue_work(priv->workqueue, &priv->restart);
  5035. } else {
  5036. if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
  5037. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  5038. "disabled by SW switch\n");
  5039. else
  5040. IWL_WARNING("Radio Frequency Kill Switch is On:\n"
  5041. "Kill switch must be turned off for "
  5042. "wireless networking to work.\n");
  5043. }
  5044. mutex_unlock(&priv->mutex);
  5045. iwl3945_rfkill_set_hw_state(priv);
  5046. }
  5047. static void iwl3945_bg_set_monitor(struct work_struct *work)
  5048. {
  5049. struct iwl3945_priv *priv = container_of(work,
  5050. struct iwl3945_priv, set_monitor);
  5051. IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
  5052. mutex_lock(&priv->mutex);
  5053. if (!iwl3945_is_ready(priv))
  5054. IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
  5055. else
  5056. if (iwl3945_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
  5057. IWL_ERROR("iwl3945_set_mode() failed\n");
  5058. mutex_unlock(&priv->mutex);
  5059. }
  5060. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  5061. static void iwl3945_bg_scan_check(struct work_struct *data)
  5062. {
  5063. struct iwl3945_priv *priv =
  5064. container_of(data, struct iwl3945_priv, scan_check.work);
  5065. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5066. return;
  5067. mutex_lock(&priv->mutex);
  5068. if (test_bit(STATUS_SCANNING, &priv->status) ||
  5069. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  5070. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
  5071. "Scan completion watchdog resetting adapter (%dms)\n",
  5072. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  5073. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  5074. iwl3945_send_scan_abort(priv);
  5075. }
  5076. mutex_unlock(&priv->mutex);
  5077. }
  5078. static void iwl3945_bg_request_scan(struct work_struct *data)
  5079. {
  5080. struct iwl3945_priv *priv =
  5081. container_of(data, struct iwl3945_priv, request_scan);
  5082. struct iwl3945_host_cmd cmd = {
  5083. .id = REPLY_SCAN_CMD,
  5084. .len = sizeof(struct iwl3945_scan_cmd),
  5085. .meta.flags = CMD_SIZE_HUGE,
  5086. };
  5087. int rc = 0;
  5088. struct iwl3945_scan_cmd *scan;
  5089. struct ieee80211_conf *conf = NULL;
  5090. u8 direct_mask;
  5091. enum ieee80211_band band;
  5092. conf = ieee80211_get_hw_conf(priv->hw);
  5093. mutex_lock(&priv->mutex);
  5094. if (!iwl3945_is_ready(priv)) {
  5095. IWL_WARNING("request scan called when driver not ready.\n");
  5096. goto done;
  5097. }
  5098. /* Make sure the scan wasn't cancelled before this queued work
  5099. * was given the chance to run... */
  5100. if (!test_bit(STATUS_SCANNING, &priv->status))
  5101. goto done;
  5102. /* This should never be called or scheduled if there is currently
  5103. * a scan active in the hardware. */
  5104. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  5105. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  5106. "Ignoring second request.\n");
  5107. rc = -EIO;
  5108. goto done;
  5109. }
  5110. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  5111. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  5112. goto done;
  5113. }
  5114. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  5115. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  5116. goto done;
  5117. }
  5118. if (iwl3945_is_rfkill(priv)) {
  5119. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  5120. goto done;
  5121. }
  5122. if (!test_bit(STATUS_READY, &priv->status)) {
  5123. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  5124. goto done;
  5125. }
  5126. if (!priv->scan_bands) {
  5127. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  5128. goto done;
  5129. }
  5130. if (!priv->scan) {
  5131. priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  5132. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  5133. if (!priv->scan) {
  5134. rc = -ENOMEM;
  5135. goto done;
  5136. }
  5137. }
  5138. scan = priv->scan;
  5139. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  5140. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  5141. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  5142. if (iwl3945_is_associated(priv)) {
  5143. u16 interval = 0;
  5144. u32 extra;
  5145. u32 suspend_time = 100;
  5146. u32 scan_suspend_time = 100;
  5147. unsigned long flags;
  5148. IWL_DEBUG_INFO("Scanning while associated...\n");
  5149. spin_lock_irqsave(&priv->lock, flags);
  5150. interval = priv->beacon_int;
  5151. spin_unlock_irqrestore(&priv->lock, flags);
  5152. scan->suspend_time = 0;
  5153. scan->max_out_time = cpu_to_le32(200 * 1024);
  5154. if (!interval)
  5155. interval = suspend_time;
  5156. /*
  5157. * suspend time format:
  5158. * 0-19: beacon interval in usec (time before exec.)
  5159. * 20-23: 0
  5160. * 24-31: number of beacons (suspend between channels)
  5161. */
  5162. extra = (suspend_time / interval) << 24;
  5163. scan_suspend_time = 0xFF0FFFFF &
  5164. (extra | ((suspend_time % interval) * 1024));
  5165. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  5166. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  5167. scan_suspend_time, interval);
  5168. }
  5169. /* We should add the ability for user to lock to PASSIVE ONLY */
  5170. if (priv->one_direct_scan) {
  5171. IWL_DEBUG_SCAN
  5172. ("Kicking off one direct scan for '%s'\n",
  5173. iwl3945_escape_essid(priv->direct_ssid,
  5174. priv->direct_ssid_len));
  5175. scan->direct_scan[0].id = WLAN_EID_SSID;
  5176. scan->direct_scan[0].len = priv->direct_ssid_len;
  5177. memcpy(scan->direct_scan[0].ssid,
  5178. priv->direct_ssid, priv->direct_ssid_len);
  5179. direct_mask = 1;
  5180. } else if (!iwl3945_is_associated(priv) && priv->essid_len) {
  5181. IWL_DEBUG_SCAN
  5182. ("Kicking off one direct scan for '%s' when not associated\n",
  5183. iwl3945_escape_essid(priv->essid, priv->essid_len));
  5184. scan->direct_scan[0].id = WLAN_EID_SSID;
  5185. scan->direct_scan[0].len = priv->essid_len;
  5186. memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
  5187. direct_mask = 1;
  5188. } else {
  5189. IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
  5190. direct_mask = 0;
  5191. }
  5192. /* We don't build a direct scan probe request; the uCode will do
  5193. * that based on the direct_mask added to each channel entry */
  5194. scan->tx_cmd.len = cpu_to_le16(
  5195. iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
  5196. IWL_MAX_SCAN_SIZE - sizeof(*scan), 0));
  5197. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  5198. scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
  5199. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  5200. /* flags + rate selection */
  5201. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  5202. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  5203. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  5204. scan->good_CRC_th = 0;
  5205. band = IEEE80211_BAND_2GHZ;
  5206. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  5207. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  5208. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  5209. band = IEEE80211_BAND_5GHZ;
  5210. } else {
  5211. IWL_WARNING("Invalid scan band count\n");
  5212. goto done;
  5213. }
  5214. /* select Rx antennas */
  5215. scan->flags |= iwl3945_get_antenna_flags(priv);
  5216. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
  5217. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  5218. if (direct_mask)
  5219. scan->channel_count =
  5220. iwl3945_get_channels_for_scan(
  5221. priv, band, 1, /* active */
  5222. direct_mask,
  5223. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  5224. else
  5225. scan->channel_count =
  5226. iwl3945_get_channels_for_scan(
  5227. priv, band, 0, /* passive */
  5228. direct_mask,
  5229. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  5230. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  5231. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  5232. cmd.data = scan;
  5233. scan->len = cpu_to_le16(cmd.len);
  5234. set_bit(STATUS_SCAN_HW, &priv->status);
  5235. rc = iwl3945_send_cmd_sync(priv, &cmd);
  5236. if (rc)
  5237. goto done;
  5238. queue_delayed_work(priv->workqueue, &priv->scan_check,
  5239. IWL_SCAN_CHECK_WATCHDOG);
  5240. mutex_unlock(&priv->mutex);
  5241. return;
  5242. done:
  5243. /* inform mac80211 scan aborted */
  5244. queue_work(priv->workqueue, &priv->scan_completed);
  5245. mutex_unlock(&priv->mutex);
  5246. }
  5247. static void iwl3945_bg_up(struct work_struct *data)
  5248. {
  5249. struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, up);
  5250. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5251. return;
  5252. mutex_lock(&priv->mutex);
  5253. __iwl3945_up(priv);
  5254. mutex_unlock(&priv->mutex);
  5255. iwl3945_rfkill_set_hw_state(priv);
  5256. }
  5257. static void iwl3945_bg_restart(struct work_struct *data)
  5258. {
  5259. struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv, restart);
  5260. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5261. return;
  5262. iwl3945_down(priv);
  5263. queue_work(priv->workqueue, &priv->up);
  5264. }
  5265. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  5266. {
  5267. struct iwl3945_priv *priv =
  5268. container_of(data, struct iwl3945_priv, rx_replenish);
  5269. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5270. return;
  5271. mutex_lock(&priv->mutex);
  5272. iwl3945_rx_replenish(priv);
  5273. mutex_unlock(&priv->mutex);
  5274. }
  5275. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  5276. static void iwl3945_bg_post_associate(struct work_struct *data)
  5277. {
  5278. struct iwl3945_priv *priv = container_of(data, struct iwl3945_priv,
  5279. post_associate.work);
  5280. int rc = 0;
  5281. struct ieee80211_conf *conf = NULL;
  5282. DECLARE_MAC_BUF(mac);
  5283. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  5284. IWL_ERROR("%s Should not be called in AP mode\n", __func__);
  5285. return;
  5286. }
  5287. IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
  5288. priv->assoc_id,
  5289. print_mac(mac, priv->active_rxon.bssid_addr));
  5290. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5291. return;
  5292. mutex_lock(&priv->mutex);
  5293. if (!priv->vif || !priv->is_open) {
  5294. mutex_unlock(&priv->mutex);
  5295. return;
  5296. }
  5297. iwl3945_scan_cancel_timeout(priv, 200);
  5298. conf = ieee80211_get_hw_conf(priv->hw);
  5299. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5300. iwl3945_commit_rxon(priv);
  5301. memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
  5302. iwl3945_setup_rxon_timing(priv);
  5303. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5304. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5305. if (rc)
  5306. IWL_WARNING("REPLY_RXON_TIMING failed - "
  5307. "Attempting to continue.\n");
  5308. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5309. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5310. IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
  5311. priv->assoc_id, priv->beacon_int);
  5312. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5313. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  5314. else
  5315. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5316. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5317. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5318. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  5319. else
  5320. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  5321. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  5322. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  5323. }
  5324. iwl3945_commit_rxon(priv);
  5325. switch (priv->iw_mode) {
  5326. case IEEE80211_IF_TYPE_STA:
  5327. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  5328. break;
  5329. case IEEE80211_IF_TYPE_IBSS:
  5330. /* clear out the station table */
  5331. iwl3945_clear_stations_table(priv);
  5332. iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
  5333. iwl3945_add_station(priv, priv->bssid, 0, 0);
  5334. iwl3945_sync_sta(priv, IWL_STA_ID,
  5335. (priv->band == IEEE80211_BAND_5GHZ) ?
  5336. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  5337. CMD_ASYNC);
  5338. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  5339. iwl3945_send_beacon_cmd(priv);
  5340. break;
  5341. default:
  5342. IWL_ERROR("%s Should not be called in %d mode\n",
  5343. __func__, priv->iw_mode);
  5344. break;
  5345. }
  5346. iwl3945_activate_qos(priv, 0);
  5347. /* we have just associated, don't start scan too early */
  5348. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  5349. mutex_unlock(&priv->mutex);
  5350. }
  5351. static void iwl3945_bg_abort_scan(struct work_struct *work)
  5352. {
  5353. struct iwl3945_priv *priv = container_of(work, struct iwl3945_priv, abort_scan);
  5354. if (!iwl3945_is_ready(priv))
  5355. return;
  5356. mutex_lock(&priv->mutex);
  5357. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  5358. iwl3945_send_scan_abort(priv);
  5359. mutex_unlock(&priv->mutex);
  5360. }
  5361. static int iwl3945_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
  5362. static void iwl3945_bg_scan_completed(struct work_struct *work)
  5363. {
  5364. struct iwl3945_priv *priv =
  5365. container_of(work, struct iwl3945_priv, scan_completed);
  5366. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
  5367. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5368. return;
  5369. if (test_bit(STATUS_CONF_PENDING, &priv->status))
  5370. iwl3945_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
  5371. ieee80211_scan_completed(priv->hw);
  5372. /* Since setting the TXPOWER may have been deferred while
  5373. * performing the scan, fire one off */
  5374. mutex_lock(&priv->mutex);
  5375. iwl3945_hw_reg_send_txpower(priv);
  5376. mutex_unlock(&priv->mutex);
  5377. }
  5378. /*****************************************************************************
  5379. *
  5380. * mac80211 entry point functions
  5381. *
  5382. *****************************************************************************/
  5383. #define UCODE_READY_TIMEOUT (2 * HZ)
  5384. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  5385. {
  5386. struct iwl3945_priv *priv = hw->priv;
  5387. int ret;
  5388. IWL_DEBUG_MAC80211("enter\n");
  5389. if (pci_enable_device(priv->pci_dev)) {
  5390. IWL_ERROR("Fail to pci_enable_device\n");
  5391. return -ENODEV;
  5392. }
  5393. pci_restore_state(priv->pci_dev);
  5394. pci_enable_msi(priv->pci_dev);
  5395. ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
  5396. DRV_NAME, priv);
  5397. if (ret) {
  5398. IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
  5399. goto out_disable_msi;
  5400. }
  5401. /* we should be verifying the device is ready to be opened */
  5402. mutex_lock(&priv->mutex);
  5403. memset(&priv->staging_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
  5404. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  5405. * ucode filename and max sizes are card-specific. */
  5406. if (!priv->ucode_code.len) {
  5407. ret = iwl3945_read_ucode(priv);
  5408. if (ret) {
  5409. IWL_ERROR("Could not read microcode: %d\n", ret);
  5410. mutex_unlock(&priv->mutex);
  5411. goto out_release_irq;
  5412. }
  5413. }
  5414. ret = __iwl3945_up(priv);
  5415. mutex_unlock(&priv->mutex);
  5416. iwl3945_rfkill_set_hw_state(priv);
  5417. if (ret)
  5418. goto out_release_irq;
  5419. IWL_DEBUG_INFO("Start UP work.\n");
  5420. if (test_bit(STATUS_IN_SUSPEND, &priv->status))
  5421. return 0;
  5422. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  5423. * mac80211 will not be run successfully. */
  5424. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  5425. test_bit(STATUS_READY, &priv->status),
  5426. UCODE_READY_TIMEOUT);
  5427. if (!ret) {
  5428. if (!test_bit(STATUS_READY, &priv->status)) {
  5429. IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
  5430. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  5431. ret = -ETIMEDOUT;
  5432. goto out_release_irq;
  5433. }
  5434. }
  5435. priv->is_open = 1;
  5436. IWL_DEBUG_MAC80211("leave\n");
  5437. return 0;
  5438. out_release_irq:
  5439. free_irq(priv->pci_dev->irq, priv);
  5440. out_disable_msi:
  5441. pci_disable_msi(priv->pci_dev);
  5442. pci_disable_device(priv->pci_dev);
  5443. priv->is_open = 0;
  5444. IWL_DEBUG_MAC80211("leave - failed\n");
  5445. return ret;
  5446. }
  5447. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  5448. {
  5449. struct iwl3945_priv *priv = hw->priv;
  5450. IWL_DEBUG_MAC80211("enter\n");
  5451. if (!priv->is_open) {
  5452. IWL_DEBUG_MAC80211("leave - skip\n");
  5453. return;
  5454. }
  5455. priv->is_open = 0;
  5456. if (iwl3945_is_ready_rf(priv)) {
  5457. /* stop mac, cancel any scan request and clear
  5458. * RXON_FILTER_ASSOC_MSK BIT
  5459. */
  5460. mutex_lock(&priv->mutex);
  5461. iwl3945_scan_cancel_timeout(priv, 100);
  5462. cancel_delayed_work(&priv->post_associate);
  5463. mutex_unlock(&priv->mutex);
  5464. }
  5465. iwl3945_down(priv);
  5466. flush_workqueue(priv->workqueue);
  5467. free_irq(priv->pci_dev->irq, priv);
  5468. pci_disable_msi(priv->pci_dev);
  5469. pci_save_state(priv->pci_dev);
  5470. pci_disable_device(priv->pci_dev);
  5471. IWL_DEBUG_MAC80211("leave\n");
  5472. }
  5473. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  5474. {
  5475. struct iwl3945_priv *priv = hw->priv;
  5476. IWL_DEBUG_MAC80211("enter\n");
  5477. IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  5478. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  5479. if (iwl3945_tx_skb(priv, skb))
  5480. dev_kfree_skb_any(skb);
  5481. IWL_DEBUG_MAC80211("leave\n");
  5482. return 0;
  5483. }
  5484. static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
  5485. struct ieee80211_if_init_conf *conf)
  5486. {
  5487. struct iwl3945_priv *priv = hw->priv;
  5488. unsigned long flags;
  5489. DECLARE_MAC_BUF(mac);
  5490. IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
  5491. if (priv->vif) {
  5492. IWL_DEBUG_MAC80211("leave - vif != NULL\n");
  5493. return -EOPNOTSUPP;
  5494. }
  5495. spin_lock_irqsave(&priv->lock, flags);
  5496. priv->vif = conf->vif;
  5497. spin_unlock_irqrestore(&priv->lock, flags);
  5498. mutex_lock(&priv->mutex);
  5499. if (conf->mac_addr) {
  5500. IWL_DEBUG_MAC80211("Set: %s\n", print_mac(mac, conf->mac_addr));
  5501. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  5502. }
  5503. if (iwl3945_is_ready(priv))
  5504. iwl3945_set_mode(priv, conf->type);
  5505. mutex_unlock(&priv->mutex);
  5506. IWL_DEBUG_MAC80211("leave\n");
  5507. return 0;
  5508. }
  5509. /**
  5510. * iwl3945_mac_config - mac80211 config callback
  5511. *
  5512. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  5513. * be set inappropriately and the driver currently sets the hardware up to
  5514. * use it whenever needed.
  5515. */
  5516. static int iwl3945_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  5517. {
  5518. struct iwl3945_priv *priv = hw->priv;
  5519. const struct iwl3945_channel_info *ch_info;
  5520. unsigned long flags;
  5521. int ret = 0;
  5522. mutex_lock(&priv->mutex);
  5523. IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
  5524. if (!iwl3945_is_ready(priv)) {
  5525. IWL_DEBUG_MAC80211("leave - not ready\n");
  5526. ret = -EIO;
  5527. goto out;
  5528. }
  5529. if (unlikely(!iwl3945_param_disable_hw_scan &&
  5530. test_bit(STATUS_SCANNING, &priv->status))) {
  5531. IWL_DEBUG_MAC80211("leave - scanning\n");
  5532. set_bit(STATUS_CONF_PENDING, &priv->status);
  5533. mutex_unlock(&priv->mutex);
  5534. return 0;
  5535. }
  5536. spin_lock_irqsave(&priv->lock, flags);
  5537. ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
  5538. conf->channel->hw_value);
  5539. if (!is_channel_valid(ch_info)) {
  5540. IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
  5541. conf->channel->hw_value, conf->channel->band);
  5542. IWL_DEBUG_MAC80211("leave - invalid channel\n");
  5543. spin_unlock_irqrestore(&priv->lock, flags);
  5544. ret = -EINVAL;
  5545. goto out;
  5546. }
  5547. iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
  5548. iwl3945_set_flags_for_phymode(priv, conf->channel->band);
  5549. /* The list of supported rates and rate mask can be different
  5550. * for each phymode; since the phymode may have changed, reset
  5551. * the rate mask to what mac80211 lists */
  5552. iwl3945_set_rate(priv);
  5553. spin_unlock_irqrestore(&priv->lock, flags);
  5554. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  5555. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  5556. iwl3945_hw_channel_switch(priv, conf->channel);
  5557. goto out;
  5558. }
  5559. #endif
  5560. iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
  5561. if (!conf->radio_enabled) {
  5562. IWL_DEBUG_MAC80211("leave - radio disabled\n");
  5563. goto out;
  5564. }
  5565. if (iwl3945_is_rfkill(priv)) {
  5566. IWL_DEBUG_MAC80211("leave - RF kill\n");
  5567. ret = -EIO;
  5568. goto out;
  5569. }
  5570. iwl3945_set_rate(priv);
  5571. if (memcmp(&priv->active_rxon,
  5572. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  5573. iwl3945_commit_rxon(priv);
  5574. else
  5575. IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
  5576. IWL_DEBUG_MAC80211("leave\n");
  5577. out:
  5578. clear_bit(STATUS_CONF_PENDING, &priv->status);
  5579. mutex_unlock(&priv->mutex);
  5580. return ret;
  5581. }
  5582. static void iwl3945_config_ap(struct iwl3945_priv *priv)
  5583. {
  5584. int rc = 0;
  5585. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5586. return;
  5587. /* The following should be done only at AP bring up */
  5588. if (!(iwl3945_is_associated(priv))) {
  5589. /* RXON - unassoc (to set timing command) */
  5590. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5591. iwl3945_commit_rxon(priv);
  5592. /* RXON Timing */
  5593. memset(&priv->rxon_timing, 0, sizeof(struct iwl3945_rxon_time_cmd));
  5594. iwl3945_setup_rxon_timing(priv);
  5595. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5596. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5597. if (rc)
  5598. IWL_WARNING("REPLY_RXON_TIMING failed - "
  5599. "Attempting to continue.\n");
  5600. /* FIXME: what should be the assoc_id for AP? */
  5601. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5602. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5603. priv->staging_rxon.flags |=
  5604. RXON_FLG_SHORT_PREAMBLE_MSK;
  5605. else
  5606. priv->staging_rxon.flags &=
  5607. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5608. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5609. if (priv->assoc_capability &
  5610. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5611. priv->staging_rxon.flags |=
  5612. RXON_FLG_SHORT_SLOT_MSK;
  5613. else
  5614. priv->staging_rxon.flags &=
  5615. ~RXON_FLG_SHORT_SLOT_MSK;
  5616. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  5617. priv->staging_rxon.flags &=
  5618. ~RXON_FLG_SHORT_SLOT_MSK;
  5619. }
  5620. /* restore RXON assoc */
  5621. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5622. iwl3945_commit_rxon(priv);
  5623. iwl3945_add_station(priv, iwl3945_broadcast_addr, 0, 0);
  5624. }
  5625. iwl3945_send_beacon_cmd(priv);
  5626. /* FIXME - we need to add code here to detect a totally new
  5627. * configuration, reset the AP, unassoc, rxon timing, assoc,
  5628. * clear sta table, add BCAST sta... */
  5629. }
  5630. /* temporary */
  5631. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb);
  5632. static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
  5633. struct ieee80211_vif *vif,
  5634. struct ieee80211_if_conf *conf)
  5635. {
  5636. struct iwl3945_priv *priv = hw->priv;
  5637. DECLARE_MAC_BUF(mac);
  5638. unsigned long flags;
  5639. int rc;
  5640. if (conf == NULL)
  5641. return -EIO;
  5642. if (priv->vif != vif) {
  5643. IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
  5644. return 0;
  5645. }
  5646. /* handle this temporarily here */
  5647. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
  5648. conf->changed & IEEE80211_IFCC_BEACON) {
  5649. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  5650. if (!beacon)
  5651. return -ENOMEM;
  5652. rc = iwl3945_mac_beacon_update(hw, beacon);
  5653. if (rc)
  5654. return rc;
  5655. }
  5656. /* XXX: this MUST use conf->mac_addr */
  5657. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  5658. (!conf->ssid_len)) {
  5659. IWL_DEBUG_MAC80211
  5660. ("Leaving in AP mode because HostAPD is not ready.\n");
  5661. return 0;
  5662. }
  5663. if (!iwl3945_is_alive(priv))
  5664. return -EAGAIN;
  5665. mutex_lock(&priv->mutex);
  5666. if (conf->bssid)
  5667. IWL_DEBUG_MAC80211("bssid: %s\n",
  5668. print_mac(mac, conf->bssid));
  5669. /*
  5670. * very dubious code was here; the probe filtering flag is never set:
  5671. *
  5672. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  5673. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  5674. */
  5675. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  5676. if (!conf->bssid) {
  5677. conf->bssid = priv->mac_addr;
  5678. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  5679. IWL_DEBUG_MAC80211("bssid was set to: %s\n",
  5680. print_mac(mac, conf->bssid));
  5681. }
  5682. if (priv->ibss_beacon)
  5683. dev_kfree_skb(priv->ibss_beacon);
  5684. priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
  5685. }
  5686. if (iwl3945_is_rfkill(priv))
  5687. goto done;
  5688. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  5689. !is_multicast_ether_addr(conf->bssid)) {
  5690. /* If there is currently a HW scan going on in the background
  5691. * then we need to cancel it else the RXON below will fail. */
  5692. if (iwl3945_scan_cancel_timeout(priv, 100)) {
  5693. IWL_WARNING("Aborted scan still in progress "
  5694. "after 100ms\n");
  5695. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  5696. mutex_unlock(&priv->mutex);
  5697. return -EAGAIN;
  5698. }
  5699. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  5700. /* TODO: Audit driver for usage of these members and see
  5701. * if mac80211 deprecates them (priv->bssid looks like it
  5702. * shouldn't be there, but I haven't scanned the IBSS code
  5703. * to verify) - jpk */
  5704. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  5705. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  5706. iwl3945_config_ap(priv);
  5707. else {
  5708. rc = iwl3945_commit_rxon(priv);
  5709. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
  5710. iwl3945_add_station(priv,
  5711. priv->active_rxon.bssid_addr, 1, 0);
  5712. }
  5713. } else {
  5714. iwl3945_scan_cancel_timeout(priv, 100);
  5715. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5716. iwl3945_commit_rxon(priv);
  5717. }
  5718. done:
  5719. spin_lock_irqsave(&priv->lock, flags);
  5720. if (!conf->ssid_len)
  5721. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  5722. else
  5723. memcpy(priv->essid, conf->ssid, conf->ssid_len);
  5724. priv->essid_len = conf->ssid_len;
  5725. spin_unlock_irqrestore(&priv->lock, flags);
  5726. IWL_DEBUG_MAC80211("leave\n");
  5727. mutex_unlock(&priv->mutex);
  5728. return 0;
  5729. }
  5730. static void iwl3945_configure_filter(struct ieee80211_hw *hw,
  5731. unsigned int changed_flags,
  5732. unsigned int *total_flags,
  5733. int mc_count, struct dev_addr_list *mc_list)
  5734. {
  5735. struct iwl3945_priv *priv = hw->priv;
  5736. if (changed_flags & (*total_flags) & FIF_OTHER_BSS) {
  5737. IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
  5738. IEEE80211_IF_TYPE_MNTR,
  5739. changed_flags, *total_flags);
  5740. /* queue work 'cuz mac80211 is holding a lock which
  5741. * prevents us from issuing (synchronous) f/w cmds */
  5742. queue_work(priv->workqueue, &priv->set_monitor);
  5743. }
  5744. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI |
  5745. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  5746. }
  5747. static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
  5748. struct ieee80211_if_init_conf *conf)
  5749. {
  5750. struct iwl3945_priv *priv = hw->priv;
  5751. IWL_DEBUG_MAC80211("enter\n");
  5752. mutex_lock(&priv->mutex);
  5753. if (iwl3945_is_ready_rf(priv)) {
  5754. iwl3945_scan_cancel_timeout(priv, 100);
  5755. cancel_delayed_work(&priv->post_associate);
  5756. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5757. iwl3945_commit_rxon(priv);
  5758. }
  5759. if (priv->vif == conf->vif) {
  5760. priv->vif = NULL;
  5761. memset(priv->bssid, 0, ETH_ALEN);
  5762. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  5763. priv->essid_len = 0;
  5764. }
  5765. mutex_unlock(&priv->mutex);
  5766. IWL_DEBUG_MAC80211("leave\n");
  5767. }
  5768. static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
  5769. {
  5770. int rc = 0;
  5771. unsigned long flags;
  5772. struct iwl3945_priv *priv = hw->priv;
  5773. IWL_DEBUG_MAC80211("enter\n");
  5774. mutex_lock(&priv->mutex);
  5775. spin_lock_irqsave(&priv->lock, flags);
  5776. if (!iwl3945_is_ready_rf(priv)) {
  5777. rc = -EIO;
  5778. IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
  5779. goto out_unlock;
  5780. }
  5781. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
  5782. rc = -EIO;
  5783. IWL_ERROR("ERROR: APs don't scan\n");
  5784. goto out_unlock;
  5785. }
  5786. /* we don't schedule scan within next_scan_jiffies period */
  5787. if (priv->next_scan_jiffies &&
  5788. time_after(priv->next_scan_jiffies, jiffies)) {
  5789. rc = -EAGAIN;
  5790. goto out_unlock;
  5791. }
  5792. /* if we just finished scan ask for delay for a broadcast scan */
  5793. if ((len == 0) && priv->last_scan_jiffies &&
  5794. time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
  5795. jiffies)) {
  5796. rc = -EAGAIN;
  5797. goto out_unlock;
  5798. }
  5799. if (len) {
  5800. IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
  5801. iwl3945_escape_essid(ssid, len), (int)len);
  5802. priv->one_direct_scan = 1;
  5803. priv->direct_ssid_len = (u8)
  5804. min((u8) len, (u8) IW_ESSID_MAX_SIZE);
  5805. memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
  5806. } else
  5807. priv->one_direct_scan = 0;
  5808. rc = iwl3945_scan_initiate(priv);
  5809. IWL_DEBUG_MAC80211("leave\n");
  5810. out_unlock:
  5811. spin_unlock_irqrestore(&priv->lock, flags);
  5812. mutex_unlock(&priv->mutex);
  5813. return rc;
  5814. }
  5815. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  5816. const u8 *local_addr, const u8 *addr,
  5817. struct ieee80211_key_conf *key)
  5818. {
  5819. struct iwl3945_priv *priv = hw->priv;
  5820. int rc = 0;
  5821. u8 sta_id;
  5822. IWL_DEBUG_MAC80211("enter\n");
  5823. if (!iwl3945_param_hwcrypto) {
  5824. IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
  5825. return -EOPNOTSUPP;
  5826. }
  5827. if (is_zero_ether_addr(addr))
  5828. /* only support pairwise keys */
  5829. return -EOPNOTSUPP;
  5830. sta_id = iwl3945_hw_find_station(priv, addr);
  5831. if (sta_id == IWL_INVALID_STATION) {
  5832. DECLARE_MAC_BUF(mac);
  5833. IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
  5834. print_mac(mac, addr));
  5835. return -EINVAL;
  5836. }
  5837. mutex_lock(&priv->mutex);
  5838. iwl3945_scan_cancel_timeout(priv, 100);
  5839. switch (cmd) {
  5840. case SET_KEY:
  5841. rc = iwl3945_update_sta_key_info(priv, key, sta_id);
  5842. if (!rc) {
  5843. iwl3945_set_rxon_hwcrypto(priv, 1);
  5844. iwl3945_commit_rxon(priv);
  5845. key->hw_key_idx = sta_id;
  5846. IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
  5847. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  5848. }
  5849. break;
  5850. case DISABLE_KEY:
  5851. rc = iwl3945_clear_sta_key_info(priv, sta_id);
  5852. if (!rc) {
  5853. iwl3945_set_rxon_hwcrypto(priv, 0);
  5854. iwl3945_commit_rxon(priv);
  5855. IWL_DEBUG_MAC80211("disable hwcrypto key\n");
  5856. }
  5857. break;
  5858. default:
  5859. rc = -EINVAL;
  5860. }
  5861. IWL_DEBUG_MAC80211("leave\n");
  5862. mutex_unlock(&priv->mutex);
  5863. return rc;
  5864. }
  5865. static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  5866. const struct ieee80211_tx_queue_params *params)
  5867. {
  5868. struct iwl3945_priv *priv = hw->priv;
  5869. unsigned long flags;
  5870. int q;
  5871. IWL_DEBUG_MAC80211("enter\n");
  5872. if (!iwl3945_is_ready_rf(priv)) {
  5873. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5874. return -EIO;
  5875. }
  5876. if (queue >= AC_NUM) {
  5877. IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  5878. return 0;
  5879. }
  5880. if (!priv->qos_data.qos_enable) {
  5881. priv->qos_data.qos_active = 0;
  5882. IWL_DEBUG_MAC80211("leave - qos not enabled\n");
  5883. return 0;
  5884. }
  5885. q = AC_NUM - 1 - queue;
  5886. spin_lock_irqsave(&priv->lock, flags);
  5887. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  5888. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  5889. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  5890. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  5891. cpu_to_le16((params->txop * 32));
  5892. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  5893. priv->qos_data.qos_active = 1;
  5894. spin_unlock_irqrestore(&priv->lock, flags);
  5895. mutex_lock(&priv->mutex);
  5896. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  5897. iwl3945_activate_qos(priv, 1);
  5898. else if (priv->assoc_id && iwl3945_is_associated(priv))
  5899. iwl3945_activate_qos(priv, 0);
  5900. mutex_unlock(&priv->mutex);
  5901. IWL_DEBUG_MAC80211("leave\n");
  5902. return 0;
  5903. }
  5904. static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
  5905. struct ieee80211_tx_queue_stats *stats)
  5906. {
  5907. struct iwl3945_priv *priv = hw->priv;
  5908. int i, avail;
  5909. struct iwl3945_tx_queue *txq;
  5910. struct iwl3945_queue *q;
  5911. unsigned long flags;
  5912. IWL_DEBUG_MAC80211("enter\n");
  5913. if (!iwl3945_is_ready_rf(priv)) {
  5914. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5915. return -EIO;
  5916. }
  5917. spin_lock_irqsave(&priv->lock, flags);
  5918. for (i = 0; i < AC_NUM; i++) {
  5919. txq = &priv->txq[i];
  5920. q = &txq->q;
  5921. avail = iwl3945_queue_space(q);
  5922. stats[i].len = q->n_window - avail;
  5923. stats[i].limit = q->n_window - q->high_mark;
  5924. stats[i].count = q->n_window;
  5925. }
  5926. spin_unlock_irqrestore(&priv->lock, flags);
  5927. IWL_DEBUG_MAC80211("leave\n");
  5928. return 0;
  5929. }
  5930. static int iwl3945_mac_get_stats(struct ieee80211_hw *hw,
  5931. struct ieee80211_low_level_stats *stats)
  5932. {
  5933. IWL_DEBUG_MAC80211("enter\n");
  5934. IWL_DEBUG_MAC80211("leave\n");
  5935. return 0;
  5936. }
  5937. static u64 iwl3945_mac_get_tsf(struct ieee80211_hw *hw)
  5938. {
  5939. IWL_DEBUG_MAC80211("enter\n");
  5940. IWL_DEBUG_MAC80211("leave\n");
  5941. return 0;
  5942. }
  5943. static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
  5944. {
  5945. struct iwl3945_priv *priv = hw->priv;
  5946. unsigned long flags;
  5947. mutex_lock(&priv->mutex);
  5948. IWL_DEBUG_MAC80211("enter\n");
  5949. iwl3945_reset_qos(priv);
  5950. cancel_delayed_work(&priv->post_associate);
  5951. spin_lock_irqsave(&priv->lock, flags);
  5952. priv->assoc_id = 0;
  5953. priv->assoc_capability = 0;
  5954. priv->call_post_assoc_from_beacon = 0;
  5955. /* new association get rid of ibss beacon skb */
  5956. if (priv->ibss_beacon)
  5957. dev_kfree_skb(priv->ibss_beacon);
  5958. priv->ibss_beacon = NULL;
  5959. priv->beacon_int = priv->hw->conf.beacon_int;
  5960. priv->timestamp1 = 0;
  5961. priv->timestamp0 = 0;
  5962. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
  5963. priv->beacon_int = 0;
  5964. spin_unlock_irqrestore(&priv->lock, flags);
  5965. if (!iwl3945_is_ready_rf(priv)) {
  5966. IWL_DEBUG_MAC80211("leave - not ready\n");
  5967. mutex_unlock(&priv->mutex);
  5968. return;
  5969. }
  5970. /* we are restarting association process
  5971. * clear RXON_FILTER_ASSOC_MSK bit
  5972. */
  5973. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  5974. iwl3945_scan_cancel_timeout(priv, 100);
  5975. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5976. iwl3945_commit_rxon(priv);
  5977. }
  5978. /* Per mac80211.h: This is only used in IBSS mode... */
  5979. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  5980. IWL_DEBUG_MAC80211("leave - not in IBSS\n");
  5981. mutex_unlock(&priv->mutex);
  5982. return;
  5983. }
  5984. iwl3945_set_rate(priv);
  5985. mutex_unlock(&priv->mutex);
  5986. IWL_DEBUG_MAC80211("leave\n");
  5987. }
  5988. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
  5989. {
  5990. struct iwl3945_priv *priv = hw->priv;
  5991. unsigned long flags;
  5992. mutex_lock(&priv->mutex);
  5993. IWL_DEBUG_MAC80211("enter\n");
  5994. if (!iwl3945_is_ready_rf(priv)) {
  5995. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5996. mutex_unlock(&priv->mutex);
  5997. return -EIO;
  5998. }
  5999. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  6000. IWL_DEBUG_MAC80211("leave - not IBSS\n");
  6001. mutex_unlock(&priv->mutex);
  6002. return -EIO;
  6003. }
  6004. spin_lock_irqsave(&priv->lock, flags);
  6005. if (priv->ibss_beacon)
  6006. dev_kfree_skb(priv->ibss_beacon);
  6007. priv->ibss_beacon = skb;
  6008. priv->assoc_id = 0;
  6009. IWL_DEBUG_MAC80211("leave\n");
  6010. spin_unlock_irqrestore(&priv->lock, flags);
  6011. iwl3945_reset_qos(priv);
  6012. queue_work(priv->workqueue, &priv->post_associate.work);
  6013. mutex_unlock(&priv->mutex);
  6014. return 0;
  6015. }
  6016. /*****************************************************************************
  6017. *
  6018. * sysfs attributes
  6019. *
  6020. *****************************************************************************/
  6021. #ifdef CONFIG_IWL3945_DEBUG
  6022. /*
  6023. * The following adds a new attribute to the sysfs representation
  6024. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  6025. * used for controlling the debug level.
  6026. *
  6027. * See the level definitions in iwl for details.
  6028. */
  6029. static ssize_t show_debug_level(struct device_driver *d, char *buf)
  6030. {
  6031. return sprintf(buf, "0x%08X\n", iwl3945_debug_level);
  6032. }
  6033. static ssize_t store_debug_level(struct device_driver *d,
  6034. const char *buf, size_t count)
  6035. {
  6036. char *p = (char *)buf;
  6037. u32 val;
  6038. val = simple_strtoul(p, &p, 0);
  6039. if (p == buf)
  6040. printk(KERN_INFO DRV_NAME
  6041. ": %s is not in hex or decimal form.\n", buf);
  6042. else
  6043. iwl3945_debug_level = val;
  6044. return strnlen(buf, count);
  6045. }
  6046. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  6047. show_debug_level, store_debug_level);
  6048. #endif /* CONFIG_IWL3945_DEBUG */
  6049. static ssize_t show_temperature(struct device *d,
  6050. struct device_attribute *attr, char *buf)
  6051. {
  6052. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6053. if (!iwl3945_is_alive(priv))
  6054. return -EAGAIN;
  6055. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  6056. }
  6057. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  6058. static ssize_t show_rs_window(struct device *d,
  6059. struct device_attribute *attr,
  6060. char *buf)
  6061. {
  6062. struct iwl3945_priv *priv = d->driver_data;
  6063. return iwl3945_fill_rs_info(priv->hw, buf, IWL_AP_ID);
  6064. }
  6065. static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
  6066. static ssize_t show_tx_power(struct device *d,
  6067. struct device_attribute *attr, char *buf)
  6068. {
  6069. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6070. return sprintf(buf, "%d\n", priv->user_txpower_limit);
  6071. }
  6072. static ssize_t store_tx_power(struct device *d,
  6073. struct device_attribute *attr,
  6074. const char *buf, size_t count)
  6075. {
  6076. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6077. char *p = (char *)buf;
  6078. u32 val;
  6079. val = simple_strtoul(p, &p, 10);
  6080. if (p == buf)
  6081. printk(KERN_INFO DRV_NAME
  6082. ": %s is not in decimal form.\n", buf);
  6083. else
  6084. iwl3945_hw_reg_set_txpower(priv, val);
  6085. return count;
  6086. }
  6087. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  6088. static ssize_t show_flags(struct device *d,
  6089. struct device_attribute *attr, char *buf)
  6090. {
  6091. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6092. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  6093. }
  6094. static ssize_t store_flags(struct device *d,
  6095. struct device_attribute *attr,
  6096. const char *buf, size_t count)
  6097. {
  6098. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6099. u32 flags = simple_strtoul(buf, NULL, 0);
  6100. mutex_lock(&priv->mutex);
  6101. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  6102. /* Cancel any currently running scans... */
  6103. if (iwl3945_scan_cancel_timeout(priv, 100))
  6104. IWL_WARNING("Could not cancel scan.\n");
  6105. else {
  6106. IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
  6107. flags);
  6108. priv->staging_rxon.flags = cpu_to_le32(flags);
  6109. iwl3945_commit_rxon(priv);
  6110. }
  6111. }
  6112. mutex_unlock(&priv->mutex);
  6113. return count;
  6114. }
  6115. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  6116. static ssize_t show_filter_flags(struct device *d,
  6117. struct device_attribute *attr, char *buf)
  6118. {
  6119. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6120. return sprintf(buf, "0x%04X\n",
  6121. le32_to_cpu(priv->active_rxon.filter_flags));
  6122. }
  6123. static ssize_t store_filter_flags(struct device *d,
  6124. struct device_attribute *attr,
  6125. const char *buf, size_t count)
  6126. {
  6127. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6128. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  6129. mutex_lock(&priv->mutex);
  6130. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  6131. /* Cancel any currently running scans... */
  6132. if (iwl3945_scan_cancel_timeout(priv, 100))
  6133. IWL_WARNING("Could not cancel scan.\n");
  6134. else {
  6135. IWL_DEBUG_INFO("Committing rxon.filter_flags = "
  6136. "0x%04X\n", filter_flags);
  6137. priv->staging_rxon.filter_flags =
  6138. cpu_to_le32(filter_flags);
  6139. iwl3945_commit_rxon(priv);
  6140. }
  6141. }
  6142. mutex_unlock(&priv->mutex);
  6143. return count;
  6144. }
  6145. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  6146. store_filter_flags);
  6147. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  6148. static ssize_t show_measurement(struct device *d,
  6149. struct device_attribute *attr, char *buf)
  6150. {
  6151. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6152. struct iwl3945_spectrum_notification measure_report;
  6153. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  6154. u8 *data = (u8 *)&measure_report;
  6155. unsigned long flags;
  6156. spin_lock_irqsave(&priv->lock, flags);
  6157. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  6158. spin_unlock_irqrestore(&priv->lock, flags);
  6159. return 0;
  6160. }
  6161. memcpy(&measure_report, &priv->measure_report, size);
  6162. priv->measurement_status = 0;
  6163. spin_unlock_irqrestore(&priv->lock, flags);
  6164. while (size && (PAGE_SIZE - len)) {
  6165. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  6166. PAGE_SIZE - len, 1);
  6167. len = strlen(buf);
  6168. if (PAGE_SIZE - len)
  6169. buf[len++] = '\n';
  6170. ofs += 16;
  6171. size -= min(size, 16U);
  6172. }
  6173. return len;
  6174. }
  6175. static ssize_t store_measurement(struct device *d,
  6176. struct device_attribute *attr,
  6177. const char *buf, size_t count)
  6178. {
  6179. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6180. struct ieee80211_measurement_params params = {
  6181. .channel = le16_to_cpu(priv->active_rxon.channel),
  6182. .start_time = cpu_to_le64(priv->last_tsf),
  6183. .duration = cpu_to_le16(1),
  6184. };
  6185. u8 type = IWL_MEASURE_BASIC;
  6186. u8 buffer[32];
  6187. u8 channel;
  6188. if (count) {
  6189. char *p = buffer;
  6190. strncpy(buffer, buf, min(sizeof(buffer), count));
  6191. channel = simple_strtoul(p, NULL, 0);
  6192. if (channel)
  6193. params.channel = channel;
  6194. p = buffer;
  6195. while (*p && *p != ' ')
  6196. p++;
  6197. if (*p)
  6198. type = simple_strtoul(p + 1, NULL, 0);
  6199. }
  6200. IWL_DEBUG_INFO("Invoking measurement of type %d on "
  6201. "channel %d (for '%s')\n", type, params.channel, buf);
  6202. iwl3945_get_measurement(priv, &params, type);
  6203. return count;
  6204. }
  6205. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  6206. show_measurement, store_measurement);
  6207. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  6208. static ssize_t store_retry_rate(struct device *d,
  6209. struct device_attribute *attr,
  6210. const char *buf, size_t count)
  6211. {
  6212. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6213. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  6214. if (priv->retry_rate <= 0)
  6215. priv->retry_rate = 1;
  6216. return count;
  6217. }
  6218. static ssize_t show_retry_rate(struct device *d,
  6219. struct device_attribute *attr, char *buf)
  6220. {
  6221. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6222. return sprintf(buf, "%d", priv->retry_rate);
  6223. }
  6224. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  6225. store_retry_rate);
  6226. static ssize_t store_power_level(struct device *d,
  6227. struct device_attribute *attr,
  6228. const char *buf, size_t count)
  6229. {
  6230. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6231. int rc;
  6232. int mode;
  6233. mode = simple_strtoul(buf, NULL, 0);
  6234. mutex_lock(&priv->mutex);
  6235. if (!iwl3945_is_ready(priv)) {
  6236. rc = -EAGAIN;
  6237. goto out;
  6238. }
  6239. if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
  6240. mode = IWL_POWER_AC;
  6241. else
  6242. mode |= IWL_POWER_ENABLED;
  6243. if (mode != priv->power_mode) {
  6244. rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
  6245. if (rc) {
  6246. IWL_DEBUG_MAC80211("failed setting power mode.\n");
  6247. goto out;
  6248. }
  6249. priv->power_mode = mode;
  6250. }
  6251. rc = count;
  6252. out:
  6253. mutex_unlock(&priv->mutex);
  6254. return rc;
  6255. }
  6256. #define MAX_WX_STRING 80
  6257. /* Values are in microsecond */
  6258. static const s32 timeout_duration[] = {
  6259. 350000,
  6260. 250000,
  6261. 75000,
  6262. 37000,
  6263. 25000,
  6264. };
  6265. static const s32 period_duration[] = {
  6266. 400000,
  6267. 700000,
  6268. 1000000,
  6269. 1000000,
  6270. 1000000
  6271. };
  6272. static ssize_t show_power_level(struct device *d,
  6273. struct device_attribute *attr, char *buf)
  6274. {
  6275. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6276. int level = IWL_POWER_LEVEL(priv->power_mode);
  6277. char *p = buf;
  6278. p += sprintf(p, "%d ", level);
  6279. switch (level) {
  6280. case IWL_POWER_MODE_CAM:
  6281. case IWL_POWER_AC:
  6282. p += sprintf(p, "(AC)");
  6283. break;
  6284. case IWL_POWER_BATTERY:
  6285. p += sprintf(p, "(BATTERY)");
  6286. break;
  6287. default:
  6288. p += sprintf(p,
  6289. "(Timeout %dms, Period %dms)",
  6290. timeout_duration[level - 1] / 1000,
  6291. period_duration[level - 1] / 1000);
  6292. }
  6293. if (!(priv->power_mode & IWL_POWER_ENABLED))
  6294. p += sprintf(p, " OFF\n");
  6295. else
  6296. p += sprintf(p, " \n");
  6297. return p - buf + 1;
  6298. }
  6299. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
  6300. store_power_level);
  6301. static ssize_t show_channels(struct device *d,
  6302. struct device_attribute *attr, char *buf)
  6303. {
  6304. /* all this shit doesn't belong into sysfs anyway */
  6305. return 0;
  6306. }
  6307. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  6308. static ssize_t show_statistics(struct device *d,
  6309. struct device_attribute *attr, char *buf)
  6310. {
  6311. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6312. u32 size = sizeof(struct iwl3945_notif_statistics);
  6313. u32 len = 0, ofs = 0;
  6314. u8 *data = (u8 *)&priv->statistics;
  6315. int rc = 0;
  6316. if (!iwl3945_is_alive(priv))
  6317. return -EAGAIN;
  6318. mutex_lock(&priv->mutex);
  6319. rc = iwl3945_send_statistics_request(priv);
  6320. mutex_unlock(&priv->mutex);
  6321. if (rc) {
  6322. len = sprintf(buf,
  6323. "Error sending statistics request: 0x%08X\n", rc);
  6324. return len;
  6325. }
  6326. while (size && (PAGE_SIZE - len)) {
  6327. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  6328. PAGE_SIZE - len, 1);
  6329. len = strlen(buf);
  6330. if (PAGE_SIZE - len)
  6331. buf[len++] = '\n';
  6332. ofs += 16;
  6333. size -= min(size, 16U);
  6334. }
  6335. return len;
  6336. }
  6337. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  6338. static ssize_t show_antenna(struct device *d,
  6339. struct device_attribute *attr, char *buf)
  6340. {
  6341. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6342. if (!iwl3945_is_alive(priv))
  6343. return -EAGAIN;
  6344. return sprintf(buf, "%d\n", priv->antenna);
  6345. }
  6346. static ssize_t store_antenna(struct device *d,
  6347. struct device_attribute *attr,
  6348. const char *buf, size_t count)
  6349. {
  6350. int ant;
  6351. struct iwl3945_priv *priv = dev_get_drvdata(d);
  6352. if (count == 0)
  6353. return 0;
  6354. if (sscanf(buf, "%1i", &ant) != 1) {
  6355. IWL_DEBUG_INFO("not in hex or decimal form.\n");
  6356. return count;
  6357. }
  6358. if ((ant >= 0) && (ant <= 2)) {
  6359. IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
  6360. priv->antenna = (enum iwl3945_antenna)ant;
  6361. } else
  6362. IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
  6363. return count;
  6364. }
  6365. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  6366. static ssize_t show_status(struct device *d,
  6367. struct device_attribute *attr, char *buf)
  6368. {
  6369. struct iwl3945_priv *priv = (struct iwl3945_priv *)d->driver_data;
  6370. if (!iwl3945_is_alive(priv))
  6371. return -EAGAIN;
  6372. return sprintf(buf, "0x%08x\n", (int)priv->status);
  6373. }
  6374. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  6375. static ssize_t dump_error_log(struct device *d,
  6376. struct device_attribute *attr,
  6377. const char *buf, size_t count)
  6378. {
  6379. char *p = (char *)buf;
  6380. if (p[0] == '1')
  6381. iwl3945_dump_nic_error_log((struct iwl3945_priv *)d->driver_data);
  6382. return strnlen(buf, count);
  6383. }
  6384. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  6385. static ssize_t dump_event_log(struct device *d,
  6386. struct device_attribute *attr,
  6387. const char *buf, size_t count)
  6388. {
  6389. char *p = (char *)buf;
  6390. if (p[0] == '1')
  6391. iwl3945_dump_nic_event_log((struct iwl3945_priv *)d->driver_data);
  6392. return strnlen(buf, count);
  6393. }
  6394. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  6395. /*****************************************************************************
  6396. *
  6397. * driver setup and teardown
  6398. *
  6399. *****************************************************************************/
  6400. static void iwl3945_setup_deferred_work(struct iwl3945_priv *priv)
  6401. {
  6402. priv->workqueue = create_workqueue(DRV_NAME);
  6403. init_waitqueue_head(&priv->wait_command_queue);
  6404. INIT_WORK(&priv->up, iwl3945_bg_up);
  6405. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  6406. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  6407. INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
  6408. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  6409. INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
  6410. INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
  6411. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  6412. INIT_WORK(&priv->set_monitor, iwl3945_bg_set_monitor);
  6413. INIT_DELAYED_WORK(&priv->post_associate, iwl3945_bg_post_associate);
  6414. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  6415. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  6416. INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
  6417. iwl3945_hw_setup_deferred_work(priv);
  6418. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  6419. iwl3945_irq_tasklet, (unsigned long)priv);
  6420. }
  6421. static void iwl3945_cancel_deferred_work(struct iwl3945_priv *priv)
  6422. {
  6423. iwl3945_hw_cancel_deferred_work(priv);
  6424. cancel_delayed_work_sync(&priv->init_alive_start);
  6425. cancel_delayed_work(&priv->scan_check);
  6426. cancel_delayed_work(&priv->alive_start);
  6427. cancel_delayed_work(&priv->post_associate);
  6428. cancel_work_sync(&priv->beacon_update);
  6429. }
  6430. static struct attribute *iwl3945_sysfs_entries[] = {
  6431. &dev_attr_antenna.attr,
  6432. &dev_attr_channels.attr,
  6433. &dev_attr_dump_errors.attr,
  6434. &dev_attr_dump_events.attr,
  6435. &dev_attr_flags.attr,
  6436. &dev_attr_filter_flags.attr,
  6437. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  6438. &dev_attr_measurement.attr,
  6439. #endif
  6440. &dev_attr_power_level.attr,
  6441. &dev_attr_retry_rate.attr,
  6442. &dev_attr_rs_window.attr,
  6443. &dev_attr_statistics.attr,
  6444. &dev_attr_status.attr,
  6445. &dev_attr_temperature.attr,
  6446. &dev_attr_tx_power.attr,
  6447. NULL
  6448. };
  6449. static struct attribute_group iwl3945_attribute_group = {
  6450. .name = NULL, /* put in device directory */
  6451. .attrs = iwl3945_sysfs_entries,
  6452. };
  6453. static struct ieee80211_ops iwl3945_hw_ops = {
  6454. .tx = iwl3945_mac_tx,
  6455. .start = iwl3945_mac_start,
  6456. .stop = iwl3945_mac_stop,
  6457. .add_interface = iwl3945_mac_add_interface,
  6458. .remove_interface = iwl3945_mac_remove_interface,
  6459. .config = iwl3945_mac_config,
  6460. .config_interface = iwl3945_mac_config_interface,
  6461. .configure_filter = iwl3945_configure_filter,
  6462. .set_key = iwl3945_mac_set_key,
  6463. .get_stats = iwl3945_mac_get_stats,
  6464. .get_tx_stats = iwl3945_mac_get_tx_stats,
  6465. .conf_tx = iwl3945_mac_conf_tx,
  6466. .get_tsf = iwl3945_mac_get_tsf,
  6467. .reset_tsf = iwl3945_mac_reset_tsf,
  6468. .hw_scan = iwl3945_mac_hw_scan
  6469. };
  6470. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  6471. {
  6472. int err = 0;
  6473. struct iwl3945_priv *priv;
  6474. struct ieee80211_hw *hw;
  6475. struct iwl_3945_cfg *cfg = (struct iwl_3945_cfg *)(ent->driver_data);
  6476. unsigned long flags;
  6477. DECLARE_MAC_BUF(mac);
  6478. /* Disabling hardware scan means that mac80211 will perform scans
  6479. * "the hard way", rather than using device's scan. */
  6480. if (iwl3945_param_disable_hw_scan) {
  6481. IWL_DEBUG_INFO("Disabling hw_scan\n");
  6482. iwl3945_hw_ops.hw_scan = NULL;
  6483. }
  6484. if ((iwl3945_param_queues_num > IWL39_MAX_NUM_QUEUES) ||
  6485. (iwl3945_param_queues_num < IWL_MIN_NUM_QUEUES)) {
  6486. IWL_ERROR("invalid queues_num, should be between %d and %d\n",
  6487. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  6488. err = -EINVAL;
  6489. goto out;
  6490. }
  6491. /* mac80211 allocates memory for this device instance, including
  6492. * space for this driver's private structure */
  6493. hw = ieee80211_alloc_hw(sizeof(struct iwl3945_priv), &iwl3945_hw_ops);
  6494. if (hw == NULL) {
  6495. IWL_ERROR("Can not allocate network device\n");
  6496. err = -ENOMEM;
  6497. goto out;
  6498. }
  6499. SET_IEEE80211_DEV(hw, &pdev->dev);
  6500. hw->rate_control_algorithm = "iwl-3945-rs";
  6501. IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
  6502. priv = hw->priv;
  6503. priv->hw = hw;
  6504. priv->pci_dev = pdev;
  6505. priv->cfg = cfg;
  6506. /* Select antenna (may be helpful if only one antenna is connected) */
  6507. priv->antenna = (enum iwl3945_antenna)iwl3945_param_antenna;
  6508. #ifdef CONFIG_IWL3945_DEBUG
  6509. iwl3945_debug_level = iwl3945_param_debug;
  6510. atomic_set(&priv->restrict_refcnt, 0);
  6511. #endif
  6512. priv->retry_rate = 1;
  6513. priv->ibss_beacon = NULL;
  6514. /* Tell mac80211 our characteristics */
  6515. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  6516. IEEE80211_HW_NOISE_DBM;
  6517. /* 4 EDCA QOS priorities */
  6518. hw->queues = 4;
  6519. spin_lock_init(&priv->lock);
  6520. spin_lock_init(&priv->power_data.lock);
  6521. spin_lock_init(&priv->sta_lock);
  6522. spin_lock_init(&priv->hcmd_lock);
  6523. INIT_LIST_HEAD(&priv->free_frames);
  6524. mutex_init(&priv->mutex);
  6525. if (pci_enable_device(pdev)) {
  6526. err = -ENODEV;
  6527. goto out_ieee80211_free_hw;
  6528. }
  6529. pci_set_master(pdev);
  6530. /* Clear the driver's (not device's) station table */
  6531. iwl3945_clear_stations_table(priv);
  6532. priv->data_retry_limit = -1;
  6533. priv->ieee_channels = NULL;
  6534. priv->ieee_rates = NULL;
  6535. priv->band = IEEE80211_BAND_2GHZ;
  6536. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  6537. if (!err)
  6538. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  6539. if (err) {
  6540. printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
  6541. goto out_pci_disable_device;
  6542. }
  6543. pci_set_drvdata(pdev, priv);
  6544. err = pci_request_regions(pdev, DRV_NAME);
  6545. if (err)
  6546. goto out_pci_disable_device;
  6547. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  6548. * PCI Tx retries from interfering with C3 CPU state */
  6549. pci_write_config_byte(pdev, 0x41, 0x00);
  6550. priv->hw_base = pci_iomap(pdev, 0, 0);
  6551. if (!priv->hw_base) {
  6552. err = -ENODEV;
  6553. goto out_pci_release_regions;
  6554. }
  6555. IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
  6556. (unsigned long long) pci_resource_len(pdev, 0));
  6557. IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
  6558. /* Initialize module parameter values here */
  6559. /* Disable radio (SW RF KILL) via parameter when loading driver */
  6560. if (iwl3945_param_disable) {
  6561. set_bit(STATUS_RF_KILL_SW, &priv->status);
  6562. IWL_DEBUG_INFO("Radio disabled.\n");
  6563. }
  6564. priv->iw_mode = IEEE80211_IF_TYPE_STA;
  6565. printk(KERN_INFO DRV_NAME
  6566. ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
  6567. /* Device-specific setup */
  6568. if (iwl3945_hw_set_hw_setting(priv)) {
  6569. IWL_ERROR("failed to set hw settings\n");
  6570. goto out_iounmap;
  6571. }
  6572. if (iwl3945_param_qos_enable)
  6573. priv->qos_data.qos_enable = 1;
  6574. iwl3945_reset_qos(priv);
  6575. priv->qos_data.qos_active = 0;
  6576. priv->qos_data.qos_cap.val = 0;
  6577. iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
  6578. iwl3945_setup_deferred_work(priv);
  6579. iwl3945_setup_rx_handlers(priv);
  6580. priv->rates_mask = IWL_RATES_MASK;
  6581. /* If power management is turned on, default to AC mode */
  6582. priv->power_mode = IWL_POWER_AC;
  6583. priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
  6584. spin_lock_irqsave(&priv->lock, flags);
  6585. iwl3945_disable_interrupts(priv);
  6586. spin_unlock_irqrestore(&priv->lock, flags);
  6587. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6588. if (err) {
  6589. IWL_ERROR("failed to create sysfs device attributes\n");
  6590. goto out_release_irq;
  6591. }
  6592. /* nic init */
  6593. iwl3945_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  6594. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  6595. iwl3945_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  6596. err = iwl3945_poll_bit(priv, CSR_GP_CNTRL,
  6597. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  6598. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  6599. if (err < 0) {
  6600. IWL_DEBUG_INFO("Failed to init the card\n");
  6601. goto out_remove_sysfs;
  6602. }
  6603. /* Read the EEPROM */
  6604. err = iwl3945_eeprom_init(priv);
  6605. if (err) {
  6606. IWL_ERROR("Unable to init EEPROM\n");
  6607. goto out_remove_sysfs;
  6608. }
  6609. /* MAC Address location in EEPROM same for 3945/4965 */
  6610. get_eeprom_mac(priv, priv->mac_addr);
  6611. IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
  6612. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  6613. err = iwl3945_init_channel_map(priv);
  6614. if (err) {
  6615. IWL_ERROR("initializing regulatory failed: %d\n", err);
  6616. goto out_remove_sysfs;
  6617. }
  6618. err = iwl3945_init_geos(priv);
  6619. if (err) {
  6620. IWL_ERROR("initializing geos failed: %d\n", err);
  6621. goto out_free_channel_map;
  6622. }
  6623. err = ieee80211_register_hw(priv->hw);
  6624. if (err) {
  6625. IWL_ERROR("Failed to register network device (error %d)\n", err);
  6626. goto out_free_geos;
  6627. }
  6628. priv->hw->conf.beacon_int = 100;
  6629. priv->mac80211_registered = 1;
  6630. pci_save_state(pdev);
  6631. pci_disable_device(pdev);
  6632. err = iwl3945_rfkill_init(priv);
  6633. if (err)
  6634. IWL_ERROR("Unable to initialize RFKILL system. "
  6635. "Ignoring error: %d\n", err);
  6636. return 0;
  6637. out_free_geos:
  6638. iwl3945_free_geos(priv);
  6639. out_free_channel_map:
  6640. iwl3945_free_channel_map(priv);
  6641. out_remove_sysfs:
  6642. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6643. out_release_irq:
  6644. destroy_workqueue(priv->workqueue);
  6645. priv->workqueue = NULL;
  6646. iwl3945_unset_hw_setting(priv);
  6647. out_iounmap:
  6648. pci_iounmap(pdev, priv->hw_base);
  6649. out_pci_release_regions:
  6650. pci_release_regions(pdev);
  6651. out_pci_disable_device:
  6652. pci_disable_device(pdev);
  6653. pci_set_drvdata(pdev, NULL);
  6654. out_ieee80211_free_hw:
  6655. ieee80211_free_hw(priv->hw);
  6656. out:
  6657. return err;
  6658. }
  6659. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  6660. {
  6661. struct iwl3945_priv *priv = pci_get_drvdata(pdev);
  6662. unsigned long flags;
  6663. if (!priv)
  6664. return;
  6665. IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
  6666. set_bit(STATUS_EXIT_PENDING, &priv->status);
  6667. iwl3945_down(priv);
  6668. /* make sure we flush any pending irq or
  6669. * tasklet for the driver
  6670. */
  6671. spin_lock_irqsave(&priv->lock, flags);
  6672. iwl3945_disable_interrupts(priv);
  6673. spin_unlock_irqrestore(&priv->lock, flags);
  6674. iwl_synchronize_irq(priv);
  6675. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6676. iwl3945_rfkill_unregister(priv);
  6677. iwl3945_dealloc_ucode_pci(priv);
  6678. if (priv->rxq.bd)
  6679. iwl3945_rx_queue_free(priv, &priv->rxq);
  6680. iwl3945_hw_txq_ctx_free(priv);
  6681. iwl3945_unset_hw_setting(priv);
  6682. iwl3945_clear_stations_table(priv);
  6683. if (priv->mac80211_registered)
  6684. ieee80211_unregister_hw(priv->hw);
  6685. /*netif_stop_queue(dev); */
  6686. flush_workqueue(priv->workqueue);
  6687. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  6688. * priv->workqueue... so we can't take down the workqueue
  6689. * until now... */
  6690. destroy_workqueue(priv->workqueue);
  6691. priv->workqueue = NULL;
  6692. pci_iounmap(pdev, priv->hw_base);
  6693. pci_release_regions(pdev);
  6694. pci_disable_device(pdev);
  6695. pci_set_drvdata(pdev, NULL);
  6696. iwl3945_free_channel_map(priv);
  6697. iwl3945_free_geos(priv);
  6698. kfree(priv->scan);
  6699. if (priv->ibss_beacon)
  6700. dev_kfree_skb(priv->ibss_beacon);
  6701. ieee80211_free_hw(priv->hw);
  6702. }
  6703. #ifdef CONFIG_PM
  6704. static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  6705. {
  6706. struct iwl3945_priv *priv = pci_get_drvdata(pdev);
  6707. if (priv->is_open) {
  6708. set_bit(STATUS_IN_SUSPEND, &priv->status);
  6709. iwl3945_mac_stop(priv->hw);
  6710. priv->is_open = 1;
  6711. }
  6712. pci_set_power_state(pdev, PCI_D3hot);
  6713. return 0;
  6714. }
  6715. static int iwl3945_pci_resume(struct pci_dev *pdev)
  6716. {
  6717. struct iwl3945_priv *priv = pci_get_drvdata(pdev);
  6718. pci_set_power_state(pdev, PCI_D0);
  6719. if (priv->is_open)
  6720. iwl3945_mac_start(priv->hw);
  6721. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  6722. return 0;
  6723. }
  6724. #endif /* CONFIG_PM */
  6725. /*************** RFKILL FUNCTIONS **********/
  6726. #ifdef CONFIG_IWL3945_RFKILL
  6727. /* software rf-kill from user */
  6728. static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
  6729. {
  6730. struct iwl3945_priv *priv = data;
  6731. int err = 0;
  6732. if (!priv->rfkill)
  6733. return 0;
  6734. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  6735. return 0;
  6736. IWL_DEBUG_RF_KILL("we recieved soft RFKILL set to state %d\n", state);
  6737. mutex_lock(&priv->mutex);
  6738. switch (state) {
  6739. case RFKILL_STATE_UNBLOCKED:
  6740. if (iwl3945_is_rfkill_hw(priv)) {
  6741. err = -EBUSY;
  6742. goto out_unlock;
  6743. }
  6744. iwl3945_radio_kill_sw(priv, 0);
  6745. break;
  6746. case RFKILL_STATE_SOFT_BLOCKED:
  6747. iwl3945_radio_kill_sw(priv, 1);
  6748. break;
  6749. default:
  6750. IWL_WARNING("we recieved unexpected RFKILL state %d\n", state);
  6751. break;
  6752. }
  6753. out_unlock:
  6754. mutex_unlock(&priv->mutex);
  6755. return err;
  6756. }
  6757. int iwl3945_rfkill_init(struct iwl3945_priv *priv)
  6758. {
  6759. struct device *device = wiphy_dev(priv->hw->wiphy);
  6760. int ret = 0;
  6761. BUG_ON(device == NULL);
  6762. IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
  6763. priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
  6764. if (!priv->rfkill) {
  6765. IWL_ERROR("Unable to allocate rfkill device.\n");
  6766. ret = -ENOMEM;
  6767. goto error;
  6768. }
  6769. priv->rfkill->name = priv->cfg->name;
  6770. priv->rfkill->data = priv;
  6771. priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
  6772. priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
  6773. priv->rfkill->user_claim_unsupported = 1;
  6774. priv->rfkill->dev.class->suspend = NULL;
  6775. priv->rfkill->dev.class->resume = NULL;
  6776. ret = rfkill_register(priv->rfkill);
  6777. if (ret) {
  6778. IWL_ERROR("Unable to register rfkill: %d\n", ret);
  6779. goto freed_rfkill;
  6780. }
  6781. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  6782. return ret;
  6783. freed_rfkill:
  6784. if (priv->rfkill != NULL)
  6785. rfkill_free(priv->rfkill);
  6786. priv->rfkill = NULL;
  6787. error:
  6788. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  6789. return ret;
  6790. }
  6791. void iwl3945_rfkill_unregister(struct iwl3945_priv *priv)
  6792. {
  6793. if (priv->rfkill)
  6794. rfkill_unregister(priv->rfkill);
  6795. priv->rfkill = NULL;
  6796. }
  6797. /* set rf-kill to the right state. */
  6798. void iwl3945_rfkill_set_hw_state(struct iwl3945_priv *priv)
  6799. {
  6800. if (!priv->rfkill)
  6801. return;
  6802. if (iwl3945_is_rfkill_hw(priv)) {
  6803. rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
  6804. return;
  6805. }
  6806. if (!iwl3945_is_rfkill_sw(priv))
  6807. rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
  6808. else
  6809. rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
  6810. }
  6811. #endif
  6812. /*****************************************************************************
  6813. *
  6814. * driver and module entry point
  6815. *
  6816. *****************************************************************************/
  6817. static struct pci_driver iwl3945_driver = {
  6818. .name = DRV_NAME,
  6819. .id_table = iwl3945_hw_card_ids,
  6820. .probe = iwl3945_pci_probe,
  6821. .remove = __devexit_p(iwl3945_pci_remove),
  6822. #ifdef CONFIG_PM
  6823. .suspend = iwl3945_pci_suspend,
  6824. .resume = iwl3945_pci_resume,
  6825. #endif
  6826. };
  6827. static int __init iwl3945_init(void)
  6828. {
  6829. int ret;
  6830. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  6831. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  6832. ret = iwl3945_rate_control_register();
  6833. if (ret) {
  6834. IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
  6835. return ret;
  6836. }
  6837. ret = pci_register_driver(&iwl3945_driver);
  6838. if (ret) {
  6839. IWL_ERROR("Unable to initialize PCI module\n");
  6840. goto error_register;
  6841. }
  6842. #ifdef CONFIG_IWL3945_DEBUG
  6843. ret = driver_create_file(&iwl3945_driver.driver, &driver_attr_debug_level);
  6844. if (ret) {
  6845. IWL_ERROR("Unable to create driver sysfs file\n");
  6846. goto error_debug;
  6847. }
  6848. #endif
  6849. return ret;
  6850. #ifdef CONFIG_IWL3945_DEBUG
  6851. error_debug:
  6852. pci_unregister_driver(&iwl3945_driver);
  6853. #endif
  6854. error_register:
  6855. iwl3945_rate_control_unregister();
  6856. return ret;
  6857. }
  6858. static void __exit iwl3945_exit(void)
  6859. {
  6860. #ifdef CONFIG_IWL3945_DEBUG
  6861. driver_remove_file(&iwl3945_driver.driver, &driver_attr_debug_level);
  6862. #endif
  6863. pci_unregister_driver(&iwl3945_driver);
  6864. iwl3945_rate_control_unregister();
  6865. }
  6866. module_param_named(antenna, iwl3945_param_antenna, int, 0444);
  6867. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  6868. module_param_named(disable, iwl3945_param_disable, int, 0444);
  6869. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  6870. module_param_named(hwcrypto, iwl3945_param_hwcrypto, int, 0444);
  6871. MODULE_PARM_DESC(hwcrypto,
  6872. "using hardware crypto engine (default 0 [software])\n");
  6873. module_param_named(debug, iwl3945_param_debug, int, 0444);
  6874. MODULE_PARM_DESC(debug, "debug output mask");
  6875. module_param_named(disable_hw_scan, iwl3945_param_disable_hw_scan, int, 0444);
  6876. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  6877. module_param_named(queues_num, iwl3945_param_queues_num, int, 0444);
  6878. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  6879. /* QoS */
  6880. module_param_named(qos_enable, iwl3945_param_qos_enable, int, 0444);
  6881. MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
  6882. module_exit(iwl3945_exit);
  6883. module_init(iwl3945_init);