main.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036
  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, 2006 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/version.h>
  29. #include <linux/firmware.h>
  30. #include <linux/wireless.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/skbuff.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.h"
  39. #include "dma.h"
  40. #include "sysfs.h"
  41. #include "xmit.h"
  42. #include "lo.h"
  43. #include "pcmcia.h"
  44. MODULE_DESCRIPTION("Broadcom B43 wireless driver");
  45. MODULE_AUTHOR("Martin Langer");
  46. MODULE_AUTHOR("Stefano Brivio");
  47. MODULE_AUTHOR("Michael Buesch");
  48. MODULE_LICENSE("GPL");
  49. static int modparam_bad_frames_preempt;
  50. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  51. MODULE_PARM_DESC(bad_frames_preempt,
  52. "enable(1) / disable(0) Bad Frames Preemption");
  53. static char modparam_fwpostfix[16];
  54. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  55. MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
  56. static int modparam_hwpctl;
  57. module_param_named(hwpctl, modparam_hwpctl, int, 0444);
  58. MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
  59. static int modparam_nohwcrypt;
  60. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  61. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  62. static const struct ssb_device_id b43_ssb_tbl[] = {
  63. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
  64. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
  65. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
  66. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
  67. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
  68. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
  69. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
  70. SSB_DEVTABLE_END
  71. };
  72. MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
  73. /* Channel and ratetables are shared for all devices.
  74. * They can't be const, because ieee80211 puts some precalculated
  75. * data in there. This data is the same for all devices, so we don't
  76. * get concurrency issues */
  77. #define RATETAB_ENT(_rateid, _flags) \
  78. { \
  79. .rate = B43_RATE_TO_BASE100KBPS(_rateid), \
  80. .val = (_rateid), \
  81. .val2 = (_rateid), \
  82. .flags = (_flags), \
  83. }
  84. static struct ieee80211_rate __b43_ratetable[] = {
  85. RATETAB_ENT(B43_CCK_RATE_1MB, IEEE80211_RATE_CCK),
  86. RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_CCK_2),
  87. RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_CCK_2),
  88. RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_CCK_2),
  89. RATETAB_ENT(B43_OFDM_RATE_6MB, IEEE80211_RATE_OFDM),
  90. RATETAB_ENT(B43_OFDM_RATE_9MB, IEEE80211_RATE_OFDM),
  91. RATETAB_ENT(B43_OFDM_RATE_12MB, IEEE80211_RATE_OFDM),
  92. RATETAB_ENT(B43_OFDM_RATE_18MB, IEEE80211_RATE_OFDM),
  93. RATETAB_ENT(B43_OFDM_RATE_24MB, IEEE80211_RATE_OFDM),
  94. RATETAB_ENT(B43_OFDM_RATE_36MB, IEEE80211_RATE_OFDM),
  95. RATETAB_ENT(B43_OFDM_RATE_48MB, IEEE80211_RATE_OFDM),
  96. RATETAB_ENT(B43_OFDM_RATE_54MB, IEEE80211_RATE_OFDM),
  97. };
  98. #define b43_a_ratetable (__b43_ratetable + 4)
  99. #define b43_a_ratetable_size 8
  100. #define b43_b_ratetable (__b43_ratetable + 0)
  101. #define b43_b_ratetable_size 4
  102. #define b43_g_ratetable (__b43_ratetable + 0)
  103. #define b43_g_ratetable_size 12
  104. #define CHANTAB_ENT(_chanid, _freq) \
  105. { \
  106. .chan = (_chanid), \
  107. .freq = (_freq), \
  108. .val = (_chanid), \
  109. .flag = IEEE80211_CHAN_W_SCAN | \
  110. IEEE80211_CHAN_W_ACTIVE_SCAN | \
  111. IEEE80211_CHAN_W_IBSS, \
  112. .power_level = 0xFF, \
  113. .antenna_max = 0xFF, \
  114. }
  115. static struct ieee80211_channel b43_2ghz_chantable[] = {
  116. CHANTAB_ENT(1, 2412),
  117. CHANTAB_ENT(2, 2417),
  118. CHANTAB_ENT(3, 2422),
  119. CHANTAB_ENT(4, 2427),
  120. CHANTAB_ENT(5, 2432),
  121. CHANTAB_ENT(6, 2437),
  122. CHANTAB_ENT(7, 2442),
  123. CHANTAB_ENT(8, 2447),
  124. CHANTAB_ENT(9, 2452),
  125. CHANTAB_ENT(10, 2457),
  126. CHANTAB_ENT(11, 2462),
  127. CHANTAB_ENT(12, 2467),
  128. CHANTAB_ENT(13, 2472),
  129. CHANTAB_ENT(14, 2484),
  130. };
  131. #define b43_2ghz_chantable_size ARRAY_SIZE(b43_2ghz_chantable)
  132. #if 0
  133. static struct ieee80211_channel b43_5ghz_chantable[] = {
  134. CHANTAB_ENT(36, 5180),
  135. CHANTAB_ENT(40, 5200),
  136. CHANTAB_ENT(44, 5220),
  137. CHANTAB_ENT(48, 5240),
  138. CHANTAB_ENT(52, 5260),
  139. CHANTAB_ENT(56, 5280),
  140. CHANTAB_ENT(60, 5300),
  141. CHANTAB_ENT(64, 5320),
  142. CHANTAB_ENT(149, 5745),
  143. CHANTAB_ENT(153, 5765),
  144. CHANTAB_ENT(157, 5785),
  145. CHANTAB_ENT(161, 5805),
  146. CHANTAB_ENT(165, 5825),
  147. };
  148. #define b43_5ghz_chantable_size ARRAY_SIZE(b43_5ghz_chantable)
  149. #endif
  150. static void b43_wireless_core_exit(struct b43_wldev *dev);
  151. static int b43_wireless_core_init(struct b43_wldev *dev);
  152. static void b43_wireless_core_stop(struct b43_wldev *dev);
  153. static int b43_wireless_core_start(struct b43_wldev *dev);
  154. static int b43_ratelimit(struct b43_wl *wl)
  155. {
  156. if (!wl || !wl->current_dev)
  157. return 1;
  158. if (b43_status(wl->current_dev) < B43_STAT_STARTED)
  159. return 1;
  160. /* We are up and running.
  161. * Ratelimit the messages to avoid DoS over the net. */
  162. return net_ratelimit();
  163. }
  164. void b43info(struct b43_wl *wl, const char *fmt, ...)
  165. {
  166. va_list args;
  167. if (!b43_ratelimit(wl))
  168. return;
  169. va_start(args, fmt);
  170. printk(KERN_INFO "b43-%s: ",
  171. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  172. vprintk(fmt, args);
  173. va_end(args);
  174. }
  175. void b43err(struct b43_wl *wl, const char *fmt, ...)
  176. {
  177. va_list args;
  178. if (!b43_ratelimit(wl))
  179. return;
  180. va_start(args, fmt);
  181. printk(KERN_ERR "b43-%s ERROR: ",
  182. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  183. vprintk(fmt, args);
  184. va_end(args);
  185. }
  186. void b43warn(struct b43_wl *wl, const char *fmt, ...)
  187. {
  188. va_list args;
  189. if (!b43_ratelimit(wl))
  190. return;
  191. va_start(args, fmt);
  192. printk(KERN_WARNING "b43-%s warning: ",
  193. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  194. vprintk(fmt, args);
  195. va_end(args);
  196. }
  197. #if B43_DEBUG
  198. void b43dbg(struct b43_wl *wl, const char *fmt, ...)
  199. {
  200. va_list args;
  201. va_start(args, fmt);
  202. printk(KERN_DEBUG "b43-%s debug: ",
  203. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  204. vprintk(fmt, args);
  205. va_end(args);
  206. }
  207. #endif /* DEBUG */
  208. static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
  209. {
  210. u32 macctl;
  211. B43_WARN_ON(offset % 4 != 0);
  212. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  213. if (macctl & B43_MACCTL_BE)
  214. val = swab32(val);
  215. b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
  216. mmiowb();
  217. b43_write32(dev, B43_MMIO_RAM_DATA, val);
  218. }
  219. static inline
  220. void b43_shm_control_word(struct b43_wldev *dev, u16 routing, u16 offset)
  221. {
  222. u32 control;
  223. /* "offset" is the WORD offset. */
  224. control = routing;
  225. control <<= 16;
  226. control |= offset;
  227. b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
  228. }
  229. u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  230. {
  231. u32 ret;
  232. if (routing == B43_SHM_SHARED) {
  233. B43_WARN_ON(offset & 0x0001);
  234. if (offset & 0x0003) {
  235. /* Unaligned access */
  236. b43_shm_control_word(dev, routing, offset >> 2);
  237. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  238. ret <<= 16;
  239. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  240. ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
  241. return ret;
  242. }
  243. offset >>= 2;
  244. }
  245. b43_shm_control_word(dev, routing, offset);
  246. ret = b43_read32(dev, B43_MMIO_SHM_DATA);
  247. return ret;
  248. }
  249. u16 b43_shm_read16(struct b43_wldev * dev, u16 routing, u16 offset)
  250. {
  251. u16 ret;
  252. if (routing == B43_SHM_SHARED) {
  253. B43_WARN_ON(offset & 0x0001);
  254. if (offset & 0x0003) {
  255. /* Unaligned access */
  256. b43_shm_control_word(dev, routing, offset >> 2);
  257. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  258. return ret;
  259. }
  260. offset >>= 2;
  261. }
  262. b43_shm_control_word(dev, routing, offset);
  263. ret = b43_read16(dev, B43_MMIO_SHM_DATA);
  264. return ret;
  265. }
  266. void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  267. {
  268. if (routing == B43_SHM_SHARED) {
  269. B43_WARN_ON(offset & 0x0001);
  270. if (offset & 0x0003) {
  271. /* Unaligned access */
  272. b43_shm_control_word(dev, routing, offset >> 2);
  273. mmiowb();
  274. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
  275. (value >> 16) & 0xffff);
  276. mmiowb();
  277. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  278. mmiowb();
  279. b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
  280. return;
  281. }
  282. offset >>= 2;
  283. }
  284. b43_shm_control_word(dev, routing, offset);
  285. mmiowb();
  286. b43_write32(dev, B43_MMIO_SHM_DATA, value);
  287. }
  288. void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  289. {
  290. if (routing == B43_SHM_SHARED) {
  291. B43_WARN_ON(offset & 0x0001);
  292. if (offset & 0x0003) {
  293. /* Unaligned access */
  294. b43_shm_control_word(dev, routing, offset >> 2);
  295. mmiowb();
  296. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
  297. return;
  298. }
  299. offset >>= 2;
  300. }
  301. b43_shm_control_word(dev, routing, offset);
  302. mmiowb();
  303. b43_write16(dev, B43_MMIO_SHM_DATA, value);
  304. }
  305. /* Read HostFlags */
  306. u32 b43_hf_read(struct b43_wldev * dev)
  307. {
  308. u32 ret;
  309. ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
  310. ret <<= 16;
  311. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
  312. return ret;
  313. }
  314. /* Write HostFlags */
  315. void b43_hf_write(struct b43_wldev *dev, u32 value)
  316. {
  317. b43_shm_write16(dev, B43_SHM_SHARED,
  318. B43_SHM_SH_HOSTFLO, (value & 0x0000FFFF));
  319. b43_shm_write16(dev, B43_SHM_SHARED,
  320. B43_SHM_SH_HOSTFHI, ((value & 0xFFFF0000) >> 16));
  321. }
  322. void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
  323. {
  324. /* We need to be careful. As we read the TSF from multiple
  325. * registers, we should take care of register overflows.
  326. * In theory, the whole tsf read process should be atomic.
  327. * We try to be atomic here, by restaring the read process,
  328. * if any of the high registers changed (overflew).
  329. */
  330. if (dev->dev->id.revision >= 3) {
  331. u32 low, high, high2;
  332. do {
  333. high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  334. low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
  335. high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  336. } while (unlikely(high != high2));
  337. *tsf = high;
  338. *tsf <<= 32;
  339. *tsf |= low;
  340. } else {
  341. u64 tmp;
  342. u16 v0, v1, v2, v3;
  343. u16 test1, test2, test3;
  344. do {
  345. v3 = b43_read16(dev, B43_MMIO_TSF_3);
  346. v2 = b43_read16(dev, B43_MMIO_TSF_2);
  347. v1 = b43_read16(dev, B43_MMIO_TSF_1);
  348. v0 = b43_read16(dev, B43_MMIO_TSF_0);
  349. test3 = b43_read16(dev, B43_MMIO_TSF_3);
  350. test2 = b43_read16(dev, B43_MMIO_TSF_2);
  351. test1 = b43_read16(dev, B43_MMIO_TSF_1);
  352. } while (v3 != test3 || v2 != test2 || v1 != test1);
  353. *tsf = v3;
  354. *tsf <<= 48;
  355. tmp = v2;
  356. tmp <<= 32;
  357. *tsf |= tmp;
  358. tmp = v1;
  359. tmp <<= 16;
  360. *tsf |= tmp;
  361. *tsf |= v0;
  362. }
  363. }
  364. static void b43_time_lock(struct b43_wldev *dev)
  365. {
  366. u32 macctl;
  367. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  368. macctl |= B43_MACCTL_TBTTHOLD;
  369. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  370. /* Commit the write */
  371. b43_read32(dev, B43_MMIO_MACCTL);
  372. }
  373. static void b43_time_unlock(struct b43_wldev *dev)
  374. {
  375. u32 macctl;
  376. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  377. macctl &= ~B43_MACCTL_TBTTHOLD;
  378. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  379. /* Commit the write */
  380. b43_read32(dev, B43_MMIO_MACCTL);
  381. }
  382. static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
  383. {
  384. /* Be careful with the in-progress timer.
  385. * First zero out the low register, so we have a full
  386. * register-overflow duration to complete the operation.
  387. */
  388. if (dev->dev->id.revision >= 3) {
  389. u32 lo = (tsf & 0x00000000FFFFFFFFULL);
  390. u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
  391. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
  392. mmiowb();
  393. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
  394. mmiowb();
  395. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
  396. } else {
  397. u16 v0 = (tsf & 0x000000000000FFFFULL);
  398. u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
  399. u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
  400. u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
  401. b43_write16(dev, B43_MMIO_TSF_0, 0);
  402. mmiowb();
  403. b43_write16(dev, B43_MMIO_TSF_3, v3);
  404. mmiowb();
  405. b43_write16(dev, B43_MMIO_TSF_2, v2);
  406. mmiowb();
  407. b43_write16(dev, B43_MMIO_TSF_1, v1);
  408. mmiowb();
  409. b43_write16(dev, B43_MMIO_TSF_0, v0);
  410. }
  411. }
  412. void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
  413. {
  414. b43_time_lock(dev);
  415. b43_tsf_write_locked(dev, tsf);
  416. b43_time_unlock(dev);
  417. }
  418. static
  419. void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
  420. {
  421. static const u8 zero_addr[ETH_ALEN] = { 0 };
  422. u16 data;
  423. if (!mac)
  424. mac = zero_addr;
  425. offset |= 0x0020;
  426. b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
  427. data = mac[0];
  428. data |= mac[1] << 8;
  429. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  430. data = mac[2];
  431. data |= mac[3] << 8;
  432. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  433. data = mac[4];
  434. data |= mac[5] << 8;
  435. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  436. }
  437. static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
  438. {
  439. const u8 *mac;
  440. const u8 *bssid;
  441. u8 mac_bssid[ETH_ALEN * 2];
  442. int i;
  443. u32 tmp;
  444. bssid = dev->wl->bssid;
  445. mac = dev->wl->mac_addr;
  446. b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
  447. memcpy(mac_bssid, mac, ETH_ALEN);
  448. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  449. /* Write our MAC address and BSSID to template ram */
  450. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  451. tmp = (u32) (mac_bssid[i + 0]);
  452. tmp |= (u32) (mac_bssid[i + 1]) << 8;
  453. tmp |= (u32) (mac_bssid[i + 2]) << 16;
  454. tmp |= (u32) (mac_bssid[i + 3]) << 24;
  455. b43_ram_write(dev, 0x20 + i, tmp);
  456. }
  457. }
  458. static void b43_upload_card_macaddress(struct b43_wldev *dev)
  459. {
  460. b43_write_mac_bssid_templates(dev);
  461. b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
  462. }
  463. static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
  464. {
  465. /* slot_time is in usec. */
  466. if (dev->phy.type != B43_PHYTYPE_G)
  467. return;
  468. b43_write16(dev, 0x684, 510 + slot_time);
  469. b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
  470. }
  471. static void b43_short_slot_timing_enable(struct b43_wldev *dev)
  472. {
  473. b43_set_slot_time(dev, 9);
  474. dev->short_slot = 1;
  475. }
  476. static void b43_short_slot_timing_disable(struct b43_wldev *dev)
  477. {
  478. b43_set_slot_time(dev, 20);
  479. dev->short_slot = 0;
  480. }
  481. /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
  482. * Returns the _previously_ enabled IRQ mask.
  483. */
  484. static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
  485. {
  486. u32 old_mask;
  487. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  488. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
  489. return old_mask;
  490. }
  491. /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
  492. * Returns the _previously_ enabled IRQ mask.
  493. */
  494. static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
  495. {
  496. u32 old_mask;
  497. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  498. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
  499. return old_mask;
  500. }
  501. /* Synchronize IRQ top- and bottom-half.
  502. * IRQs must be masked before calling this.
  503. * This must not be called with the irq_lock held.
  504. */
  505. static void b43_synchronize_irq(struct b43_wldev *dev)
  506. {
  507. synchronize_irq(dev->dev->irq);
  508. tasklet_kill(&dev->isr_tasklet);
  509. }
  510. /* DummyTransmission function, as documented on
  511. * http://bcm-specs.sipsolutions.net/DummyTransmission
  512. */
  513. void b43_dummy_transmission(struct b43_wldev *dev)
  514. {
  515. struct b43_phy *phy = &dev->phy;
  516. unsigned int i, max_loop;
  517. u16 value;
  518. u32 buffer[5] = {
  519. 0x00000000,
  520. 0x00D40000,
  521. 0x00000000,
  522. 0x01000000,
  523. 0x00000000,
  524. };
  525. switch (phy->type) {
  526. case B43_PHYTYPE_A:
  527. max_loop = 0x1E;
  528. buffer[0] = 0x000201CC;
  529. break;
  530. case B43_PHYTYPE_B:
  531. case B43_PHYTYPE_G:
  532. max_loop = 0xFA;
  533. buffer[0] = 0x000B846E;
  534. break;
  535. default:
  536. B43_WARN_ON(1);
  537. return;
  538. }
  539. for (i = 0; i < 5; i++)
  540. b43_ram_write(dev, i * 4, buffer[i]);
  541. /* Commit writes */
  542. b43_read32(dev, B43_MMIO_MACCTL);
  543. b43_write16(dev, 0x0568, 0x0000);
  544. b43_write16(dev, 0x07C0, 0x0000);
  545. value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
  546. b43_write16(dev, 0x050C, value);
  547. b43_write16(dev, 0x0508, 0x0000);
  548. b43_write16(dev, 0x050A, 0x0000);
  549. b43_write16(dev, 0x054C, 0x0000);
  550. b43_write16(dev, 0x056A, 0x0014);
  551. b43_write16(dev, 0x0568, 0x0826);
  552. b43_write16(dev, 0x0500, 0x0000);
  553. b43_write16(dev, 0x0502, 0x0030);
  554. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  555. b43_radio_write16(dev, 0x0051, 0x0017);
  556. for (i = 0x00; i < max_loop; i++) {
  557. value = b43_read16(dev, 0x050E);
  558. if (value & 0x0080)
  559. break;
  560. udelay(10);
  561. }
  562. for (i = 0x00; i < 0x0A; i++) {
  563. value = b43_read16(dev, 0x050E);
  564. if (value & 0x0400)
  565. break;
  566. udelay(10);
  567. }
  568. for (i = 0x00; i < 0x0A; i++) {
  569. value = b43_read16(dev, 0x0690);
  570. if (!(value & 0x0100))
  571. break;
  572. udelay(10);
  573. }
  574. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  575. b43_radio_write16(dev, 0x0051, 0x0037);
  576. }
  577. static void key_write(struct b43_wldev *dev,
  578. u8 index, u8 algorithm, const u8 * key)
  579. {
  580. unsigned int i;
  581. u32 offset;
  582. u16 value;
  583. u16 kidx;
  584. /* Key index/algo block */
  585. kidx = b43_kidx_to_fw(dev, index);
  586. value = ((kidx << 4) | algorithm);
  587. b43_shm_write16(dev, B43_SHM_SHARED,
  588. B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
  589. /* Write the key to the Key Table Pointer offset */
  590. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  591. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  592. value = key[i];
  593. value |= (u16) (key[i + 1]) << 8;
  594. b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
  595. }
  596. }
  597. static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
  598. {
  599. u32 addrtmp[2] = { 0, 0, };
  600. u8 per_sta_keys_start = 8;
  601. if (b43_new_kidx_api(dev))
  602. per_sta_keys_start = 4;
  603. B43_WARN_ON(index < per_sta_keys_start);
  604. /* We have two default TX keys and possibly two default RX keys.
  605. * Physical mac 0 is mapped to physical key 4 or 8, depending
  606. * on the firmware version.
  607. * So we must adjust the index here.
  608. */
  609. index -= per_sta_keys_start;
  610. if (addr) {
  611. addrtmp[0] = addr[0];
  612. addrtmp[0] |= ((u32) (addr[1]) << 8);
  613. addrtmp[0] |= ((u32) (addr[2]) << 16);
  614. addrtmp[0] |= ((u32) (addr[3]) << 24);
  615. addrtmp[1] = addr[4];
  616. addrtmp[1] |= ((u32) (addr[5]) << 8);
  617. }
  618. if (dev->dev->id.revision >= 5) {
  619. /* Receive match transmitter address mechanism */
  620. b43_shm_write32(dev, B43_SHM_RCMTA,
  621. (index * 2) + 0, addrtmp[0]);
  622. b43_shm_write16(dev, B43_SHM_RCMTA,
  623. (index * 2) + 1, addrtmp[1]);
  624. } else {
  625. /* RXE (Receive Engine) and
  626. * PSM (Programmable State Machine) mechanism
  627. */
  628. if (index < 8) {
  629. /* TODO write to RCM 16, 19, 22 and 25 */
  630. } else {
  631. b43_shm_write32(dev, B43_SHM_SHARED,
  632. B43_SHM_SH_PSM + (index * 6) + 0,
  633. addrtmp[0]);
  634. b43_shm_write16(dev, B43_SHM_SHARED,
  635. B43_SHM_SH_PSM + (index * 6) + 4,
  636. addrtmp[1]);
  637. }
  638. }
  639. }
  640. static void do_key_write(struct b43_wldev *dev,
  641. u8 index, u8 algorithm,
  642. const u8 * key, size_t key_len, const u8 * mac_addr)
  643. {
  644. u8 buf[B43_SEC_KEYSIZE] = { 0, };
  645. u8 per_sta_keys_start = 8;
  646. if (b43_new_kidx_api(dev))
  647. per_sta_keys_start = 4;
  648. B43_WARN_ON(index >= dev->max_nr_keys);
  649. B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
  650. if (index >= per_sta_keys_start)
  651. keymac_write(dev, index, NULL); /* First zero out mac. */
  652. if (key)
  653. memcpy(buf, key, key_len);
  654. key_write(dev, index, algorithm, buf);
  655. if (index >= per_sta_keys_start)
  656. keymac_write(dev, index, mac_addr);
  657. dev->key[index].algorithm = algorithm;
  658. }
  659. static int b43_key_write(struct b43_wldev *dev,
  660. int index, u8 algorithm,
  661. const u8 * key, size_t key_len,
  662. const u8 * mac_addr,
  663. struct ieee80211_key_conf *keyconf)
  664. {
  665. int i;
  666. int sta_keys_start;
  667. if (key_len > B43_SEC_KEYSIZE)
  668. return -EINVAL;
  669. for (i = 0; i < dev->max_nr_keys; i++) {
  670. /* Check that we don't already have this key. */
  671. B43_WARN_ON(dev->key[i].keyconf == keyconf);
  672. }
  673. if (index < 0) {
  674. /* Either pairwise key or address is 00:00:00:00:00:00
  675. * for transmit-only keys. Search the index. */
  676. if (b43_new_kidx_api(dev))
  677. sta_keys_start = 4;
  678. else
  679. sta_keys_start = 8;
  680. for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
  681. if (!dev->key[i].keyconf) {
  682. /* found empty */
  683. index = i;
  684. break;
  685. }
  686. }
  687. if (index < 0) {
  688. b43err(dev->wl, "Out of hardware key memory\n");
  689. return -ENOSPC;
  690. }
  691. } else
  692. B43_WARN_ON(index > 3);
  693. do_key_write(dev, index, algorithm, key, key_len, mac_addr);
  694. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  695. /* Default RX key */
  696. B43_WARN_ON(mac_addr);
  697. do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
  698. }
  699. keyconf->hw_key_idx = index;
  700. dev->key[index].keyconf = keyconf;
  701. return 0;
  702. }
  703. static int b43_key_clear(struct b43_wldev *dev, int index)
  704. {
  705. if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
  706. return -EINVAL;
  707. do_key_write(dev, index, B43_SEC_ALGO_NONE,
  708. NULL, B43_SEC_KEYSIZE, NULL);
  709. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  710. do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
  711. NULL, B43_SEC_KEYSIZE, NULL);
  712. }
  713. dev->key[index].keyconf = NULL;
  714. return 0;
  715. }
  716. static void b43_clear_keys(struct b43_wldev *dev)
  717. {
  718. int i;
  719. for (i = 0; i < dev->max_nr_keys; i++)
  720. b43_key_clear(dev, i);
  721. }
  722. void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
  723. {
  724. u32 macctl;
  725. u16 ucstat;
  726. bool hwps;
  727. bool awake;
  728. int i;
  729. B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
  730. (ps_flags & B43_PS_DISABLED));
  731. B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
  732. if (ps_flags & B43_PS_ENABLED) {
  733. hwps = 1;
  734. } else if (ps_flags & B43_PS_DISABLED) {
  735. hwps = 0;
  736. } else {
  737. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  738. // and thus is not an AP and we are associated, set bit 25
  739. }
  740. if (ps_flags & B43_PS_AWAKE) {
  741. awake = 1;
  742. } else if (ps_flags & B43_PS_ASLEEP) {
  743. awake = 0;
  744. } else {
  745. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  746. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  747. // successful, set bit26
  748. }
  749. /* FIXME: For now we force awake-on and hwps-off */
  750. hwps = 0;
  751. awake = 1;
  752. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  753. if (hwps)
  754. macctl |= B43_MACCTL_HWPS;
  755. else
  756. macctl &= ~B43_MACCTL_HWPS;
  757. if (awake)
  758. macctl |= B43_MACCTL_AWAKE;
  759. else
  760. macctl &= ~B43_MACCTL_AWAKE;
  761. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  762. /* Commit write */
  763. b43_read32(dev, B43_MMIO_MACCTL);
  764. if (awake && dev->dev->id.revision >= 5) {
  765. /* Wait for the microcode to wake up. */
  766. for (i = 0; i < 100; i++) {
  767. ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
  768. B43_SHM_SH_UCODESTAT);
  769. if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
  770. break;
  771. udelay(10);
  772. }
  773. }
  774. }
  775. /* Turn the Analog ON/OFF */
  776. static void b43_switch_analog(struct b43_wldev *dev, int on)
  777. {
  778. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  779. }
  780. void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
  781. {
  782. u32 tmslow;
  783. u32 macctl;
  784. flags |= B43_TMSLOW_PHYCLKEN;
  785. flags |= B43_TMSLOW_PHYRESET;
  786. ssb_device_enable(dev->dev, flags);
  787. msleep(2); /* Wait for the PLL to turn on. */
  788. /* Now take the PHY out of Reset again */
  789. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  790. tmslow |= SSB_TMSLOW_FGC;
  791. tmslow &= ~B43_TMSLOW_PHYRESET;
  792. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  793. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  794. msleep(1);
  795. tmslow &= ~SSB_TMSLOW_FGC;
  796. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  797. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  798. msleep(1);
  799. /* Turn Analog ON */
  800. b43_switch_analog(dev, 1);
  801. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  802. macctl &= ~B43_MACCTL_GMODE;
  803. if (flags & B43_TMSLOW_GMODE)
  804. macctl |= B43_MACCTL_GMODE;
  805. macctl |= B43_MACCTL_IHR_ENABLED;
  806. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  807. }
  808. static void handle_irq_transmit_status(struct b43_wldev *dev)
  809. {
  810. u32 v0, v1;
  811. u16 tmp;
  812. struct b43_txstatus stat;
  813. while (1) {
  814. v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  815. if (!(v0 & 0x00000001))
  816. break;
  817. v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  818. stat.cookie = (v0 >> 16);
  819. stat.seq = (v1 & 0x0000FFFF);
  820. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  821. tmp = (v0 & 0x0000FFFF);
  822. stat.frame_count = ((tmp & 0xF000) >> 12);
  823. stat.rts_count = ((tmp & 0x0F00) >> 8);
  824. stat.supp_reason = ((tmp & 0x001C) >> 2);
  825. stat.pm_indicated = !!(tmp & 0x0080);
  826. stat.intermediate = !!(tmp & 0x0040);
  827. stat.for_ampdu = !!(tmp & 0x0020);
  828. stat.acked = !!(tmp & 0x0002);
  829. b43_handle_txstatus(dev, &stat);
  830. }
  831. }
  832. static void drain_txstatus_queue(struct b43_wldev *dev)
  833. {
  834. u32 dummy;
  835. if (dev->dev->id.revision < 5)
  836. return;
  837. /* Read all entries from the microcode TXstatus FIFO
  838. * and throw them away.
  839. */
  840. while (1) {
  841. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  842. if (!(dummy & 0x00000001))
  843. break;
  844. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  845. }
  846. }
  847. static u32 b43_jssi_read(struct b43_wldev *dev)
  848. {
  849. u32 val = 0;
  850. val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
  851. val <<= 16;
  852. val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
  853. return val;
  854. }
  855. static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
  856. {
  857. b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
  858. b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
  859. }
  860. static void b43_generate_noise_sample(struct b43_wldev *dev)
  861. {
  862. b43_jssi_write(dev, 0x7F7F7F7F);
  863. b43_write32(dev, B43_MMIO_MACCMD,
  864. b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
  865. B43_WARN_ON(dev->noisecalc.channel_at_start != dev->phy.channel);
  866. }
  867. static void b43_calculate_link_quality(struct b43_wldev *dev)
  868. {
  869. /* Top half of Link Quality calculation. */
  870. if (dev->noisecalc.calculation_running)
  871. return;
  872. dev->noisecalc.channel_at_start = dev->phy.channel;
  873. dev->noisecalc.calculation_running = 1;
  874. dev->noisecalc.nr_samples = 0;
  875. b43_generate_noise_sample(dev);
  876. }
  877. static void handle_irq_noise(struct b43_wldev *dev)
  878. {
  879. struct b43_phy *phy = &dev->phy;
  880. u16 tmp;
  881. u8 noise[4];
  882. u8 i, j;
  883. s32 average;
  884. /* Bottom half of Link Quality calculation. */
  885. B43_WARN_ON(!dev->noisecalc.calculation_running);
  886. if (dev->noisecalc.channel_at_start != phy->channel)
  887. goto drop_calculation;
  888. *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
  889. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  890. noise[2] == 0x7F || noise[3] == 0x7F)
  891. goto generate_new;
  892. /* Get the noise samples. */
  893. B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
  894. i = dev->noisecalc.nr_samples;
  895. noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  896. noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  897. noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  898. noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  899. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  900. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  901. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  902. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  903. dev->noisecalc.nr_samples++;
  904. if (dev->noisecalc.nr_samples == 8) {
  905. /* Calculate the Link Quality by the noise samples. */
  906. average = 0;
  907. for (i = 0; i < 8; i++) {
  908. for (j = 0; j < 4; j++)
  909. average += dev->noisecalc.samples[i][j];
  910. }
  911. average /= (8 * 4);
  912. average *= 125;
  913. average += 64;
  914. average /= 128;
  915. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
  916. tmp = (tmp / 128) & 0x1F;
  917. if (tmp >= 8)
  918. average += 2;
  919. else
  920. average -= 25;
  921. if (tmp == 8)
  922. average -= 72;
  923. else
  924. average -= 48;
  925. dev->stats.link_noise = average;
  926. drop_calculation:
  927. dev->noisecalc.calculation_running = 0;
  928. return;
  929. }
  930. generate_new:
  931. b43_generate_noise_sample(dev);
  932. }
  933. static void handle_irq_tbtt_indication(struct b43_wldev *dev)
  934. {
  935. if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
  936. ///TODO: PS TBTT
  937. } else {
  938. if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
  939. b43_power_saving_ctl_bits(dev, 0);
  940. }
  941. if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
  942. dev->dfq_valid = 1;
  943. }
  944. static void handle_irq_atim_end(struct b43_wldev *dev)
  945. {
  946. if (dev->dfq_valid) {
  947. b43_write32(dev, B43_MMIO_MACCMD,
  948. b43_read32(dev, B43_MMIO_MACCMD)
  949. | B43_MACCMD_DFQ_VALID);
  950. dev->dfq_valid = 0;
  951. }
  952. }
  953. static void handle_irq_pmq(struct b43_wldev *dev)
  954. {
  955. u32 tmp;
  956. //TODO: AP mode.
  957. while (1) {
  958. tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
  959. if (!(tmp & 0x00000008))
  960. break;
  961. }
  962. /* 16bit write is odd, but correct. */
  963. b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
  964. }
  965. static void b43_write_template_common(struct b43_wldev *dev,
  966. const u8 * data, u16 size,
  967. u16 ram_offset,
  968. u16 shm_size_offset, u8 rate)
  969. {
  970. u32 i, tmp;
  971. struct b43_plcp_hdr4 plcp;
  972. plcp.data = 0;
  973. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  974. b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  975. ram_offset += sizeof(u32);
  976. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  977. * So leave the first two bytes of the next write blank.
  978. */
  979. tmp = (u32) (data[0]) << 16;
  980. tmp |= (u32) (data[1]) << 24;
  981. b43_ram_write(dev, ram_offset, tmp);
  982. ram_offset += sizeof(u32);
  983. for (i = 2; i < size; i += sizeof(u32)) {
  984. tmp = (u32) (data[i + 0]);
  985. if (i + 1 < size)
  986. tmp |= (u32) (data[i + 1]) << 8;
  987. if (i + 2 < size)
  988. tmp |= (u32) (data[i + 2]) << 16;
  989. if (i + 3 < size)
  990. tmp |= (u32) (data[i + 3]) << 24;
  991. b43_ram_write(dev, ram_offset + i - 2, tmp);
  992. }
  993. b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
  994. size + sizeof(struct b43_plcp_hdr6));
  995. }
  996. static void b43_write_beacon_template(struct b43_wldev *dev,
  997. u16 ram_offset,
  998. u16 shm_size_offset, u8 rate)
  999. {
  1000. int len;
  1001. const u8 *data;
  1002. B43_WARN_ON(!dev->cached_beacon);
  1003. len = min((size_t) dev->cached_beacon->len,
  1004. 0x200 - sizeof(struct b43_plcp_hdr6));
  1005. data = (const u8 *)(dev->cached_beacon->data);
  1006. b43_write_template_common(dev, data,
  1007. len, ram_offset, shm_size_offset, rate);
  1008. }
  1009. static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  1010. u16 shm_offset, u16 size, u8 rate)
  1011. {
  1012. struct b43_plcp_hdr4 plcp;
  1013. u32 tmp;
  1014. __le16 dur;
  1015. plcp.data = 0;
  1016. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  1017. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1018. dev->wl->if_id, size,
  1019. B43_RATE_TO_BASE100KBPS(rate));
  1020. /* Write PLCP in two parts and timing for packet transfer */
  1021. tmp = le32_to_cpu(plcp.data);
  1022. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
  1023. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
  1024. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
  1025. }
  1026. /* Instead of using custom probe response template, this function
  1027. * just patches custom beacon template by:
  1028. * 1) Changing packet type
  1029. * 2) Patching duration field
  1030. * 3) Stripping TIM
  1031. */
  1032. static u8 *b43_generate_probe_resp(struct b43_wldev *dev,
  1033. u16 * dest_size, u8 rate)
  1034. {
  1035. const u8 *src_data;
  1036. u8 *dest_data;
  1037. u16 src_size, elem_size, src_pos, dest_pos;
  1038. __le16 dur;
  1039. struct ieee80211_hdr *hdr;
  1040. B43_WARN_ON(!dev->cached_beacon);
  1041. src_size = dev->cached_beacon->len;
  1042. src_data = (const u8 *)dev->cached_beacon->data;
  1043. if (unlikely(src_size < 0x24)) {
  1044. b43dbg(dev->wl, "b43_generate_probe_resp: " "invalid beacon\n");
  1045. return NULL;
  1046. }
  1047. dest_data = kmalloc(src_size, GFP_ATOMIC);
  1048. if (unlikely(!dest_data))
  1049. return NULL;
  1050. /* 0x24 is offset of first variable-len Information-Element
  1051. * in beacon frame.
  1052. */
  1053. memcpy(dest_data, src_data, 0x24);
  1054. src_pos = dest_pos = 0x24;
  1055. for (; src_pos < src_size - 2; src_pos += elem_size) {
  1056. elem_size = src_data[src_pos + 1] + 2;
  1057. if (src_data[src_pos] != 0x05) { /* TIM */
  1058. memcpy(dest_data + dest_pos, src_data + src_pos,
  1059. elem_size);
  1060. dest_pos += elem_size;
  1061. }
  1062. }
  1063. *dest_size = dest_pos;
  1064. hdr = (struct ieee80211_hdr *)dest_data;
  1065. /* Set the frame control. */
  1066. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1067. IEEE80211_STYPE_PROBE_RESP);
  1068. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1069. dev->wl->if_id, *dest_size,
  1070. B43_RATE_TO_BASE100KBPS(rate));
  1071. hdr->duration_id = dur;
  1072. return dest_data;
  1073. }
  1074. static void b43_write_probe_resp_template(struct b43_wldev *dev,
  1075. u16 ram_offset,
  1076. u16 shm_size_offset, u8 rate)
  1077. {
  1078. u8 *probe_resp_data;
  1079. u16 size;
  1080. B43_WARN_ON(!dev->cached_beacon);
  1081. size = dev->cached_beacon->len;
  1082. probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
  1083. if (unlikely(!probe_resp_data))
  1084. return;
  1085. /* Looks like PLCP headers plus packet timings are stored for
  1086. * all possible basic rates
  1087. */
  1088. b43_write_probe_resp_plcp(dev, 0x31A, size, B43_CCK_RATE_1MB);
  1089. b43_write_probe_resp_plcp(dev, 0x32C, size, B43_CCK_RATE_2MB);
  1090. b43_write_probe_resp_plcp(dev, 0x33E, size, B43_CCK_RATE_5MB);
  1091. b43_write_probe_resp_plcp(dev, 0x350, size, B43_CCK_RATE_11MB);
  1092. size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
  1093. b43_write_template_common(dev, probe_resp_data,
  1094. size, ram_offset, shm_size_offset, rate);
  1095. kfree(probe_resp_data);
  1096. }
  1097. static int b43_refresh_cached_beacon(struct b43_wldev *dev,
  1098. struct sk_buff *beacon)
  1099. {
  1100. if (dev->cached_beacon)
  1101. kfree_skb(dev->cached_beacon);
  1102. dev->cached_beacon = beacon;
  1103. return 0;
  1104. }
  1105. static void b43_update_templates(struct b43_wldev *dev)
  1106. {
  1107. u32 cmd;
  1108. B43_WARN_ON(!dev->cached_beacon);
  1109. b43_write_beacon_template(dev, 0x68, 0x18, B43_CCK_RATE_1MB);
  1110. b43_write_beacon_template(dev, 0x468, 0x1A, B43_CCK_RATE_1MB);
  1111. b43_write_probe_resp_template(dev, 0x268, 0x4A, B43_CCK_RATE_11MB);
  1112. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1113. cmd |= B43_MACCMD_BEACON0_VALID | B43_MACCMD_BEACON1_VALID;
  1114. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1115. }
  1116. static void b43_refresh_templates(struct b43_wldev *dev, struct sk_buff *beacon)
  1117. {
  1118. int err;
  1119. err = b43_refresh_cached_beacon(dev, beacon);
  1120. if (unlikely(err))
  1121. return;
  1122. b43_update_templates(dev);
  1123. }
  1124. static void b43_set_ssid(struct b43_wldev *dev, const u8 * ssid, u8 ssid_len)
  1125. {
  1126. u32 tmp;
  1127. u16 i, len;
  1128. len = min((u16) ssid_len, (u16) 0x100);
  1129. for (i = 0; i < len; i += sizeof(u32)) {
  1130. tmp = (u32) (ssid[i + 0]);
  1131. if (i + 1 < len)
  1132. tmp |= (u32) (ssid[i + 1]) << 8;
  1133. if (i + 2 < len)
  1134. tmp |= (u32) (ssid[i + 2]) << 16;
  1135. if (i + 3 < len)
  1136. tmp |= (u32) (ssid[i + 3]) << 24;
  1137. b43_shm_write32(dev, B43_SHM_SHARED, 0x380 + i, tmp);
  1138. }
  1139. b43_shm_write16(dev, B43_SHM_SHARED, 0x48, len);
  1140. }
  1141. static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
  1142. {
  1143. b43_time_lock(dev);
  1144. if (dev->dev->id.revision >= 3) {
  1145. b43_write32(dev, 0x188, (beacon_int << 16));
  1146. } else {
  1147. b43_write16(dev, 0x606, (beacon_int >> 6));
  1148. b43_write16(dev, 0x610, beacon_int);
  1149. }
  1150. b43_time_unlock(dev);
  1151. }
  1152. static void handle_irq_beacon(struct b43_wldev *dev)
  1153. {
  1154. u32 status;
  1155. if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
  1156. return;
  1157. dev->irq_savedstate &= ~B43_IRQ_BEACON;
  1158. status = b43_read32(dev, B43_MMIO_MACCMD);
  1159. if (!dev->cached_beacon || ((status & 0x1) && (status & 0x2))) {
  1160. /* ACK beacon IRQ. */
  1161. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
  1162. dev->irq_savedstate |= B43_IRQ_BEACON;
  1163. if (dev->cached_beacon)
  1164. kfree_skb(dev->cached_beacon);
  1165. dev->cached_beacon = NULL;
  1166. return;
  1167. }
  1168. if (!(status & 0x1)) {
  1169. b43_write_beacon_template(dev, 0x68, 0x18, B43_CCK_RATE_1MB);
  1170. status |= 0x1;
  1171. b43_write32(dev, B43_MMIO_MACCMD, status);
  1172. }
  1173. if (!(status & 0x2)) {
  1174. b43_write_beacon_template(dev, 0x468, 0x1A, B43_CCK_RATE_1MB);
  1175. status |= 0x2;
  1176. b43_write32(dev, B43_MMIO_MACCMD, status);
  1177. }
  1178. }
  1179. static void handle_irq_ucode_debug(struct b43_wldev *dev)
  1180. {
  1181. //TODO
  1182. }
  1183. /* Interrupt handler bottom-half */
  1184. static void b43_interrupt_tasklet(struct b43_wldev *dev)
  1185. {
  1186. u32 reason;
  1187. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1188. u32 merged_dma_reason = 0;
  1189. int i;
  1190. unsigned long flags;
  1191. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1192. B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
  1193. reason = dev->irq_reason;
  1194. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1195. dma_reason[i] = dev->dma_reason[i];
  1196. merged_dma_reason |= dma_reason[i];
  1197. }
  1198. if (unlikely(reason & B43_IRQ_MAC_TXERR))
  1199. b43err(dev->wl, "MAC transmission error\n");
  1200. if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
  1201. b43err(dev->wl, "PHY transmission error\n");
  1202. rmb();
  1203. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1204. atomic_set(&dev->phy.txerr_cnt,
  1205. B43_PHY_TX_BADNESS_LIMIT);
  1206. b43err(dev->wl, "Too many PHY TX errors, "
  1207. "restarting the controller\n");
  1208. b43_controller_restart(dev, "PHY TX errors");
  1209. }
  1210. }
  1211. if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
  1212. B43_DMAIRQ_NONFATALMASK))) {
  1213. if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
  1214. b43err(dev->wl, "Fatal DMA error: "
  1215. "0x%08X, 0x%08X, 0x%08X, "
  1216. "0x%08X, 0x%08X, 0x%08X\n",
  1217. dma_reason[0], dma_reason[1],
  1218. dma_reason[2], dma_reason[3],
  1219. dma_reason[4], dma_reason[5]);
  1220. b43_controller_restart(dev, "DMA error");
  1221. mmiowb();
  1222. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1223. return;
  1224. }
  1225. if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
  1226. b43err(dev->wl, "DMA error: "
  1227. "0x%08X, 0x%08X, 0x%08X, "
  1228. "0x%08X, 0x%08X, 0x%08X\n",
  1229. dma_reason[0], dma_reason[1],
  1230. dma_reason[2], dma_reason[3],
  1231. dma_reason[4], dma_reason[5]);
  1232. }
  1233. }
  1234. if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
  1235. handle_irq_ucode_debug(dev);
  1236. if (reason & B43_IRQ_TBTT_INDI)
  1237. handle_irq_tbtt_indication(dev);
  1238. if (reason & B43_IRQ_ATIM_END)
  1239. handle_irq_atim_end(dev);
  1240. if (reason & B43_IRQ_BEACON)
  1241. handle_irq_beacon(dev);
  1242. if (reason & B43_IRQ_PMQ)
  1243. handle_irq_pmq(dev);
  1244. if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
  1245. ;/* TODO */
  1246. if (reason & B43_IRQ_NOISESAMPLE_OK)
  1247. handle_irq_noise(dev);
  1248. /* Check the DMA reason registers for received data. */
  1249. if (dma_reason[0] & B43_DMAIRQ_RX_DONE)
  1250. b43_dma_rx(dev->dma.rx_ring0);
  1251. if (dma_reason[3] & B43_DMAIRQ_RX_DONE)
  1252. b43_dma_rx(dev->dma.rx_ring3);
  1253. B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
  1254. B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
  1255. B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
  1256. B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
  1257. if (reason & B43_IRQ_TX_OK)
  1258. handle_irq_transmit_status(dev);
  1259. b43_interrupt_enable(dev, dev->irq_savedstate);
  1260. mmiowb();
  1261. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1262. }
  1263. static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
  1264. {
  1265. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
  1266. b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
  1267. b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
  1268. b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
  1269. b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
  1270. b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
  1271. b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
  1272. }
  1273. /* Interrupt handler top-half */
  1274. static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
  1275. {
  1276. irqreturn_t ret = IRQ_NONE;
  1277. struct b43_wldev *dev = dev_id;
  1278. u32 reason;
  1279. if (!dev)
  1280. return IRQ_NONE;
  1281. spin_lock(&dev->wl->irq_lock);
  1282. if (b43_status(dev) < B43_STAT_STARTED)
  1283. goto out;
  1284. reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1285. if (reason == 0xffffffff) /* shared IRQ */
  1286. goto out;
  1287. ret = IRQ_HANDLED;
  1288. reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1289. if (!reason)
  1290. goto out;
  1291. dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
  1292. & 0x0001DC00;
  1293. dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
  1294. & 0x0000DC00;
  1295. dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
  1296. & 0x0000DC00;
  1297. dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
  1298. & 0x0001DC00;
  1299. dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
  1300. & 0x0000DC00;
  1301. dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
  1302. & 0x0000DC00;
  1303. b43_interrupt_ack(dev, reason);
  1304. /* disable all IRQs. They are enabled again in the bottom half. */
  1305. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1306. /* save the reason code and call our bottom half. */
  1307. dev->irq_reason = reason;
  1308. tasklet_schedule(&dev->isr_tasklet);
  1309. out:
  1310. mmiowb();
  1311. spin_unlock(&dev->wl->irq_lock);
  1312. return ret;
  1313. }
  1314. static void b43_release_firmware(struct b43_wldev *dev)
  1315. {
  1316. release_firmware(dev->fw.ucode);
  1317. dev->fw.ucode = NULL;
  1318. release_firmware(dev->fw.pcm);
  1319. dev->fw.pcm = NULL;
  1320. release_firmware(dev->fw.initvals);
  1321. dev->fw.initvals = NULL;
  1322. release_firmware(dev->fw.initvals_band);
  1323. dev->fw.initvals_band = NULL;
  1324. }
  1325. static void b43_print_fw_helptext(struct b43_wl *wl)
  1326. {
  1327. b43err(wl, "You must go to "
  1328. "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
  1329. "and download the correct firmware (version 4).\n");
  1330. }
  1331. static int do_request_fw(struct b43_wldev *dev,
  1332. const char *name,
  1333. const struct firmware **fw)
  1334. {
  1335. char path[sizeof(modparam_fwpostfix) + 32];
  1336. struct b43_fw_header *hdr;
  1337. u32 size;
  1338. int err;
  1339. if (!name)
  1340. return 0;
  1341. snprintf(path, ARRAY_SIZE(path),
  1342. "b43%s/%s.fw",
  1343. modparam_fwpostfix, name);
  1344. err = request_firmware(fw, path, dev->dev->dev);
  1345. if (err) {
  1346. b43err(dev->wl, "Firmware file \"%s\" not found "
  1347. "or load failed.\n", path);
  1348. return err;
  1349. }
  1350. if ((*fw)->size < sizeof(struct b43_fw_header))
  1351. goto err_format;
  1352. hdr = (struct b43_fw_header *)((*fw)->data);
  1353. switch (hdr->type) {
  1354. case B43_FW_TYPE_UCODE:
  1355. case B43_FW_TYPE_PCM:
  1356. size = be32_to_cpu(hdr->size);
  1357. if (size != (*fw)->size - sizeof(struct b43_fw_header))
  1358. goto err_format;
  1359. /* fallthrough */
  1360. case B43_FW_TYPE_IV:
  1361. if (hdr->ver != 1)
  1362. goto err_format;
  1363. break;
  1364. default:
  1365. goto err_format;
  1366. }
  1367. return err;
  1368. err_format:
  1369. b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1370. return -EPROTO;
  1371. }
  1372. static int b43_request_firmware(struct b43_wldev *dev)
  1373. {
  1374. struct b43_firmware *fw = &dev->fw;
  1375. const u8 rev = dev->dev->id.revision;
  1376. const char *filename;
  1377. u32 tmshigh;
  1378. int err;
  1379. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1380. if (!fw->ucode) {
  1381. if ((rev >= 5) && (rev <= 10))
  1382. filename = "ucode5";
  1383. else if ((rev >= 11) && (rev <= 12))
  1384. filename = "ucode11";
  1385. else if (rev >= 13)
  1386. filename = "ucode13";
  1387. else
  1388. goto err_no_ucode;
  1389. err = do_request_fw(dev, filename, &fw->ucode);
  1390. if (err)
  1391. goto err_load;
  1392. }
  1393. if (!fw->pcm) {
  1394. if ((rev >= 5) && (rev <= 10))
  1395. filename = "pcm5";
  1396. else if (rev >= 11)
  1397. filename = NULL;
  1398. else
  1399. goto err_no_pcm;
  1400. err = do_request_fw(dev, filename, &fw->pcm);
  1401. if (err)
  1402. goto err_load;
  1403. }
  1404. if (!fw->initvals) {
  1405. switch (dev->phy.type) {
  1406. case B43_PHYTYPE_A:
  1407. if ((rev >= 5) && (rev <= 10)) {
  1408. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1409. filename = "a0g1initvals5";
  1410. else
  1411. filename = "a0g0initvals5";
  1412. } else
  1413. goto err_no_initvals;
  1414. break;
  1415. case B43_PHYTYPE_G:
  1416. if ((rev >= 5) && (rev <= 10))
  1417. filename = "b0g0initvals5";
  1418. else if (rev >= 13)
  1419. filename = "lp0initvals13";
  1420. else
  1421. goto err_no_initvals;
  1422. break;
  1423. default:
  1424. goto err_no_initvals;
  1425. }
  1426. err = do_request_fw(dev, filename, &fw->initvals);
  1427. if (err)
  1428. goto err_load;
  1429. }
  1430. if (!fw->initvals_band) {
  1431. switch (dev->phy.type) {
  1432. case B43_PHYTYPE_A:
  1433. if ((rev >= 5) && (rev <= 10)) {
  1434. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1435. filename = "a0g1bsinitvals5";
  1436. else
  1437. filename = "a0g0bsinitvals5";
  1438. } else if (rev >= 11)
  1439. filename = NULL;
  1440. else
  1441. goto err_no_initvals;
  1442. break;
  1443. case B43_PHYTYPE_G:
  1444. if ((rev >= 5) && (rev <= 10))
  1445. filename = "b0g0bsinitvals5";
  1446. else if (rev >= 11)
  1447. filename = NULL;
  1448. else
  1449. goto err_no_initvals;
  1450. break;
  1451. default:
  1452. goto err_no_initvals;
  1453. }
  1454. err = do_request_fw(dev, filename, &fw->initvals_band);
  1455. if (err)
  1456. goto err_load;
  1457. }
  1458. return 0;
  1459. err_load:
  1460. b43_print_fw_helptext(dev->wl);
  1461. goto error;
  1462. err_no_ucode:
  1463. err = -ENODEV;
  1464. b43err(dev->wl, "No microcode available for core rev %u\n", rev);
  1465. goto error;
  1466. err_no_pcm:
  1467. err = -ENODEV;
  1468. b43err(dev->wl, "No PCM available for core rev %u\n", rev);
  1469. goto error;
  1470. err_no_initvals:
  1471. err = -ENODEV;
  1472. b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
  1473. "core rev %u\n", dev->phy.type, rev);
  1474. goto error;
  1475. error:
  1476. b43_release_firmware(dev);
  1477. return err;
  1478. }
  1479. static int b43_upload_microcode(struct b43_wldev *dev)
  1480. {
  1481. const size_t hdr_len = sizeof(struct b43_fw_header);
  1482. const __be32 *data;
  1483. unsigned int i, len;
  1484. u16 fwrev, fwpatch, fwdate, fwtime;
  1485. u32 tmp;
  1486. int err = 0;
  1487. /* Upload Microcode. */
  1488. data = (__be32 *) (dev->fw.ucode->data + hdr_len);
  1489. len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
  1490. b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
  1491. for (i = 0; i < len; i++) {
  1492. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1493. udelay(10);
  1494. }
  1495. if (dev->fw.pcm) {
  1496. /* Upload PCM data. */
  1497. data = (__be32 *) (dev->fw.pcm->data + hdr_len);
  1498. len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
  1499. b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
  1500. b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
  1501. /* No need for autoinc bit in SHM_HW */
  1502. b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
  1503. for (i = 0; i < len; i++) {
  1504. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1505. udelay(10);
  1506. }
  1507. }
  1508. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
  1509. b43_write32(dev, B43_MMIO_MACCTL,
  1510. B43_MACCTL_PSM_RUN |
  1511. B43_MACCTL_IHR_ENABLED | B43_MACCTL_INFRA);
  1512. /* Wait for the microcode to load and respond */
  1513. i = 0;
  1514. while (1) {
  1515. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1516. if (tmp == B43_IRQ_MAC_SUSPENDED)
  1517. break;
  1518. i++;
  1519. if (i >= 50) {
  1520. b43err(dev->wl, "Microcode not responding\n");
  1521. b43_print_fw_helptext(dev->wl);
  1522. err = -ENODEV;
  1523. goto out;
  1524. }
  1525. udelay(10);
  1526. }
  1527. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
  1528. /* Get and check the revisions. */
  1529. fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
  1530. fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
  1531. fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
  1532. fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
  1533. if (fwrev <= 0x128) {
  1534. b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
  1535. "binary drivers older than version 4.x is unsupported. "
  1536. "You must upgrade your firmware files.\n");
  1537. b43_print_fw_helptext(dev->wl);
  1538. b43_write32(dev, B43_MMIO_MACCTL, 0);
  1539. err = -EOPNOTSUPP;
  1540. goto out;
  1541. }
  1542. b43dbg(dev->wl, "Loading firmware version %u.%u "
  1543. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
  1544. fwrev, fwpatch,
  1545. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1546. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
  1547. dev->fw.rev = fwrev;
  1548. dev->fw.patch = fwpatch;
  1549. out:
  1550. return err;
  1551. }
  1552. static int b43_write_initvals(struct b43_wldev *dev,
  1553. const struct b43_iv *ivals,
  1554. size_t count,
  1555. size_t array_size)
  1556. {
  1557. const struct b43_iv *iv;
  1558. u16 offset;
  1559. size_t i;
  1560. bool bit32;
  1561. BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
  1562. iv = ivals;
  1563. for (i = 0; i < count; i++) {
  1564. if (array_size < sizeof(iv->offset_size))
  1565. goto err_format;
  1566. array_size -= sizeof(iv->offset_size);
  1567. offset = be16_to_cpu(iv->offset_size);
  1568. bit32 = !!(offset & B43_IV_32BIT);
  1569. offset &= B43_IV_OFFSET_MASK;
  1570. if (offset >= 0x1000)
  1571. goto err_format;
  1572. if (bit32) {
  1573. u32 value;
  1574. if (array_size < sizeof(iv->data.d32))
  1575. goto err_format;
  1576. array_size -= sizeof(iv->data.d32);
  1577. value = be32_to_cpu(get_unaligned(&iv->data.d32));
  1578. b43_write32(dev, offset, value);
  1579. iv = (const struct b43_iv *)((const uint8_t *)iv +
  1580. sizeof(__be16) +
  1581. sizeof(__be32));
  1582. } else {
  1583. u16 value;
  1584. if (array_size < sizeof(iv->data.d16))
  1585. goto err_format;
  1586. array_size -= sizeof(iv->data.d16);
  1587. value = be16_to_cpu(iv->data.d16);
  1588. b43_write16(dev, offset, value);
  1589. iv = (const struct b43_iv *)((const uint8_t *)iv +
  1590. sizeof(__be16) +
  1591. sizeof(__be16));
  1592. }
  1593. }
  1594. if (array_size)
  1595. goto err_format;
  1596. return 0;
  1597. err_format:
  1598. b43err(dev->wl, "Initial Values Firmware file-format error.\n");
  1599. b43_print_fw_helptext(dev->wl);
  1600. return -EPROTO;
  1601. }
  1602. static int b43_upload_initvals(struct b43_wldev *dev)
  1603. {
  1604. const size_t hdr_len = sizeof(struct b43_fw_header);
  1605. const struct b43_fw_header *hdr;
  1606. struct b43_firmware *fw = &dev->fw;
  1607. const struct b43_iv *ivals;
  1608. size_t count;
  1609. int err;
  1610. hdr = (const struct b43_fw_header *)(fw->initvals->data);
  1611. ivals = (const struct b43_iv *)(fw->initvals->data + hdr_len);
  1612. count = be32_to_cpu(hdr->size);
  1613. err = b43_write_initvals(dev, ivals, count,
  1614. fw->initvals->size - hdr_len);
  1615. if (err)
  1616. goto out;
  1617. if (fw->initvals_band) {
  1618. hdr = (const struct b43_fw_header *)(fw->initvals_band->data);
  1619. ivals = (const struct b43_iv *)(fw->initvals_band->data + hdr_len);
  1620. count = be32_to_cpu(hdr->size);
  1621. err = b43_write_initvals(dev, ivals, count,
  1622. fw->initvals_band->size - hdr_len);
  1623. if (err)
  1624. goto out;
  1625. }
  1626. out:
  1627. return err;
  1628. }
  1629. /* Initialize the GPIOs
  1630. * http://bcm-specs.sipsolutions.net/GPIO
  1631. */
  1632. static int b43_gpio_init(struct b43_wldev *dev)
  1633. {
  1634. struct ssb_bus *bus = dev->dev->bus;
  1635. struct ssb_device *gpiodev, *pcidev = NULL;
  1636. u32 mask, set;
  1637. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1638. & ~B43_MACCTL_GPOUTSMSK);
  1639. b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
  1640. | 0x000F);
  1641. mask = 0x0000001F;
  1642. set = 0x0000000F;
  1643. if (dev->dev->bus->chip_id == 0x4301) {
  1644. mask |= 0x0060;
  1645. set |= 0x0060;
  1646. }
  1647. if (0 /* FIXME: conditional unknown */ ) {
  1648. b43_write16(dev, B43_MMIO_GPIO_MASK,
  1649. b43_read16(dev, B43_MMIO_GPIO_MASK)
  1650. | 0x0100);
  1651. mask |= 0x0180;
  1652. set |= 0x0180;
  1653. }
  1654. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
  1655. b43_write16(dev, B43_MMIO_GPIO_MASK,
  1656. b43_read16(dev, B43_MMIO_GPIO_MASK)
  1657. | 0x0200);
  1658. mask |= 0x0200;
  1659. set |= 0x0200;
  1660. }
  1661. if (dev->dev->id.revision >= 2)
  1662. mask |= 0x0010; /* FIXME: This is redundant. */
  1663. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1664. pcidev = bus->pcicore.dev;
  1665. #endif
  1666. gpiodev = bus->chipco.dev ? : pcidev;
  1667. if (!gpiodev)
  1668. return 0;
  1669. ssb_write32(gpiodev, B43_GPIO_CONTROL,
  1670. (ssb_read32(gpiodev, B43_GPIO_CONTROL)
  1671. & mask) | set);
  1672. return 0;
  1673. }
  1674. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  1675. static void b43_gpio_cleanup(struct b43_wldev *dev)
  1676. {
  1677. struct ssb_bus *bus = dev->dev->bus;
  1678. struct ssb_device *gpiodev, *pcidev = NULL;
  1679. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1680. pcidev = bus->pcicore.dev;
  1681. #endif
  1682. gpiodev = bus->chipco.dev ? : pcidev;
  1683. if (!gpiodev)
  1684. return;
  1685. ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
  1686. }
  1687. /* http://bcm-specs.sipsolutions.net/EnableMac */
  1688. void b43_mac_enable(struct b43_wldev *dev)
  1689. {
  1690. dev->mac_suspended--;
  1691. B43_WARN_ON(dev->mac_suspended < 0);
  1692. B43_WARN_ON(irqs_disabled());
  1693. if (dev->mac_suspended == 0) {
  1694. b43_write32(dev, B43_MMIO_MACCTL,
  1695. b43_read32(dev, B43_MMIO_MACCTL)
  1696. | B43_MACCTL_ENABLED);
  1697. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  1698. B43_IRQ_MAC_SUSPENDED);
  1699. /* Commit writes */
  1700. b43_read32(dev, B43_MMIO_MACCTL);
  1701. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1702. b43_power_saving_ctl_bits(dev, 0);
  1703. /* Re-enable IRQs. */
  1704. spin_lock_irq(&dev->wl->irq_lock);
  1705. b43_interrupt_enable(dev, dev->irq_savedstate);
  1706. spin_unlock_irq(&dev->wl->irq_lock);
  1707. }
  1708. }
  1709. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1710. void b43_mac_suspend(struct b43_wldev *dev)
  1711. {
  1712. int i;
  1713. u32 tmp;
  1714. might_sleep();
  1715. B43_WARN_ON(irqs_disabled());
  1716. B43_WARN_ON(dev->mac_suspended < 0);
  1717. if (dev->mac_suspended == 0) {
  1718. /* Mask IRQs before suspending MAC. Otherwise
  1719. * the MAC stays busy and won't suspend. */
  1720. spin_lock_irq(&dev->wl->irq_lock);
  1721. tmp = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1722. spin_unlock_irq(&dev->wl->irq_lock);
  1723. b43_synchronize_irq(dev);
  1724. dev->irq_savedstate = tmp;
  1725. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  1726. b43_write32(dev, B43_MMIO_MACCTL,
  1727. b43_read32(dev, B43_MMIO_MACCTL)
  1728. & ~B43_MACCTL_ENABLED);
  1729. /* force pci to flush the write */
  1730. b43_read32(dev, B43_MMIO_MACCTL);
  1731. for (i = 40; i; i--) {
  1732. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1733. if (tmp & B43_IRQ_MAC_SUSPENDED)
  1734. goto out;
  1735. msleep(1);
  1736. }
  1737. b43err(dev->wl, "MAC suspend failed\n");
  1738. }
  1739. out:
  1740. dev->mac_suspended++;
  1741. }
  1742. static void b43_adjust_opmode(struct b43_wldev *dev)
  1743. {
  1744. struct b43_wl *wl = dev->wl;
  1745. u32 ctl;
  1746. u16 cfp_pretbtt;
  1747. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  1748. /* Reset status to STA infrastructure mode. */
  1749. ctl &= ~B43_MACCTL_AP;
  1750. ctl &= ~B43_MACCTL_KEEP_CTL;
  1751. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  1752. ctl &= ~B43_MACCTL_KEEP_BAD;
  1753. ctl &= ~B43_MACCTL_PROMISC;
  1754. ctl &= ~B43_MACCTL_BEACPROMISC;
  1755. ctl |= B43_MACCTL_INFRA;
  1756. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  1757. ctl |= B43_MACCTL_AP;
  1758. else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
  1759. ctl &= ~B43_MACCTL_INFRA;
  1760. if (wl->filter_flags & FIF_CONTROL)
  1761. ctl |= B43_MACCTL_KEEP_CTL;
  1762. if (wl->filter_flags & FIF_FCSFAIL)
  1763. ctl |= B43_MACCTL_KEEP_BAD;
  1764. if (wl->filter_flags & FIF_PLCPFAIL)
  1765. ctl |= B43_MACCTL_KEEP_BADPLCP;
  1766. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1767. ctl |= B43_MACCTL_PROMISC;
  1768. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1769. ctl |= B43_MACCTL_BEACPROMISC;
  1770. /* Workaround: On old hardware the HW-MAC-address-filter
  1771. * doesn't work properly, so always run promisc in filter
  1772. * it in software. */
  1773. if (dev->dev->id.revision <= 4)
  1774. ctl |= B43_MACCTL_PROMISC;
  1775. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  1776. cfp_pretbtt = 2;
  1777. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  1778. if (dev->dev->bus->chip_id == 0x4306 &&
  1779. dev->dev->bus->chip_rev == 3)
  1780. cfp_pretbtt = 100;
  1781. else
  1782. cfp_pretbtt = 50;
  1783. }
  1784. b43_write16(dev, 0x612, cfp_pretbtt);
  1785. }
  1786. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  1787. {
  1788. u16 offset;
  1789. if (is_ofdm) {
  1790. offset = 0x480;
  1791. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1792. } else {
  1793. offset = 0x4C0;
  1794. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1795. }
  1796. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  1797. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  1798. }
  1799. static void b43_rate_memory_init(struct b43_wldev *dev)
  1800. {
  1801. switch (dev->phy.type) {
  1802. case B43_PHYTYPE_A:
  1803. case B43_PHYTYPE_G:
  1804. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  1805. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  1806. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  1807. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  1808. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  1809. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  1810. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  1811. if (dev->phy.type == B43_PHYTYPE_A)
  1812. break;
  1813. /* fallthrough */
  1814. case B43_PHYTYPE_B:
  1815. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  1816. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  1817. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  1818. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  1819. break;
  1820. default:
  1821. B43_WARN_ON(1);
  1822. }
  1823. }
  1824. /* Set the TX-Antenna for management frames sent by firmware. */
  1825. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  1826. {
  1827. u16 ant = 0;
  1828. u16 tmp;
  1829. switch (antenna) {
  1830. case B43_ANTENNA0:
  1831. ant |= B43_TX4_PHY_ANT0;
  1832. break;
  1833. case B43_ANTENNA1:
  1834. ant |= B43_TX4_PHY_ANT1;
  1835. break;
  1836. case B43_ANTENNA_AUTO:
  1837. ant |= B43_TX4_PHY_ANTLAST;
  1838. break;
  1839. default:
  1840. B43_WARN_ON(1);
  1841. }
  1842. /* FIXME We also need to set the other flags of the PHY control field somewhere. */
  1843. /* For Beacons */
  1844. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1845. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1846. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, tmp);
  1847. /* For ACK/CTS */
  1848. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  1849. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1850. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  1851. /* For Probe Resposes */
  1852. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  1853. tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
  1854. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  1855. }
  1856. /* This is the opposite of b43_chip_init() */
  1857. static void b43_chip_exit(struct b43_wldev *dev)
  1858. {
  1859. b43_radio_turn_off(dev, 1);
  1860. b43_gpio_cleanup(dev);
  1861. /* firmware is released later */
  1862. }
  1863. /* Initialize the chip
  1864. * http://bcm-specs.sipsolutions.net/ChipInit
  1865. */
  1866. static int b43_chip_init(struct b43_wldev *dev)
  1867. {
  1868. struct b43_phy *phy = &dev->phy;
  1869. int err, tmp;
  1870. u32 value32;
  1871. u16 value16;
  1872. b43_write32(dev, B43_MMIO_MACCTL,
  1873. B43_MACCTL_PSM_JMP0 | B43_MACCTL_IHR_ENABLED);
  1874. err = b43_request_firmware(dev);
  1875. if (err)
  1876. goto out;
  1877. err = b43_upload_microcode(dev);
  1878. if (err)
  1879. goto out; /* firmware is released later */
  1880. err = b43_gpio_init(dev);
  1881. if (err)
  1882. goto out; /* firmware is released later */
  1883. err = b43_upload_initvals(dev);
  1884. if (err)
  1885. goto err_gpio_clean;
  1886. b43_radio_turn_on(dev);
  1887. b43_write16(dev, 0x03E6, 0x0000);
  1888. err = b43_phy_init(dev);
  1889. if (err)
  1890. goto err_radio_off;
  1891. /* Select initial Interference Mitigation. */
  1892. tmp = phy->interfmode;
  1893. phy->interfmode = B43_INTERFMODE_NONE;
  1894. b43_radio_set_interference_mitigation(dev, tmp);
  1895. b43_set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  1896. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  1897. if (phy->type == B43_PHYTYPE_B) {
  1898. value16 = b43_read16(dev, 0x005E);
  1899. value16 |= 0x0004;
  1900. b43_write16(dev, 0x005E, value16);
  1901. }
  1902. b43_write32(dev, 0x0100, 0x01000000);
  1903. if (dev->dev->id.revision < 5)
  1904. b43_write32(dev, 0x010C, 0x01000000);
  1905. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1906. & ~B43_MACCTL_INFRA);
  1907. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  1908. | B43_MACCTL_INFRA);
  1909. /* Probe Response Timeout value */
  1910. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  1911. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  1912. /* Initially set the wireless operation mode. */
  1913. b43_adjust_opmode(dev);
  1914. if (dev->dev->id.revision < 3) {
  1915. b43_write16(dev, 0x060E, 0x0000);
  1916. b43_write16(dev, 0x0610, 0x8000);
  1917. b43_write16(dev, 0x0604, 0x0000);
  1918. b43_write16(dev, 0x0606, 0x0200);
  1919. } else {
  1920. b43_write32(dev, 0x0188, 0x80000000);
  1921. b43_write32(dev, 0x018C, 0x02000000);
  1922. }
  1923. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  1924. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  1925. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  1926. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  1927. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  1928. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  1929. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  1930. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  1931. value32 |= 0x00100000;
  1932. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  1933. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  1934. dev->dev->bus->chipco.fast_pwrup_delay);
  1935. err = 0;
  1936. b43dbg(dev->wl, "Chip initialized\n");
  1937. out:
  1938. return err;
  1939. err_radio_off:
  1940. b43_radio_turn_off(dev, 1);
  1941. err_gpio_clean:
  1942. b43_gpio_cleanup(dev);
  1943. return err;
  1944. }
  1945. static void b43_periodic_every120sec(struct b43_wldev *dev)
  1946. {
  1947. struct b43_phy *phy = &dev->phy;
  1948. if (phy->type != B43_PHYTYPE_G || phy->rev < 2)
  1949. return;
  1950. b43_mac_suspend(dev);
  1951. b43_lo_g_measure(dev);
  1952. b43_mac_enable(dev);
  1953. if (b43_has_hardware_pctl(phy))
  1954. b43_lo_g_ctl_mark_all_unused(dev);
  1955. }
  1956. static void b43_periodic_every60sec(struct b43_wldev *dev)
  1957. {
  1958. struct b43_phy *phy = &dev->phy;
  1959. if (!b43_has_hardware_pctl(phy))
  1960. b43_lo_g_ctl_mark_all_unused(dev);
  1961. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI) {
  1962. b43_mac_suspend(dev);
  1963. b43_calc_nrssi_slope(dev);
  1964. if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 8)) {
  1965. u8 old_chan = phy->channel;
  1966. /* VCO Calibration */
  1967. if (old_chan >= 8)
  1968. b43_radio_selectchannel(dev, 1, 0);
  1969. else
  1970. b43_radio_selectchannel(dev, 13, 0);
  1971. b43_radio_selectchannel(dev, old_chan, 0);
  1972. }
  1973. b43_mac_enable(dev);
  1974. }
  1975. }
  1976. static void b43_periodic_every30sec(struct b43_wldev *dev)
  1977. {
  1978. /* Update device statistics. */
  1979. b43_calculate_link_quality(dev);
  1980. }
  1981. static void b43_periodic_every15sec(struct b43_wldev *dev)
  1982. {
  1983. struct b43_phy *phy = &dev->phy;
  1984. if (phy->type == B43_PHYTYPE_G) {
  1985. //TODO: update_aci_moving_average
  1986. if (phy->aci_enable && phy->aci_wlan_automatic) {
  1987. b43_mac_suspend(dev);
  1988. if (!phy->aci_enable && 1 /*TODO: not scanning? */ ) {
  1989. if (0 /*TODO: bunch of conditions */ ) {
  1990. b43_radio_set_interference_mitigation
  1991. (dev, B43_INTERFMODE_MANUALWLAN);
  1992. }
  1993. } else if (1 /*TODO*/) {
  1994. /*
  1995. if ((aci_average > 1000) && !(b43_radio_aci_scan(dev))) {
  1996. b43_radio_set_interference_mitigation(dev,
  1997. B43_INTERFMODE_NONE);
  1998. }
  1999. */
  2000. }
  2001. b43_mac_enable(dev);
  2002. } else if (phy->interfmode == B43_INTERFMODE_NONWLAN &&
  2003. phy->rev == 1) {
  2004. //TODO: implement rev1 workaround
  2005. }
  2006. }
  2007. b43_phy_xmitpower(dev); //FIXME: unless scanning?
  2008. //TODO for APHY (temperature?)
  2009. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2010. wmb();
  2011. }
  2012. static void do_periodic_work(struct b43_wldev *dev)
  2013. {
  2014. unsigned int state;
  2015. state = dev->periodic_state;
  2016. if (state % 8 == 0)
  2017. b43_periodic_every120sec(dev);
  2018. if (state % 4 == 0)
  2019. b43_periodic_every60sec(dev);
  2020. if (state % 2 == 0)
  2021. b43_periodic_every30sec(dev);
  2022. b43_periodic_every15sec(dev);
  2023. }
  2024. /* Periodic work locking policy:
  2025. * The whole periodic work handler is protected by
  2026. * wl->mutex. If another lock is needed somewhere in the
  2027. * pwork callchain, it's aquired in-place, where it's needed.
  2028. */
  2029. static void b43_periodic_work_handler(struct work_struct *work)
  2030. {
  2031. struct b43_wldev *dev = container_of(work, struct b43_wldev,
  2032. periodic_work.work);
  2033. struct b43_wl *wl = dev->wl;
  2034. unsigned long delay;
  2035. mutex_lock(&wl->mutex);
  2036. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2037. goto out;
  2038. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2039. goto out_requeue;
  2040. do_periodic_work(dev);
  2041. dev->periodic_state++;
  2042. out_requeue:
  2043. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2044. delay = msecs_to_jiffies(50);
  2045. else
  2046. delay = round_jiffies_relative(HZ * 15);
  2047. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  2048. out:
  2049. mutex_unlock(&wl->mutex);
  2050. }
  2051. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2052. {
  2053. struct delayed_work *work = &dev->periodic_work;
  2054. dev->periodic_state = 0;
  2055. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2056. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2057. }
  2058. /* Check if communication with the device works correctly. */
  2059. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2060. {
  2061. u32 v, backup;
  2062. backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2063. /* Check for read/write and endianness problems. */
  2064. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2065. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2066. goto error;
  2067. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2068. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2069. goto error;
  2070. b43_shm_write32(dev, B43_SHM_SHARED, 0, backup);
  2071. if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
  2072. /* The 32bit register shadows the two 16bit registers
  2073. * with update sideeffects. Validate this. */
  2074. b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
  2075. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
  2076. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
  2077. goto error;
  2078. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
  2079. goto error;
  2080. }
  2081. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
  2082. v = b43_read32(dev, B43_MMIO_MACCTL);
  2083. v |= B43_MACCTL_GMODE;
  2084. if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2085. goto error;
  2086. return 0;
  2087. error:
  2088. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2089. return -ENODEV;
  2090. }
  2091. static void b43_security_init(struct b43_wldev *dev)
  2092. {
  2093. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2094. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2095. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2096. /* KTP is a word address, but we address SHM bytewise.
  2097. * So multiply by two.
  2098. */
  2099. dev->ktp *= 2;
  2100. if (dev->dev->id.revision >= 5) {
  2101. /* Number of RCMTA address slots */
  2102. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2103. }
  2104. b43_clear_keys(dev);
  2105. }
  2106. static int b43_rng_read(struct hwrng *rng, u32 * data)
  2107. {
  2108. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2109. unsigned long flags;
  2110. /* Don't take wl->mutex here, as it could deadlock with
  2111. * hwrng internal locking. It's not needed to take
  2112. * wl->mutex here, anyway. */
  2113. spin_lock_irqsave(&wl->irq_lock, flags);
  2114. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2115. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2116. return (sizeof(u16));
  2117. }
  2118. static void b43_rng_exit(struct b43_wl *wl)
  2119. {
  2120. if (wl->rng_initialized)
  2121. hwrng_unregister(&wl->rng);
  2122. }
  2123. static int b43_rng_init(struct b43_wl *wl)
  2124. {
  2125. int err;
  2126. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2127. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2128. wl->rng.name = wl->rng_name;
  2129. wl->rng.data_read = b43_rng_read;
  2130. wl->rng.priv = (unsigned long)wl;
  2131. wl->rng_initialized = 1;
  2132. err = hwrng_register(&wl->rng);
  2133. if (err) {
  2134. wl->rng_initialized = 0;
  2135. b43err(wl, "Failed to register the random "
  2136. "number generator (%d)\n", err);
  2137. }
  2138. return err;
  2139. }
  2140. static int b43_op_tx(struct ieee80211_hw *hw,
  2141. struct sk_buff *skb,
  2142. struct ieee80211_tx_control *ctl)
  2143. {
  2144. struct b43_wl *wl = hw_to_b43_wl(hw);
  2145. struct b43_wldev *dev = wl->current_dev;
  2146. int err = -ENODEV;
  2147. if (unlikely(!dev))
  2148. goto out;
  2149. if (unlikely(b43_status(dev) < B43_STAT_STARTED))
  2150. goto out;
  2151. /* DMA-TX is done without a global lock. */
  2152. err = b43_dma_tx(dev, skb, ctl);
  2153. out:
  2154. if (unlikely(err))
  2155. return NETDEV_TX_BUSY;
  2156. return NETDEV_TX_OK;
  2157. }
  2158. static int b43_op_conf_tx(struct ieee80211_hw *hw,
  2159. int queue,
  2160. const struct ieee80211_tx_queue_params *params)
  2161. {
  2162. return 0;
  2163. }
  2164. static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
  2165. struct ieee80211_tx_queue_stats *stats)
  2166. {
  2167. struct b43_wl *wl = hw_to_b43_wl(hw);
  2168. struct b43_wldev *dev = wl->current_dev;
  2169. unsigned long flags;
  2170. int err = -ENODEV;
  2171. if (!dev)
  2172. goto out;
  2173. spin_lock_irqsave(&wl->irq_lock, flags);
  2174. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2175. b43_dma_get_tx_stats(dev, stats);
  2176. err = 0;
  2177. }
  2178. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2179. out:
  2180. return err;
  2181. }
  2182. static int b43_op_get_stats(struct ieee80211_hw *hw,
  2183. struct ieee80211_low_level_stats *stats)
  2184. {
  2185. struct b43_wl *wl = hw_to_b43_wl(hw);
  2186. unsigned long flags;
  2187. spin_lock_irqsave(&wl->irq_lock, flags);
  2188. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2189. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2190. return 0;
  2191. }
  2192. static const char *phymode_to_string(unsigned int phymode)
  2193. {
  2194. switch (phymode) {
  2195. case B43_PHYMODE_A:
  2196. return "A";
  2197. case B43_PHYMODE_B:
  2198. return "B";
  2199. case B43_PHYMODE_G:
  2200. return "G";
  2201. default:
  2202. B43_WARN_ON(1);
  2203. }
  2204. return "";
  2205. }
  2206. static int find_wldev_for_phymode(struct b43_wl *wl,
  2207. unsigned int phymode,
  2208. struct b43_wldev **dev, bool * gmode)
  2209. {
  2210. struct b43_wldev *d;
  2211. list_for_each_entry(d, &wl->devlist, list) {
  2212. if (d->phy.possible_phymodes & phymode) {
  2213. /* Ok, this device supports the PHY-mode.
  2214. * Now figure out how the gmode bit has to be
  2215. * set to support it. */
  2216. if (phymode == B43_PHYMODE_A)
  2217. *gmode = 0;
  2218. else
  2219. *gmode = 1;
  2220. *dev = d;
  2221. return 0;
  2222. }
  2223. }
  2224. return -ESRCH;
  2225. }
  2226. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2227. {
  2228. struct ssb_device *sdev = dev->dev;
  2229. u32 tmslow;
  2230. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2231. tmslow &= ~B43_TMSLOW_GMODE;
  2232. tmslow |= B43_TMSLOW_PHYRESET;
  2233. tmslow |= SSB_TMSLOW_FGC;
  2234. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2235. msleep(1);
  2236. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2237. tmslow &= ~SSB_TMSLOW_FGC;
  2238. tmslow |= B43_TMSLOW_PHYRESET;
  2239. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2240. msleep(1);
  2241. }
  2242. /* Expects wl->mutex locked */
  2243. static int b43_switch_phymode(struct b43_wl *wl, unsigned int new_mode)
  2244. {
  2245. struct b43_wldev *up_dev;
  2246. struct b43_wldev *down_dev;
  2247. int err;
  2248. bool gmode = 0;
  2249. int prev_status;
  2250. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2251. if (err) {
  2252. b43err(wl, "Could not find a device for %s-PHY mode\n",
  2253. phymode_to_string(new_mode));
  2254. return err;
  2255. }
  2256. if ((up_dev == wl->current_dev) &&
  2257. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2258. /* This device is already running. */
  2259. return 0;
  2260. }
  2261. b43dbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2262. phymode_to_string(new_mode));
  2263. down_dev = wl->current_dev;
  2264. prev_status = b43_status(down_dev);
  2265. /* Shutdown the currently running core. */
  2266. if (prev_status >= B43_STAT_STARTED)
  2267. b43_wireless_core_stop(down_dev);
  2268. if (prev_status >= B43_STAT_INITIALIZED)
  2269. b43_wireless_core_exit(down_dev);
  2270. if (down_dev != up_dev) {
  2271. /* We switch to a different core, so we put PHY into
  2272. * RESET on the old core. */
  2273. b43_put_phy_into_reset(down_dev);
  2274. }
  2275. /* Now start the new core. */
  2276. up_dev->phy.gmode = gmode;
  2277. if (prev_status >= B43_STAT_INITIALIZED) {
  2278. err = b43_wireless_core_init(up_dev);
  2279. if (err) {
  2280. b43err(wl, "Fatal: Could not initialize device for "
  2281. "newly selected %s-PHY mode\n",
  2282. phymode_to_string(new_mode));
  2283. goto init_failure;
  2284. }
  2285. }
  2286. if (prev_status >= B43_STAT_STARTED) {
  2287. err = b43_wireless_core_start(up_dev);
  2288. if (err) {
  2289. b43err(wl, "Fatal: Coult not start device for "
  2290. "newly selected %s-PHY mode\n",
  2291. phymode_to_string(new_mode));
  2292. b43_wireless_core_exit(up_dev);
  2293. goto init_failure;
  2294. }
  2295. }
  2296. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2297. wl->current_dev = up_dev;
  2298. return 0;
  2299. init_failure:
  2300. /* Whoops, failed to init the new core. No core is operating now. */
  2301. wl->current_dev = NULL;
  2302. return err;
  2303. }
  2304. /* Check if the use of the antenna that ieee80211 told us to
  2305. * use is possible. This will fall back to DEFAULT.
  2306. * "antenna_nr" is the antenna identifier we got from ieee80211. */
  2307. u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
  2308. u8 antenna_nr)
  2309. {
  2310. u8 antenna_mask;
  2311. if (antenna_nr == 0) {
  2312. /* Zero means "use default antenna". That's always OK. */
  2313. return 0;
  2314. }
  2315. /* Get the mask of available antennas. */
  2316. if (dev->phy.gmode)
  2317. antenna_mask = dev->dev->bus->sprom.ant_available_bg;
  2318. else
  2319. antenna_mask = dev->dev->bus->sprom.ant_available_a;
  2320. if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
  2321. /* This antenna is not available. Fall back to default. */
  2322. return 0;
  2323. }
  2324. return antenna_nr;
  2325. }
  2326. static int b43_antenna_from_ieee80211(struct b43_wldev *dev, u8 antenna)
  2327. {
  2328. antenna = b43_ieee80211_antenna_sanitize(dev, antenna);
  2329. switch (antenna) {
  2330. case 0: /* default/diversity */
  2331. return B43_ANTENNA_DEFAULT;
  2332. case 1: /* Antenna 0 */
  2333. return B43_ANTENNA0;
  2334. case 2: /* Antenna 1 */
  2335. return B43_ANTENNA1;
  2336. default:
  2337. return B43_ANTENNA_DEFAULT;
  2338. }
  2339. }
  2340. static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  2341. {
  2342. struct b43_wl *wl = hw_to_b43_wl(hw);
  2343. struct b43_wldev *dev;
  2344. struct b43_phy *phy;
  2345. unsigned long flags;
  2346. unsigned int new_phymode = 0xFFFF;
  2347. int antenna;
  2348. int err = 0;
  2349. u32 savedirqs;
  2350. mutex_lock(&wl->mutex);
  2351. /* Switch the PHY mode (if necessary). */
  2352. switch (conf->phymode) {
  2353. case MODE_IEEE80211A:
  2354. new_phymode = B43_PHYMODE_A;
  2355. break;
  2356. case MODE_IEEE80211B:
  2357. new_phymode = B43_PHYMODE_B;
  2358. break;
  2359. case MODE_IEEE80211G:
  2360. new_phymode = B43_PHYMODE_G;
  2361. break;
  2362. default:
  2363. B43_WARN_ON(1);
  2364. }
  2365. err = b43_switch_phymode(wl, new_phymode);
  2366. if (err)
  2367. goto out_unlock_mutex;
  2368. dev = wl->current_dev;
  2369. phy = &dev->phy;
  2370. /* Disable IRQs while reconfiguring the device.
  2371. * This makes it possible to drop the spinlock throughout
  2372. * the reconfiguration process. */
  2373. spin_lock_irqsave(&wl->irq_lock, flags);
  2374. if (b43_status(dev) < B43_STAT_STARTED) {
  2375. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2376. goto out_unlock_mutex;
  2377. }
  2378. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2379. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2380. b43_synchronize_irq(dev);
  2381. /* Switch to the requested channel.
  2382. * The firmware takes care of races with the TX handler. */
  2383. if (conf->channel_val != phy->channel)
  2384. b43_radio_selectchannel(dev, conf->channel_val, 0);
  2385. /* Enable/Disable ShortSlot timing. */
  2386. if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
  2387. dev->short_slot) {
  2388. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  2389. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
  2390. b43_short_slot_timing_enable(dev);
  2391. else
  2392. b43_short_slot_timing_disable(dev);
  2393. }
  2394. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  2395. /* Adjust the desired TX power level. */
  2396. if (conf->power_level != 0) {
  2397. if (conf->power_level != phy->power_level) {
  2398. phy->power_level = conf->power_level;
  2399. b43_phy_xmitpower(dev);
  2400. }
  2401. }
  2402. /* Antennas for RX and management frame TX. */
  2403. antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_tx);
  2404. b43_mgmtframe_txantenna(dev, antenna);
  2405. antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_rx);
  2406. b43_set_rx_antenna(dev, antenna);
  2407. /* Update templates for AP mode. */
  2408. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
  2409. b43_set_beacon_int(dev, conf->beacon_int);
  2410. if (!!conf->radio_enabled != phy->radio_on) {
  2411. if (conf->radio_enabled) {
  2412. b43_radio_turn_on(dev);
  2413. b43info(dev->wl, "Radio turned on by software\n");
  2414. if (!dev->radio_hw_enable) {
  2415. b43info(dev->wl, "The hardware RF-kill button "
  2416. "still turns the radio physically off. "
  2417. "Press the button to turn it on.\n");
  2418. }
  2419. } else {
  2420. b43_radio_turn_off(dev, 0);
  2421. b43info(dev->wl, "Radio turned off by software\n");
  2422. }
  2423. }
  2424. spin_lock_irqsave(&wl->irq_lock, flags);
  2425. b43_interrupt_enable(dev, savedirqs);
  2426. mmiowb();
  2427. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2428. out_unlock_mutex:
  2429. mutex_unlock(&wl->mutex);
  2430. return err;
  2431. }
  2432. static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2433. const u8 *local_addr, const u8 *addr,
  2434. struct ieee80211_key_conf *key)
  2435. {
  2436. struct b43_wl *wl = hw_to_b43_wl(hw);
  2437. struct b43_wldev *dev;
  2438. unsigned long flags;
  2439. u8 algorithm;
  2440. u8 index;
  2441. int err;
  2442. DECLARE_MAC_BUF(mac);
  2443. if (modparam_nohwcrypt)
  2444. return -ENOSPC; /* User disabled HW-crypto */
  2445. mutex_lock(&wl->mutex);
  2446. spin_lock_irqsave(&wl->irq_lock, flags);
  2447. dev = wl->current_dev;
  2448. err = -ENODEV;
  2449. if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
  2450. goto out_unlock;
  2451. err = -EINVAL;
  2452. switch (key->alg) {
  2453. case ALG_WEP:
  2454. if (key->keylen == 5)
  2455. algorithm = B43_SEC_ALGO_WEP40;
  2456. else
  2457. algorithm = B43_SEC_ALGO_WEP104;
  2458. break;
  2459. case ALG_TKIP:
  2460. algorithm = B43_SEC_ALGO_TKIP;
  2461. break;
  2462. case ALG_CCMP:
  2463. algorithm = B43_SEC_ALGO_AES;
  2464. break;
  2465. default:
  2466. B43_WARN_ON(1);
  2467. goto out_unlock;
  2468. }
  2469. index = (u8) (key->keyidx);
  2470. if (index > 3)
  2471. goto out_unlock;
  2472. switch (cmd) {
  2473. case SET_KEY:
  2474. if (algorithm == B43_SEC_ALGO_TKIP) {
  2475. /* FIXME: No TKIP hardware encryption for now. */
  2476. err = -EOPNOTSUPP;
  2477. goto out_unlock;
  2478. }
  2479. if (is_broadcast_ether_addr(addr)) {
  2480. /* addr is FF:FF:FF:FF:FF:FF for default keys */
  2481. err = b43_key_write(dev, index, algorithm,
  2482. key->key, key->keylen, NULL, key);
  2483. } else {
  2484. /*
  2485. * either pairwise key or address is 00:00:00:00:00:00
  2486. * for transmit-only keys
  2487. */
  2488. err = b43_key_write(dev, -1, algorithm,
  2489. key->key, key->keylen, addr, key);
  2490. }
  2491. if (err)
  2492. goto out_unlock;
  2493. if (algorithm == B43_SEC_ALGO_WEP40 ||
  2494. algorithm == B43_SEC_ALGO_WEP104) {
  2495. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  2496. } else {
  2497. b43_hf_write(dev,
  2498. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  2499. }
  2500. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2501. break;
  2502. case DISABLE_KEY: {
  2503. err = b43_key_clear(dev, key->hw_key_idx);
  2504. if (err)
  2505. goto out_unlock;
  2506. break;
  2507. }
  2508. default:
  2509. B43_WARN_ON(1);
  2510. }
  2511. out_unlock:
  2512. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2513. mutex_unlock(&wl->mutex);
  2514. if (!err) {
  2515. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  2516. "mac: %s\n",
  2517. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  2518. print_mac(mac, addr));
  2519. }
  2520. return err;
  2521. }
  2522. static void b43_op_configure_filter(struct ieee80211_hw *hw,
  2523. unsigned int changed, unsigned int *fflags,
  2524. int mc_count, struct dev_addr_list *mc_list)
  2525. {
  2526. struct b43_wl *wl = hw_to_b43_wl(hw);
  2527. struct b43_wldev *dev = wl->current_dev;
  2528. unsigned long flags;
  2529. if (!dev) {
  2530. *fflags = 0;
  2531. return;
  2532. }
  2533. spin_lock_irqsave(&wl->irq_lock, flags);
  2534. *fflags &= FIF_PROMISC_IN_BSS |
  2535. FIF_ALLMULTI |
  2536. FIF_FCSFAIL |
  2537. FIF_PLCPFAIL |
  2538. FIF_CONTROL |
  2539. FIF_OTHER_BSS |
  2540. FIF_BCN_PRBRESP_PROMISC;
  2541. changed &= FIF_PROMISC_IN_BSS |
  2542. FIF_ALLMULTI |
  2543. FIF_FCSFAIL |
  2544. FIF_PLCPFAIL |
  2545. FIF_CONTROL |
  2546. FIF_OTHER_BSS |
  2547. FIF_BCN_PRBRESP_PROMISC;
  2548. wl->filter_flags = *fflags;
  2549. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  2550. b43_adjust_opmode(dev);
  2551. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2552. }
  2553. static int b43_op_config_interface(struct ieee80211_hw *hw,
  2554. int if_id,
  2555. struct ieee80211_if_conf *conf)
  2556. {
  2557. struct b43_wl *wl = hw_to_b43_wl(hw);
  2558. struct b43_wldev *dev = wl->current_dev;
  2559. unsigned long flags;
  2560. if (!dev)
  2561. return -ENODEV;
  2562. mutex_lock(&wl->mutex);
  2563. spin_lock_irqsave(&wl->irq_lock, flags);
  2564. B43_WARN_ON(wl->if_id != if_id);
  2565. if (conf->bssid)
  2566. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2567. else
  2568. memset(wl->bssid, 0, ETH_ALEN);
  2569. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  2570. if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
  2571. B43_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
  2572. b43_set_ssid(dev, conf->ssid, conf->ssid_len);
  2573. if (conf->beacon)
  2574. b43_refresh_templates(dev, conf->beacon);
  2575. }
  2576. b43_write_mac_bssid_templates(dev);
  2577. }
  2578. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2579. mutex_unlock(&wl->mutex);
  2580. return 0;
  2581. }
  2582. /* Locking: wl->mutex */
  2583. static void b43_wireless_core_stop(struct b43_wldev *dev)
  2584. {
  2585. struct b43_wl *wl = dev->wl;
  2586. unsigned long flags;
  2587. if (b43_status(dev) < B43_STAT_STARTED)
  2588. return;
  2589. /* Disable and sync interrupts. We must do this before than
  2590. * setting the status to INITIALIZED, as the interrupt handler
  2591. * won't care about IRQs then. */
  2592. spin_lock_irqsave(&wl->irq_lock, flags);
  2593. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2594. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  2595. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2596. b43_synchronize_irq(dev);
  2597. b43_set_status(dev, B43_STAT_INITIALIZED);
  2598. mutex_unlock(&wl->mutex);
  2599. /* Must unlock as it would otherwise deadlock. No races here.
  2600. * Cancel the possibly running self-rearming periodic work. */
  2601. cancel_delayed_work_sync(&dev->periodic_work);
  2602. mutex_lock(&wl->mutex);
  2603. ieee80211_stop_queues(wl->hw); //FIXME this could cause a deadlock, as mac80211 seems buggy.
  2604. b43_mac_suspend(dev);
  2605. free_irq(dev->dev->irq, dev);
  2606. b43dbg(wl, "Wireless interface stopped\n");
  2607. }
  2608. /* Locking: wl->mutex */
  2609. static int b43_wireless_core_start(struct b43_wldev *dev)
  2610. {
  2611. int err;
  2612. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  2613. drain_txstatus_queue(dev);
  2614. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  2615. IRQF_SHARED, KBUILD_MODNAME, dev);
  2616. if (err) {
  2617. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  2618. goto out;
  2619. }
  2620. /* We are ready to run. */
  2621. b43_set_status(dev, B43_STAT_STARTED);
  2622. /* Start data flow (TX/RX). */
  2623. b43_mac_enable(dev);
  2624. b43_interrupt_enable(dev, dev->irq_savedstate);
  2625. ieee80211_start_queues(dev->wl->hw);
  2626. /* Start maintainance work */
  2627. b43_periodic_tasks_setup(dev);
  2628. b43dbg(dev->wl, "Wireless interface started\n");
  2629. out:
  2630. return err;
  2631. }
  2632. /* Get PHY and RADIO versioning numbers */
  2633. static int b43_phy_versioning(struct b43_wldev *dev)
  2634. {
  2635. struct b43_phy *phy = &dev->phy;
  2636. u32 tmp;
  2637. u8 analog_type;
  2638. u8 phy_type;
  2639. u8 phy_rev;
  2640. u16 radio_manuf;
  2641. u16 radio_ver;
  2642. u16 radio_rev;
  2643. int unsupported = 0;
  2644. /* Get PHY versioning */
  2645. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  2646. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  2647. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  2648. phy_rev = (tmp & B43_PHYVER_VERSION);
  2649. switch (phy_type) {
  2650. case B43_PHYTYPE_A:
  2651. if (phy_rev >= 4)
  2652. unsupported = 1;
  2653. break;
  2654. case B43_PHYTYPE_B:
  2655. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  2656. && phy_rev != 7)
  2657. unsupported = 1;
  2658. break;
  2659. case B43_PHYTYPE_G:
  2660. if (phy_rev > 9)
  2661. unsupported = 1;
  2662. break;
  2663. #ifdef CONFIG_B43_NPHY
  2664. case B43_PHYTYPE_N:
  2665. if (phy_rev > 1)
  2666. unsupported = 1;
  2667. break;
  2668. #endif
  2669. default:
  2670. unsupported = 1;
  2671. };
  2672. if (unsupported) {
  2673. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  2674. "(Analog %u, Type %u, Revision %u)\n",
  2675. analog_type, phy_type, phy_rev);
  2676. return -EOPNOTSUPP;
  2677. }
  2678. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2679. analog_type, phy_type, phy_rev);
  2680. /* Get RADIO versioning */
  2681. if (dev->dev->bus->chip_id == 0x4317) {
  2682. if (dev->dev->bus->chip_rev == 0)
  2683. tmp = 0x3205017F;
  2684. else if (dev->dev->bus->chip_rev == 1)
  2685. tmp = 0x4205017F;
  2686. else
  2687. tmp = 0x5205017F;
  2688. } else {
  2689. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2690. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH);
  2691. tmp <<= 16;
  2692. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  2693. tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  2694. }
  2695. radio_manuf = (tmp & 0x00000FFF);
  2696. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2697. radio_rev = (tmp & 0xF0000000) >> 28;
  2698. if (radio_manuf != 0x17F /* Broadcom */)
  2699. unsupported = 1;
  2700. switch (phy_type) {
  2701. case B43_PHYTYPE_A:
  2702. if (radio_ver != 0x2060)
  2703. unsupported = 1;
  2704. if (radio_rev != 1)
  2705. unsupported = 1;
  2706. if (radio_manuf != 0x17F)
  2707. unsupported = 1;
  2708. break;
  2709. case B43_PHYTYPE_B:
  2710. if ((radio_ver & 0xFFF0) != 0x2050)
  2711. unsupported = 1;
  2712. break;
  2713. case B43_PHYTYPE_G:
  2714. if (radio_ver != 0x2050)
  2715. unsupported = 1;
  2716. break;
  2717. case B43_PHYTYPE_N:
  2718. if (radio_ver != 5)
  2719. unsupported = 1;
  2720. break;
  2721. default:
  2722. B43_WARN_ON(1);
  2723. }
  2724. if (unsupported) {
  2725. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  2726. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2727. radio_manuf, radio_ver, radio_rev);
  2728. return -EOPNOTSUPP;
  2729. }
  2730. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  2731. radio_manuf, radio_ver, radio_rev);
  2732. phy->radio_manuf = radio_manuf;
  2733. phy->radio_ver = radio_ver;
  2734. phy->radio_rev = radio_rev;
  2735. phy->analog = analog_type;
  2736. phy->type = phy_type;
  2737. phy->rev = phy_rev;
  2738. return 0;
  2739. }
  2740. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  2741. struct b43_phy *phy)
  2742. {
  2743. struct b43_txpower_lo_control *lo;
  2744. int i;
  2745. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2746. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2747. phy->aci_enable = 0;
  2748. phy->aci_wlan_automatic = 0;
  2749. phy->aci_hw_rssi = 0;
  2750. phy->radio_off_context.valid = 0;
  2751. lo = phy->lo_control;
  2752. if (lo) {
  2753. memset(lo, 0, sizeof(*(phy->lo_control)));
  2754. lo->rebuild = 1;
  2755. lo->tx_bias = 0xFF;
  2756. }
  2757. phy->max_lb_gain = 0;
  2758. phy->trsw_rx_gain = 0;
  2759. phy->txpwr_offset = 0;
  2760. /* NRSSI */
  2761. phy->nrssislope = 0;
  2762. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2763. phy->nrssi[i] = -1000;
  2764. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2765. phy->nrssi_lt[i] = i;
  2766. phy->lofcal = 0xFFFF;
  2767. phy->initval = 0xFFFF;
  2768. phy->interfmode = B43_INTERFMODE_NONE;
  2769. phy->channel = 0xFF;
  2770. phy->hardware_power_control = !!modparam_hwpctl;
  2771. /* PHY TX errors counter. */
  2772. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2773. /* OFDM-table address caching. */
  2774. phy->ofdmtab_addr_direction = B43_OFDMTAB_DIRECTION_UNKNOWN;
  2775. }
  2776. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  2777. {
  2778. dev->dfq_valid = 0;
  2779. /* Assume the radio is enabled. If it's not enabled, the state will
  2780. * immediately get fixed on the first periodic work run. */
  2781. dev->radio_hw_enable = 1;
  2782. /* Stats */
  2783. memset(&dev->stats, 0, sizeof(dev->stats));
  2784. setup_struct_phy_for_init(dev, &dev->phy);
  2785. /* IRQ related flags */
  2786. dev->irq_reason = 0;
  2787. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2788. dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
  2789. dev->mac_suspended = 1;
  2790. /* Noise calculation context */
  2791. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2792. }
  2793. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  2794. {
  2795. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2796. u32 hf;
  2797. if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
  2798. return;
  2799. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  2800. return;
  2801. hf = b43_hf_read(dev);
  2802. if (sprom->boardflags_lo & B43_BFL_BTCMOD)
  2803. hf |= B43_HF_BTCOEXALT;
  2804. else
  2805. hf |= B43_HF_BTCOEX;
  2806. b43_hf_write(dev, hf);
  2807. //TODO
  2808. }
  2809. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  2810. { //TODO
  2811. }
  2812. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  2813. {
  2814. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2815. struct ssb_bus *bus = dev->dev->bus;
  2816. u32 tmp;
  2817. if (bus->pcicore.dev &&
  2818. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2819. bus->pcicore.dev->id.revision <= 5) {
  2820. /* IMCFGLO timeouts workaround. */
  2821. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2822. tmp &= ~SSB_IMCFGLO_REQTO;
  2823. tmp &= ~SSB_IMCFGLO_SERTO;
  2824. switch (bus->bustype) {
  2825. case SSB_BUSTYPE_PCI:
  2826. case SSB_BUSTYPE_PCMCIA:
  2827. tmp |= 0x32;
  2828. break;
  2829. case SSB_BUSTYPE_SSB:
  2830. tmp |= 0x53;
  2831. break;
  2832. }
  2833. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2834. }
  2835. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2836. }
  2837. /* Write the short and long frame retry limit values. */
  2838. static void b43_set_retry_limits(struct b43_wldev *dev,
  2839. unsigned int short_retry,
  2840. unsigned int long_retry)
  2841. {
  2842. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2843. * the chip-internal counter. */
  2844. short_retry = min(short_retry, (unsigned int)0xF);
  2845. long_retry = min(long_retry, (unsigned int)0xF);
  2846. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
  2847. short_retry);
  2848. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
  2849. long_retry);
  2850. }
  2851. /* Shutdown a wireless core */
  2852. /* Locking: wl->mutex */
  2853. static void b43_wireless_core_exit(struct b43_wldev *dev)
  2854. {
  2855. struct b43_phy *phy = &dev->phy;
  2856. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  2857. if (b43_status(dev) != B43_STAT_INITIALIZED)
  2858. return;
  2859. b43_set_status(dev, B43_STAT_UNINIT);
  2860. b43_leds_exit(dev);
  2861. b43_rng_exit(dev->wl);
  2862. b43_dma_free(dev);
  2863. b43_chip_exit(dev);
  2864. b43_radio_turn_off(dev, 1);
  2865. b43_switch_analog(dev, 0);
  2866. if (phy->dyn_tssi_tbl)
  2867. kfree(phy->tssi2dbm);
  2868. kfree(phy->lo_control);
  2869. phy->lo_control = NULL;
  2870. ssb_device_disable(dev->dev, 0);
  2871. ssb_bus_may_powerdown(dev->dev->bus);
  2872. }
  2873. /* Initialize a wireless core */
  2874. static int b43_wireless_core_init(struct b43_wldev *dev)
  2875. {
  2876. struct b43_wl *wl = dev->wl;
  2877. struct ssb_bus *bus = dev->dev->bus;
  2878. struct ssb_sprom *sprom = &bus->sprom;
  2879. struct b43_phy *phy = &dev->phy;
  2880. int err;
  2881. u32 hf, tmp;
  2882. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  2883. err = ssb_bus_powerup(bus, 0);
  2884. if (err)
  2885. goto out;
  2886. if (!ssb_device_is_enabled(dev->dev)) {
  2887. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  2888. b43_wireless_core_reset(dev, tmp);
  2889. }
  2890. if ((phy->type == B43_PHYTYPE_B) || (phy->type == B43_PHYTYPE_G)) {
  2891. phy->lo_control =
  2892. kzalloc(sizeof(*(phy->lo_control)), GFP_KERNEL);
  2893. if (!phy->lo_control) {
  2894. err = -ENOMEM;
  2895. goto err_busdown;
  2896. }
  2897. }
  2898. setup_struct_wldev_for_init(dev);
  2899. err = b43_phy_init_tssi2dbm_table(dev);
  2900. if (err)
  2901. goto err_kfree_lo_control;
  2902. /* Enable IRQ routing to this device. */
  2903. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  2904. b43_imcfglo_timeouts_workaround(dev);
  2905. b43_bluetooth_coext_disable(dev);
  2906. b43_phy_early_init(dev);
  2907. err = b43_chip_init(dev);
  2908. if (err)
  2909. goto err_kfree_tssitbl;
  2910. b43_shm_write16(dev, B43_SHM_SHARED,
  2911. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  2912. hf = b43_hf_read(dev);
  2913. if (phy->type == B43_PHYTYPE_G) {
  2914. hf |= B43_HF_SYMW;
  2915. if (phy->rev == 1)
  2916. hf |= B43_HF_GDCW;
  2917. if (sprom->boardflags_lo & B43_BFL_PACTRL)
  2918. hf |= B43_HF_OFDMPABOOST;
  2919. } else if (phy->type == B43_PHYTYPE_B) {
  2920. hf |= B43_HF_SYMW;
  2921. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  2922. hf &= ~B43_HF_GDCW;
  2923. }
  2924. b43_hf_write(dev, hf);
  2925. b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
  2926. B43_DEFAULT_LONG_RETRY_LIMIT);
  2927. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  2928. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  2929. /* Disable sending probe responses from firmware.
  2930. * Setting the MaxTime to one usec will always trigger
  2931. * a timeout, so we never send any probe resp.
  2932. * A timeout of zero is infinite. */
  2933. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  2934. b43_rate_memory_init(dev);
  2935. /* Minimum Contention Window */
  2936. if (phy->type == B43_PHYTYPE_B) {
  2937. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  2938. } else {
  2939. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  2940. }
  2941. /* Maximum Contention Window */
  2942. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  2943. err = b43_dma_init(dev);
  2944. if (err)
  2945. goto err_chip_exit;
  2946. b43_qos_init(dev);
  2947. //FIXME
  2948. #if 1
  2949. b43_write16(dev, 0x0612, 0x0050);
  2950. b43_shm_write16(dev, B43_SHM_SHARED, 0x0416, 0x0050);
  2951. b43_shm_write16(dev, B43_SHM_SHARED, 0x0414, 0x01F4);
  2952. #endif
  2953. b43_bluetooth_coext_enable(dev);
  2954. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  2955. memset(wl->bssid, 0, ETH_ALEN);
  2956. memset(wl->mac_addr, 0, ETH_ALEN);
  2957. b43_upload_card_macaddress(dev);
  2958. b43_security_init(dev);
  2959. b43_rng_init(wl);
  2960. b43_set_status(dev, B43_STAT_INITIALIZED);
  2961. b43_leds_init(dev);
  2962. out:
  2963. return err;
  2964. err_chip_exit:
  2965. b43_chip_exit(dev);
  2966. err_kfree_tssitbl:
  2967. if (phy->dyn_tssi_tbl)
  2968. kfree(phy->tssi2dbm);
  2969. err_kfree_lo_control:
  2970. kfree(phy->lo_control);
  2971. phy->lo_control = NULL;
  2972. err_busdown:
  2973. ssb_bus_may_powerdown(bus);
  2974. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  2975. return err;
  2976. }
  2977. static int b43_op_add_interface(struct ieee80211_hw *hw,
  2978. struct ieee80211_if_init_conf *conf)
  2979. {
  2980. struct b43_wl *wl = hw_to_b43_wl(hw);
  2981. struct b43_wldev *dev;
  2982. unsigned long flags;
  2983. int err = -EOPNOTSUPP;
  2984. /* TODO: allow WDS/AP devices to coexist */
  2985. if (conf->type != IEEE80211_IF_TYPE_AP &&
  2986. conf->type != IEEE80211_IF_TYPE_STA &&
  2987. conf->type != IEEE80211_IF_TYPE_WDS &&
  2988. conf->type != IEEE80211_IF_TYPE_IBSS)
  2989. return -EOPNOTSUPP;
  2990. mutex_lock(&wl->mutex);
  2991. if (wl->operating)
  2992. goto out_mutex_unlock;
  2993. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  2994. dev = wl->current_dev;
  2995. wl->operating = 1;
  2996. wl->if_id = conf->if_id;
  2997. wl->if_type = conf->type;
  2998. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  2999. spin_lock_irqsave(&wl->irq_lock, flags);
  3000. b43_adjust_opmode(dev);
  3001. b43_upload_card_macaddress(dev);
  3002. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3003. err = 0;
  3004. out_mutex_unlock:
  3005. mutex_unlock(&wl->mutex);
  3006. return err;
  3007. }
  3008. static void b43_op_remove_interface(struct ieee80211_hw *hw,
  3009. struct ieee80211_if_init_conf *conf)
  3010. {
  3011. struct b43_wl *wl = hw_to_b43_wl(hw);
  3012. struct b43_wldev *dev = wl->current_dev;
  3013. unsigned long flags;
  3014. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3015. mutex_lock(&wl->mutex);
  3016. B43_WARN_ON(!wl->operating);
  3017. B43_WARN_ON(wl->if_id != conf->if_id);
  3018. wl->operating = 0;
  3019. spin_lock_irqsave(&wl->irq_lock, flags);
  3020. b43_adjust_opmode(dev);
  3021. memset(wl->mac_addr, 0, ETH_ALEN);
  3022. b43_upload_card_macaddress(dev);
  3023. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3024. mutex_unlock(&wl->mutex);
  3025. }
  3026. static int b43_op_start(struct ieee80211_hw *hw)
  3027. {
  3028. struct b43_wl *wl = hw_to_b43_wl(hw);
  3029. struct b43_wldev *dev = wl->current_dev;
  3030. int did_init = 0;
  3031. int err = 0;
  3032. /* First register RFkill.
  3033. * LEDs that are registered later depend on it. */
  3034. b43_rfkill_init(dev);
  3035. mutex_lock(&wl->mutex);
  3036. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3037. err = b43_wireless_core_init(dev);
  3038. if (err)
  3039. goto out_mutex_unlock;
  3040. did_init = 1;
  3041. }
  3042. if (b43_status(dev) < B43_STAT_STARTED) {
  3043. err = b43_wireless_core_start(dev);
  3044. if (err) {
  3045. if (did_init)
  3046. b43_wireless_core_exit(dev);
  3047. goto out_mutex_unlock;
  3048. }
  3049. }
  3050. out_mutex_unlock:
  3051. mutex_unlock(&wl->mutex);
  3052. return err;
  3053. }
  3054. static void b43_op_stop(struct ieee80211_hw *hw)
  3055. {
  3056. struct b43_wl *wl = hw_to_b43_wl(hw);
  3057. struct b43_wldev *dev = wl->current_dev;
  3058. b43_rfkill_exit(dev);
  3059. mutex_lock(&wl->mutex);
  3060. if (b43_status(dev) >= B43_STAT_STARTED)
  3061. b43_wireless_core_stop(dev);
  3062. b43_wireless_core_exit(dev);
  3063. mutex_unlock(&wl->mutex);
  3064. }
  3065. static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
  3066. u32 short_retry_limit, u32 long_retry_limit)
  3067. {
  3068. struct b43_wl *wl = hw_to_b43_wl(hw);
  3069. struct b43_wldev *dev;
  3070. int err = 0;
  3071. mutex_lock(&wl->mutex);
  3072. dev = wl->current_dev;
  3073. if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED))) {
  3074. err = -ENODEV;
  3075. goto out_unlock;
  3076. }
  3077. b43_set_retry_limits(dev, short_retry_limit, long_retry_limit);
  3078. out_unlock:
  3079. mutex_unlock(&wl->mutex);
  3080. return err;
  3081. }
  3082. static const struct ieee80211_ops b43_hw_ops = {
  3083. .tx = b43_op_tx,
  3084. .conf_tx = b43_op_conf_tx,
  3085. .add_interface = b43_op_add_interface,
  3086. .remove_interface = b43_op_remove_interface,
  3087. .config = b43_op_config,
  3088. .config_interface = b43_op_config_interface,
  3089. .configure_filter = b43_op_configure_filter,
  3090. .set_key = b43_op_set_key,
  3091. .get_stats = b43_op_get_stats,
  3092. .get_tx_stats = b43_op_get_tx_stats,
  3093. .start = b43_op_start,
  3094. .stop = b43_op_stop,
  3095. .set_retry_limit = b43_op_set_retry_limit,
  3096. };
  3097. /* Hard-reset the chip. Do not call this directly.
  3098. * Use b43_controller_restart()
  3099. */
  3100. static void b43_chip_reset(struct work_struct *work)
  3101. {
  3102. struct b43_wldev *dev =
  3103. container_of(work, struct b43_wldev, restart_work);
  3104. struct b43_wl *wl = dev->wl;
  3105. int err = 0;
  3106. int prev_status;
  3107. mutex_lock(&wl->mutex);
  3108. prev_status = b43_status(dev);
  3109. /* Bring the device down... */
  3110. if (prev_status >= B43_STAT_STARTED)
  3111. b43_wireless_core_stop(dev);
  3112. if (prev_status >= B43_STAT_INITIALIZED)
  3113. b43_wireless_core_exit(dev);
  3114. /* ...and up again. */
  3115. if (prev_status >= B43_STAT_INITIALIZED) {
  3116. err = b43_wireless_core_init(dev);
  3117. if (err)
  3118. goto out;
  3119. }
  3120. if (prev_status >= B43_STAT_STARTED) {
  3121. err = b43_wireless_core_start(dev);
  3122. if (err) {
  3123. b43_wireless_core_exit(dev);
  3124. goto out;
  3125. }
  3126. }
  3127. out:
  3128. mutex_unlock(&wl->mutex);
  3129. if (err)
  3130. b43err(wl, "Controller restart FAILED\n");
  3131. else
  3132. b43info(wl, "Controller restarted\n");
  3133. }
  3134. static int b43_setup_modes(struct b43_wldev *dev,
  3135. bool have_2ghz_phy, bool have_5ghz_phy)
  3136. {
  3137. struct ieee80211_hw *hw = dev->wl->hw;
  3138. struct ieee80211_hw_mode *mode;
  3139. struct b43_phy *phy = &dev->phy;
  3140. int err;
  3141. /* XXX: This function will go away soon, when mac80211
  3142. * band stuff is rewritten. So this is just a hack.
  3143. * For now we always claim GPHY mode, as there is no
  3144. * support for NPHY and APHY in the device, yet.
  3145. * This assumption is OK, as any B, N or A PHY will already
  3146. * have died a horrible sanity check death earlier. */
  3147. mode = &phy->hwmodes[0];
  3148. mode->mode = MODE_IEEE80211G;
  3149. mode->num_channels = b43_2ghz_chantable_size;
  3150. mode->channels = b43_2ghz_chantable;
  3151. mode->num_rates = b43_g_ratetable_size;
  3152. mode->rates = b43_g_ratetable;
  3153. err = ieee80211_register_hwmode(hw, mode);
  3154. if (err)
  3155. return err;
  3156. phy->possible_phymodes |= B43_PHYMODE_G;
  3157. return 0;
  3158. }
  3159. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3160. {
  3161. /* We release firmware that late to not be required to re-request
  3162. * is all the time when we reinit the core. */
  3163. b43_release_firmware(dev);
  3164. }
  3165. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3166. {
  3167. struct b43_wl *wl = dev->wl;
  3168. struct ssb_bus *bus = dev->dev->bus;
  3169. struct pci_dev *pdev = bus->host_pci;
  3170. int err;
  3171. bool have_2ghz_phy = 0, have_5ghz_phy = 0;
  3172. u32 tmp;
  3173. /* Do NOT do any device initialization here.
  3174. * Do it in wireless_core_init() instead.
  3175. * This function is for gathering basic information about the HW, only.
  3176. * Also some structs may be set up here. But most likely you want to have
  3177. * that in core_init(), too.
  3178. */
  3179. err = ssb_bus_powerup(bus, 0);
  3180. if (err) {
  3181. b43err(wl, "Bus powerup failed\n");
  3182. goto out;
  3183. }
  3184. /* Get the PHY type. */
  3185. if (dev->dev->id.revision >= 5) {
  3186. u32 tmshigh;
  3187. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3188. have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
  3189. have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
  3190. } else
  3191. B43_WARN_ON(1);
  3192. dev->phy.gmode = have_2ghz_phy;
  3193. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3194. b43_wireless_core_reset(dev, tmp);
  3195. err = b43_phy_versioning(dev);
  3196. if (err)
  3197. goto err_powerdown;
  3198. /* Check if this device supports multiband. */
  3199. if (!pdev ||
  3200. (pdev->device != 0x4312 &&
  3201. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3202. /* No multiband support. */
  3203. have_2ghz_phy = 0;
  3204. have_5ghz_phy = 0;
  3205. switch (dev->phy.type) {
  3206. case B43_PHYTYPE_A:
  3207. have_5ghz_phy = 1;
  3208. break;
  3209. case B43_PHYTYPE_G:
  3210. case B43_PHYTYPE_N:
  3211. have_2ghz_phy = 1;
  3212. break;
  3213. default:
  3214. B43_WARN_ON(1);
  3215. }
  3216. }
  3217. if (dev->phy.type == B43_PHYTYPE_A) {
  3218. /* FIXME */
  3219. b43err(wl, "IEEE 802.11a devices are unsupported\n");
  3220. err = -EOPNOTSUPP;
  3221. goto err_powerdown;
  3222. }
  3223. dev->phy.gmode = have_2ghz_phy;
  3224. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3225. b43_wireless_core_reset(dev, tmp);
  3226. err = b43_validate_chipaccess(dev);
  3227. if (err)
  3228. goto err_powerdown;
  3229. err = b43_setup_modes(dev, have_2ghz_phy, have_5ghz_phy);
  3230. if (err)
  3231. goto err_powerdown;
  3232. /* Now set some default "current_dev" */
  3233. if (!wl->current_dev)
  3234. wl->current_dev = dev;
  3235. INIT_WORK(&dev->restart_work, b43_chip_reset);
  3236. b43_radio_turn_off(dev, 1);
  3237. b43_switch_analog(dev, 0);
  3238. ssb_device_disable(dev->dev, 0);
  3239. ssb_bus_may_powerdown(bus);
  3240. out:
  3241. return err;
  3242. err_powerdown:
  3243. ssb_bus_may_powerdown(bus);
  3244. return err;
  3245. }
  3246. static void b43_one_core_detach(struct ssb_device *dev)
  3247. {
  3248. struct b43_wldev *wldev;
  3249. struct b43_wl *wl;
  3250. wldev = ssb_get_drvdata(dev);
  3251. wl = wldev->wl;
  3252. cancel_work_sync(&wldev->restart_work);
  3253. b43_debugfs_remove_device(wldev);
  3254. b43_wireless_core_detach(wldev);
  3255. list_del(&wldev->list);
  3256. wl->nr_devs--;
  3257. ssb_set_drvdata(dev, NULL);
  3258. kfree(wldev);
  3259. }
  3260. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  3261. {
  3262. struct b43_wldev *wldev;
  3263. struct pci_dev *pdev;
  3264. int err = -ENOMEM;
  3265. if (!list_empty(&wl->devlist)) {
  3266. /* We are not the first core on this chip. */
  3267. pdev = dev->bus->host_pci;
  3268. /* Only special chips support more than one wireless
  3269. * core, although some of the other chips have more than
  3270. * one wireless core as well. Check for this and
  3271. * bail out early.
  3272. */
  3273. if (!pdev ||
  3274. ((pdev->device != 0x4321) &&
  3275. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  3276. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  3277. return -ENODEV;
  3278. }
  3279. }
  3280. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3281. if (!wldev)
  3282. goto out;
  3283. wldev->dev = dev;
  3284. wldev->wl = wl;
  3285. b43_set_status(wldev, B43_STAT_UNINIT);
  3286. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3287. tasklet_init(&wldev->isr_tasklet,
  3288. (void (*)(unsigned long))b43_interrupt_tasklet,
  3289. (unsigned long)wldev);
  3290. INIT_LIST_HEAD(&wldev->list);
  3291. err = b43_wireless_core_attach(wldev);
  3292. if (err)
  3293. goto err_kfree_wldev;
  3294. list_add(&wldev->list, &wl->devlist);
  3295. wl->nr_devs++;
  3296. ssb_set_drvdata(dev, wldev);
  3297. b43_debugfs_add_device(wldev);
  3298. out:
  3299. return err;
  3300. err_kfree_wldev:
  3301. kfree(wldev);
  3302. return err;
  3303. }
  3304. static void b43_sprom_fixup(struct ssb_bus *bus)
  3305. {
  3306. /* boardflags workarounds */
  3307. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  3308. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  3309. bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
  3310. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3311. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  3312. bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
  3313. }
  3314. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  3315. {
  3316. struct ieee80211_hw *hw = wl->hw;
  3317. ssb_set_devtypedata(dev, NULL);
  3318. ieee80211_free_hw(hw);
  3319. }
  3320. static int b43_wireless_init(struct ssb_device *dev)
  3321. {
  3322. struct ssb_sprom *sprom = &dev->bus->sprom;
  3323. struct ieee80211_hw *hw;
  3324. struct b43_wl *wl;
  3325. int err = -ENOMEM;
  3326. b43_sprom_fixup(dev->bus);
  3327. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  3328. if (!hw) {
  3329. b43err(NULL, "Could not allocate ieee80211 device\n");
  3330. goto out;
  3331. }
  3332. /* fill hw info */
  3333. hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  3334. IEEE80211_HW_RX_INCLUDES_FCS;
  3335. hw->max_signal = 100;
  3336. hw->max_rssi = -110;
  3337. hw->max_noise = -110;
  3338. hw->queues = 1; /* FIXME: hardware has more queues */
  3339. SET_IEEE80211_DEV(hw, dev->dev);
  3340. if (is_valid_ether_addr(sprom->et1mac))
  3341. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  3342. else
  3343. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  3344. /* Get and initialize struct b43_wl */
  3345. wl = hw_to_b43_wl(hw);
  3346. memset(wl, 0, sizeof(*wl));
  3347. wl->hw = hw;
  3348. spin_lock_init(&wl->irq_lock);
  3349. spin_lock_init(&wl->leds_lock);
  3350. mutex_init(&wl->mutex);
  3351. INIT_LIST_HEAD(&wl->devlist);
  3352. ssb_set_devtypedata(dev, wl);
  3353. b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3354. err = 0;
  3355. out:
  3356. return err;
  3357. }
  3358. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  3359. {
  3360. struct b43_wl *wl;
  3361. int err;
  3362. int first = 0;
  3363. wl = ssb_get_devtypedata(dev);
  3364. if (!wl) {
  3365. /* Probing the first core. Must setup common struct b43_wl */
  3366. first = 1;
  3367. err = b43_wireless_init(dev);
  3368. if (err)
  3369. goto out;
  3370. wl = ssb_get_devtypedata(dev);
  3371. B43_WARN_ON(!wl);
  3372. }
  3373. err = b43_one_core_attach(dev, wl);
  3374. if (err)
  3375. goto err_wireless_exit;
  3376. if (first) {
  3377. err = ieee80211_register_hw(wl->hw);
  3378. if (err)
  3379. goto err_one_core_detach;
  3380. }
  3381. out:
  3382. return err;
  3383. err_one_core_detach:
  3384. b43_one_core_detach(dev);
  3385. err_wireless_exit:
  3386. if (first)
  3387. b43_wireless_exit(dev, wl);
  3388. return err;
  3389. }
  3390. static void b43_remove(struct ssb_device *dev)
  3391. {
  3392. struct b43_wl *wl = ssb_get_devtypedata(dev);
  3393. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3394. B43_WARN_ON(!wl);
  3395. if (wl->current_dev == wldev)
  3396. ieee80211_unregister_hw(wl->hw);
  3397. b43_one_core_detach(dev);
  3398. if (list_empty(&wl->devlist)) {
  3399. /* Last core on the chip unregistered.
  3400. * We can destroy common struct b43_wl.
  3401. */
  3402. b43_wireless_exit(dev, wl);
  3403. }
  3404. }
  3405. /* Perform a hardware reset. This can be called from any context. */
  3406. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  3407. {
  3408. /* Must avoid requeueing, if we are in shutdown. */
  3409. if (b43_status(dev) < B43_STAT_INITIALIZED)
  3410. return;
  3411. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  3412. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  3413. }
  3414. #ifdef CONFIG_PM
  3415. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  3416. {
  3417. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3418. struct b43_wl *wl = wldev->wl;
  3419. b43dbg(wl, "Suspending...\n");
  3420. mutex_lock(&wl->mutex);
  3421. wldev->suspend_init_status = b43_status(wldev);
  3422. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  3423. b43_wireless_core_stop(wldev);
  3424. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  3425. b43_wireless_core_exit(wldev);
  3426. mutex_unlock(&wl->mutex);
  3427. b43dbg(wl, "Device suspended.\n");
  3428. return 0;
  3429. }
  3430. static int b43_resume(struct ssb_device *dev)
  3431. {
  3432. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  3433. struct b43_wl *wl = wldev->wl;
  3434. int err = 0;
  3435. b43dbg(wl, "Resuming...\n");
  3436. mutex_lock(&wl->mutex);
  3437. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  3438. err = b43_wireless_core_init(wldev);
  3439. if (err) {
  3440. b43err(wl, "Resume failed at core init\n");
  3441. goto out;
  3442. }
  3443. }
  3444. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  3445. err = b43_wireless_core_start(wldev);
  3446. if (err) {
  3447. b43_wireless_core_exit(wldev);
  3448. b43err(wl, "Resume failed at core start\n");
  3449. goto out;
  3450. }
  3451. }
  3452. mutex_unlock(&wl->mutex);
  3453. b43dbg(wl, "Device resumed.\n");
  3454. out:
  3455. return err;
  3456. }
  3457. #else /* CONFIG_PM */
  3458. # define b43_suspend NULL
  3459. # define b43_resume NULL
  3460. #endif /* CONFIG_PM */
  3461. static struct ssb_driver b43_ssb_driver = {
  3462. .name = KBUILD_MODNAME,
  3463. .id_table = b43_ssb_tbl,
  3464. .probe = b43_probe,
  3465. .remove = b43_remove,
  3466. .suspend = b43_suspend,
  3467. .resume = b43_resume,
  3468. };
  3469. static int __init b43_init(void)
  3470. {
  3471. int err;
  3472. b43_debugfs_init();
  3473. err = b43_pcmcia_init();
  3474. if (err)
  3475. goto err_dfs_exit;
  3476. err = ssb_driver_register(&b43_ssb_driver);
  3477. if (err)
  3478. goto err_pcmcia_exit;
  3479. return err;
  3480. err_pcmcia_exit:
  3481. b43_pcmcia_exit();
  3482. err_dfs_exit:
  3483. b43_debugfs_exit();
  3484. return err;
  3485. }
  3486. static void __exit b43_exit(void)
  3487. {
  3488. ssb_driver_unregister(&b43_ssb_driver);
  3489. b43_pcmcia_exit();
  3490. b43_debugfs_exit();
  3491. }
  3492. module_init(b43_init)
  3493. module_exit(b43_exit)