main.c 133 KB

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