main.c 135 KB

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