main.c 134 KB

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