main.c 129 KB

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