main.c 132 KB

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