main.c 134 KB

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