main.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036
  1. /*
  2. *
  3. * Broadcom B43legacy wireless driver
  4. *
  5. * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
  6. * Copyright (c) 2005-2008 Stefano Brivio <stefano.brivio@polimi.it>
  7. * Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  8. * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  9. * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  10. * Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
  11. *
  12. * Some parts of the code in this file are derived from the ipw2200
  13. * driver Copyright(c) 2003 - 2004 Intel Corporation.
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; see the file COPYING. If not, write to
  26. * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  27. * Boston, MA 02110-1301, USA.
  28. *
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/firmware.h>
  36. #include <linux/wireless.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/slab.h>
  42. #include <net/dst.h>
  43. #include <asm/unaligned.h>
  44. #include "b43legacy.h"
  45. #include "main.h"
  46. #include "debugfs.h"
  47. #include "phy.h"
  48. #include "dma.h"
  49. #include "pio.h"
  50. #include "sysfs.h"
  51. #include "xmit.h"
  52. #include "radio.h"
  53. MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
  54. MODULE_AUTHOR("Martin Langer");
  55. MODULE_AUTHOR("Stefano Brivio");
  56. MODULE_AUTHOR("Michael Buesch");
  57. MODULE_LICENSE("GPL");
  58. MODULE_FIRMWARE(B43legacy_SUPPORTED_FIRMWARE_ID);
  59. MODULE_FIRMWARE("b43legacy/ucode2.fw");
  60. MODULE_FIRMWARE("b43legacy/ucode4.fw");
  61. #if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
  62. static int modparam_pio;
  63. module_param_named(pio, modparam_pio, int, 0444);
  64. MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
  65. #elif defined(CONFIG_B43LEGACY_DMA)
  66. # define modparam_pio 0
  67. #elif defined(CONFIG_B43LEGACY_PIO)
  68. # define modparam_pio 1
  69. #endif
  70. static int modparam_bad_frames_preempt;
  71. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  72. MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
  73. " Preemption");
  74. static char modparam_fwpostfix[16];
  75. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  76. MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
  77. /* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
  78. static const struct ssb_device_id b43legacy_ssb_tbl[] = {
  79. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
  80. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
  81. SSB_DEVTABLE_END
  82. };
  83. MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
  84. /* Channel and ratetables are shared for all devices.
  85. * They can't be const, because ieee80211 puts some precalculated
  86. * data in there. This data is the same for all devices, so we don't
  87. * get concurrency issues */
  88. #define RATETAB_ENT(_rateid, _flags) \
  89. { \
  90. .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
  91. .hw_value = (_rateid), \
  92. .flags = (_flags), \
  93. }
  94. /*
  95. * NOTE: When changing this, sync with xmit.c's
  96. * b43legacy_plcp_get_bitrate_idx_* functions!
  97. */
  98. static struct ieee80211_rate __b43legacy_ratetable[] = {
  99. RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
  100. RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
  101. RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
  102. RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
  103. RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
  104. RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
  105. RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
  106. RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
  107. RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
  108. RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
  109. RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
  110. RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
  111. };
  112. #define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
  113. #define b43legacy_b_ratetable_size 4
  114. #define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
  115. #define b43legacy_g_ratetable_size 12
  116. #define CHANTAB_ENT(_chanid, _freq) \
  117. { \
  118. .center_freq = (_freq), \
  119. .hw_value = (_chanid), \
  120. }
  121. static struct ieee80211_channel b43legacy_bg_chantable[] = {
  122. CHANTAB_ENT(1, 2412),
  123. CHANTAB_ENT(2, 2417),
  124. CHANTAB_ENT(3, 2422),
  125. CHANTAB_ENT(4, 2427),
  126. CHANTAB_ENT(5, 2432),
  127. CHANTAB_ENT(6, 2437),
  128. CHANTAB_ENT(7, 2442),
  129. CHANTAB_ENT(8, 2447),
  130. CHANTAB_ENT(9, 2452),
  131. CHANTAB_ENT(10, 2457),
  132. CHANTAB_ENT(11, 2462),
  133. CHANTAB_ENT(12, 2467),
  134. CHANTAB_ENT(13, 2472),
  135. CHANTAB_ENT(14, 2484),
  136. };
  137. static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
  138. .channels = b43legacy_bg_chantable,
  139. .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
  140. .bitrates = b43legacy_b_ratetable,
  141. .n_bitrates = b43legacy_b_ratetable_size,
  142. };
  143. static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
  144. .channels = b43legacy_bg_chantable,
  145. .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
  146. .bitrates = b43legacy_g_ratetable,
  147. .n_bitrates = b43legacy_g_ratetable_size,
  148. };
  149. static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
  150. static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
  151. static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
  152. static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
  153. static int b43legacy_ratelimit(struct b43legacy_wl *wl)
  154. {
  155. if (!wl || !wl->current_dev)
  156. return 1;
  157. if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
  158. return 1;
  159. /* We are up and running.
  160. * Ratelimit the messages to avoid DoS over the net. */
  161. return net_ratelimit();
  162. }
  163. void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
  164. {
  165. struct va_format vaf;
  166. va_list args;
  167. if (!b43legacy_ratelimit(wl))
  168. return;
  169. va_start(args, fmt);
  170. vaf.fmt = fmt;
  171. vaf.va = &args;
  172. printk(KERN_INFO "b43legacy-%s: %pV",
  173. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
  174. va_end(args);
  175. }
  176. void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
  177. {
  178. struct va_format vaf;
  179. va_list args;
  180. if (!b43legacy_ratelimit(wl))
  181. return;
  182. va_start(args, fmt);
  183. vaf.fmt = fmt;
  184. vaf.va = &args;
  185. printk(KERN_ERR "b43legacy-%s ERROR: %pV",
  186. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
  187. va_end(args);
  188. }
  189. void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
  190. {
  191. struct va_format vaf;
  192. va_list args;
  193. if (!b43legacy_ratelimit(wl))
  194. return;
  195. va_start(args, fmt);
  196. vaf.fmt = fmt;
  197. vaf.va = &args;
  198. printk(KERN_WARNING "b43legacy-%s warning: %pV",
  199. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
  200. va_end(args);
  201. }
  202. #if B43legacy_DEBUG
  203. void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
  204. {
  205. struct va_format vaf;
  206. va_list args;
  207. va_start(args, fmt);
  208. vaf.fmt = fmt;
  209. vaf.va = &args;
  210. printk(KERN_DEBUG "b43legacy-%s debug: %pV",
  211. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
  212. va_end(args);
  213. }
  214. #endif /* DEBUG */
  215. static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
  216. u32 val)
  217. {
  218. u32 status;
  219. B43legacy_WARN_ON(offset % 4 != 0);
  220. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  221. if (status & B43legacy_MACCTL_BE)
  222. val = swab32(val);
  223. b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
  224. mmiowb();
  225. b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
  226. }
  227. static inline
  228. void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
  229. u16 routing, u16 offset)
  230. {
  231. u32 control;
  232. /* "offset" is the WORD offset. */
  233. control = routing;
  234. control <<= 16;
  235. control |= offset;
  236. b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
  237. }
  238. u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
  239. u16 routing, u16 offset)
  240. {
  241. u32 ret;
  242. if (routing == B43legacy_SHM_SHARED) {
  243. B43legacy_WARN_ON((offset & 0x0001) != 0);
  244. if (offset & 0x0003) {
  245. /* Unaligned access */
  246. b43legacy_shm_control_word(dev, routing, offset >> 2);
  247. ret = b43legacy_read16(dev,
  248. B43legacy_MMIO_SHM_DATA_UNALIGNED);
  249. ret <<= 16;
  250. b43legacy_shm_control_word(dev, routing,
  251. (offset >> 2) + 1);
  252. ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
  253. return ret;
  254. }
  255. offset >>= 2;
  256. }
  257. b43legacy_shm_control_word(dev, routing, offset);
  258. ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
  259. return ret;
  260. }
  261. u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
  262. u16 routing, u16 offset)
  263. {
  264. u16 ret;
  265. if (routing == B43legacy_SHM_SHARED) {
  266. B43legacy_WARN_ON((offset & 0x0001) != 0);
  267. if (offset & 0x0003) {
  268. /* Unaligned access */
  269. b43legacy_shm_control_word(dev, routing, offset >> 2);
  270. ret = b43legacy_read16(dev,
  271. B43legacy_MMIO_SHM_DATA_UNALIGNED);
  272. return ret;
  273. }
  274. offset >>= 2;
  275. }
  276. b43legacy_shm_control_word(dev, routing, offset);
  277. ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
  278. return ret;
  279. }
  280. void b43legacy_shm_write32(struct b43legacy_wldev *dev,
  281. u16 routing, u16 offset,
  282. u32 value)
  283. {
  284. if (routing == B43legacy_SHM_SHARED) {
  285. B43legacy_WARN_ON((offset & 0x0001) != 0);
  286. if (offset & 0x0003) {
  287. /* Unaligned access */
  288. b43legacy_shm_control_word(dev, routing, offset >> 2);
  289. mmiowb();
  290. b43legacy_write16(dev,
  291. B43legacy_MMIO_SHM_DATA_UNALIGNED,
  292. (value >> 16) & 0xffff);
  293. mmiowb();
  294. b43legacy_shm_control_word(dev, routing,
  295. (offset >> 2) + 1);
  296. mmiowb();
  297. b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
  298. value & 0xffff);
  299. return;
  300. }
  301. offset >>= 2;
  302. }
  303. b43legacy_shm_control_word(dev, routing, offset);
  304. mmiowb();
  305. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
  306. }
  307. void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
  308. u16 value)
  309. {
  310. if (routing == B43legacy_SHM_SHARED) {
  311. B43legacy_WARN_ON((offset & 0x0001) != 0);
  312. if (offset & 0x0003) {
  313. /* Unaligned access */
  314. b43legacy_shm_control_word(dev, routing, offset >> 2);
  315. mmiowb();
  316. b43legacy_write16(dev,
  317. B43legacy_MMIO_SHM_DATA_UNALIGNED,
  318. value);
  319. return;
  320. }
  321. offset >>= 2;
  322. }
  323. b43legacy_shm_control_word(dev, routing, offset);
  324. mmiowb();
  325. b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
  326. }
  327. /* Read HostFlags */
  328. u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
  329. {
  330. u32 ret;
  331. ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  332. B43legacy_SHM_SH_HOSTFHI);
  333. ret <<= 16;
  334. ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  335. B43legacy_SHM_SH_HOSTFLO);
  336. return ret;
  337. }
  338. /* Write HostFlags */
  339. void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
  340. {
  341. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  342. B43legacy_SHM_SH_HOSTFLO,
  343. (value & 0x0000FFFF));
  344. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  345. B43legacy_SHM_SH_HOSTFHI,
  346. ((value & 0xFFFF0000) >> 16));
  347. }
  348. void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
  349. {
  350. /* We need to be careful. As we read the TSF from multiple
  351. * registers, we should take care of register overflows.
  352. * In theory, the whole tsf read process should be atomic.
  353. * We try to be atomic here, by restaring the read process,
  354. * if any of the high registers changed (overflew).
  355. */
  356. if (dev->dev->id.revision >= 3) {
  357. u32 low;
  358. u32 high;
  359. u32 high2;
  360. do {
  361. high = b43legacy_read32(dev,
  362. B43legacy_MMIO_REV3PLUS_TSF_HIGH);
  363. low = b43legacy_read32(dev,
  364. B43legacy_MMIO_REV3PLUS_TSF_LOW);
  365. high2 = b43legacy_read32(dev,
  366. B43legacy_MMIO_REV3PLUS_TSF_HIGH);
  367. } while (unlikely(high != high2));
  368. *tsf = high;
  369. *tsf <<= 32;
  370. *tsf |= low;
  371. } else {
  372. u64 tmp;
  373. u16 v0;
  374. u16 v1;
  375. u16 v2;
  376. u16 v3;
  377. u16 test1;
  378. u16 test2;
  379. u16 test3;
  380. do {
  381. v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
  382. v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
  383. v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
  384. v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
  385. test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
  386. test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
  387. test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
  388. } while (v3 != test3 || v2 != test2 || v1 != test1);
  389. *tsf = v3;
  390. *tsf <<= 48;
  391. tmp = v2;
  392. tmp <<= 32;
  393. *tsf |= tmp;
  394. tmp = v1;
  395. tmp <<= 16;
  396. *tsf |= tmp;
  397. *tsf |= v0;
  398. }
  399. }
  400. static void b43legacy_time_lock(struct b43legacy_wldev *dev)
  401. {
  402. u32 status;
  403. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  404. status |= B43legacy_MACCTL_TBTTHOLD;
  405. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
  406. mmiowb();
  407. }
  408. static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
  409. {
  410. u32 status;
  411. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  412. status &= ~B43legacy_MACCTL_TBTTHOLD;
  413. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
  414. }
  415. static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
  416. {
  417. /* Be careful with the in-progress timer.
  418. * First zero out the low register, so we have a full
  419. * register-overflow duration to complete the operation.
  420. */
  421. if (dev->dev->id.revision >= 3) {
  422. u32 lo = (tsf & 0x00000000FFFFFFFFULL);
  423. u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
  424. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
  425. mmiowb();
  426. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
  427. hi);
  428. mmiowb();
  429. b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
  430. lo);
  431. } else {
  432. u16 v0 = (tsf & 0x000000000000FFFFULL);
  433. u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
  434. u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
  435. u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
  436. b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
  437. mmiowb();
  438. b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
  439. mmiowb();
  440. b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
  441. mmiowb();
  442. b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
  443. mmiowb();
  444. b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
  445. }
  446. }
  447. void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
  448. {
  449. b43legacy_time_lock(dev);
  450. b43legacy_tsf_write_locked(dev, tsf);
  451. b43legacy_time_unlock(dev);
  452. }
  453. static
  454. void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
  455. u16 offset, const u8 *mac)
  456. {
  457. static const u8 zero_addr[ETH_ALEN] = { 0 };
  458. u16 data;
  459. if (!mac)
  460. mac = zero_addr;
  461. offset |= 0x0020;
  462. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
  463. data = mac[0];
  464. data |= mac[1] << 8;
  465. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  466. data = mac[2];
  467. data |= mac[3] << 8;
  468. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  469. data = mac[4];
  470. data |= mac[5] << 8;
  471. b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
  472. }
  473. static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
  474. {
  475. static const u8 zero_addr[ETH_ALEN] = { 0 };
  476. const u8 *mac = dev->wl->mac_addr;
  477. const u8 *bssid = dev->wl->bssid;
  478. u8 mac_bssid[ETH_ALEN * 2];
  479. int i;
  480. u32 tmp;
  481. if (!bssid)
  482. bssid = zero_addr;
  483. if (!mac)
  484. mac = zero_addr;
  485. b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
  486. memcpy(mac_bssid, mac, ETH_ALEN);
  487. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  488. /* Write our MAC address and BSSID to template ram */
  489. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  490. tmp = (u32)(mac_bssid[i + 0]);
  491. tmp |= (u32)(mac_bssid[i + 1]) << 8;
  492. tmp |= (u32)(mac_bssid[i + 2]) << 16;
  493. tmp |= (u32)(mac_bssid[i + 3]) << 24;
  494. b43legacy_ram_write(dev, 0x20 + i, tmp);
  495. b43legacy_ram_write(dev, 0x78 + i, tmp);
  496. b43legacy_ram_write(dev, 0x478 + i, tmp);
  497. }
  498. }
  499. static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
  500. {
  501. b43legacy_write_mac_bssid_templates(dev);
  502. b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
  503. dev->wl->mac_addr);
  504. }
  505. static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
  506. u16 slot_time)
  507. {
  508. /* slot_time is in usec. */
  509. if (dev->phy.type != B43legacy_PHYTYPE_G)
  510. return;
  511. b43legacy_write16(dev, 0x684, 510 + slot_time);
  512. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
  513. slot_time);
  514. }
  515. static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
  516. {
  517. b43legacy_set_slot_time(dev, 9);
  518. }
  519. static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
  520. {
  521. b43legacy_set_slot_time(dev, 20);
  522. }
  523. /* Synchronize IRQ top- and bottom-half.
  524. * IRQs must be masked before calling this.
  525. * This must not be called with the irq_lock held.
  526. */
  527. static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
  528. {
  529. synchronize_irq(dev->dev->irq);
  530. tasklet_kill(&dev->isr_tasklet);
  531. }
  532. /* DummyTransmission function, as documented on
  533. * http://bcm-specs.sipsolutions.net/DummyTransmission
  534. */
  535. void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
  536. {
  537. struct b43legacy_phy *phy = &dev->phy;
  538. unsigned int i;
  539. unsigned int max_loop;
  540. u16 value;
  541. u32 buffer[5] = {
  542. 0x00000000,
  543. 0x00D40000,
  544. 0x00000000,
  545. 0x01000000,
  546. 0x00000000,
  547. };
  548. switch (phy->type) {
  549. case B43legacy_PHYTYPE_B:
  550. case B43legacy_PHYTYPE_G:
  551. max_loop = 0xFA;
  552. buffer[0] = 0x000B846E;
  553. break;
  554. default:
  555. B43legacy_BUG_ON(1);
  556. return;
  557. }
  558. for (i = 0; i < 5; i++)
  559. b43legacy_ram_write(dev, i * 4, buffer[i]);
  560. /* dummy read follows */
  561. b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  562. b43legacy_write16(dev, 0x0568, 0x0000);
  563. b43legacy_write16(dev, 0x07C0, 0x0000);
  564. b43legacy_write16(dev, 0x050C, 0x0000);
  565. b43legacy_write16(dev, 0x0508, 0x0000);
  566. b43legacy_write16(dev, 0x050A, 0x0000);
  567. b43legacy_write16(dev, 0x054C, 0x0000);
  568. b43legacy_write16(dev, 0x056A, 0x0014);
  569. b43legacy_write16(dev, 0x0568, 0x0826);
  570. b43legacy_write16(dev, 0x0500, 0x0000);
  571. b43legacy_write16(dev, 0x0502, 0x0030);
  572. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  573. b43legacy_radio_write16(dev, 0x0051, 0x0017);
  574. for (i = 0x00; i < max_loop; i++) {
  575. value = b43legacy_read16(dev, 0x050E);
  576. if (value & 0x0080)
  577. break;
  578. udelay(10);
  579. }
  580. for (i = 0x00; i < 0x0A; i++) {
  581. value = b43legacy_read16(dev, 0x050E);
  582. if (value & 0x0400)
  583. break;
  584. udelay(10);
  585. }
  586. for (i = 0x00; i < 0x0A; i++) {
  587. value = b43legacy_read16(dev, 0x0690);
  588. if (!(value & 0x0100))
  589. break;
  590. udelay(10);
  591. }
  592. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  593. b43legacy_radio_write16(dev, 0x0051, 0x0037);
  594. }
  595. /* Turn the Analog ON/OFF */
  596. static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
  597. {
  598. b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
  599. }
  600. void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
  601. {
  602. u32 tmslow;
  603. u32 macctl;
  604. flags |= B43legacy_TMSLOW_PHYCLKEN;
  605. flags |= B43legacy_TMSLOW_PHYRESET;
  606. ssb_device_enable(dev->dev, flags);
  607. msleep(2); /* Wait for the PLL to turn on. */
  608. /* Now take the PHY out of Reset again */
  609. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  610. tmslow |= SSB_TMSLOW_FGC;
  611. tmslow &= ~B43legacy_TMSLOW_PHYRESET;
  612. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  613. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  614. msleep(1);
  615. tmslow &= ~SSB_TMSLOW_FGC;
  616. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  617. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  618. msleep(1);
  619. /* Turn Analog ON */
  620. b43legacy_switch_analog(dev, 1);
  621. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  622. macctl &= ~B43legacy_MACCTL_GMODE;
  623. if (flags & B43legacy_TMSLOW_GMODE) {
  624. macctl |= B43legacy_MACCTL_GMODE;
  625. dev->phy.gmode = 1;
  626. } else
  627. dev->phy.gmode = 0;
  628. macctl |= B43legacy_MACCTL_IHR_ENABLED;
  629. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  630. }
  631. static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
  632. {
  633. u32 v0;
  634. u32 v1;
  635. u16 tmp;
  636. struct b43legacy_txstatus stat;
  637. while (1) {
  638. v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
  639. if (!(v0 & 0x00000001))
  640. break;
  641. v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
  642. stat.cookie = (v0 >> 16);
  643. stat.seq = (v1 & 0x0000FFFF);
  644. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  645. tmp = (v0 & 0x0000FFFF);
  646. stat.frame_count = ((tmp & 0xF000) >> 12);
  647. stat.rts_count = ((tmp & 0x0F00) >> 8);
  648. stat.supp_reason = ((tmp & 0x001C) >> 2);
  649. stat.pm_indicated = !!(tmp & 0x0080);
  650. stat.intermediate = !!(tmp & 0x0040);
  651. stat.for_ampdu = !!(tmp & 0x0020);
  652. stat.acked = !!(tmp & 0x0002);
  653. b43legacy_handle_txstatus(dev, &stat);
  654. }
  655. }
  656. static void drain_txstatus_queue(struct b43legacy_wldev *dev)
  657. {
  658. u32 dummy;
  659. if (dev->dev->id.revision < 5)
  660. return;
  661. /* Read all entries from the microcode TXstatus FIFO
  662. * and throw them away.
  663. */
  664. while (1) {
  665. dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
  666. if (!(dummy & 0x00000001))
  667. break;
  668. dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
  669. }
  670. }
  671. static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
  672. {
  673. u32 val = 0;
  674. val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
  675. val <<= 16;
  676. val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
  677. return val;
  678. }
  679. static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
  680. {
  681. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
  682. (jssi & 0x0000FFFF));
  683. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
  684. (jssi & 0xFFFF0000) >> 16);
  685. }
  686. static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
  687. {
  688. b43legacy_jssi_write(dev, 0x7F7F7F7F);
  689. b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
  690. b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
  691. | B43legacy_MACCMD_BGNOISE);
  692. B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
  693. dev->phy.channel);
  694. }
  695. static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
  696. {
  697. /* Top half of Link Quality calculation. */
  698. if (dev->noisecalc.calculation_running)
  699. return;
  700. dev->noisecalc.channel_at_start = dev->phy.channel;
  701. dev->noisecalc.calculation_running = 1;
  702. dev->noisecalc.nr_samples = 0;
  703. b43legacy_generate_noise_sample(dev);
  704. }
  705. static void handle_irq_noise(struct b43legacy_wldev *dev)
  706. {
  707. struct b43legacy_phy *phy = &dev->phy;
  708. u16 tmp;
  709. u8 noise[4];
  710. u8 i;
  711. u8 j;
  712. s32 average;
  713. /* Bottom half of Link Quality calculation. */
  714. B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
  715. if (dev->noisecalc.channel_at_start != phy->channel)
  716. goto drop_calculation;
  717. *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
  718. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  719. noise[2] == 0x7F || noise[3] == 0x7F)
  720. goto generate_new;
  721. /* Get the noise samples. */
  722. B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
  723. i = dev->noisecalc.nr_samples;
  724. noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  725. noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  726. noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  727. noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  728. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  729. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  730. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  731. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  732. dev->noisecalc.nr_samples++;
  733. if (dev->noisecalc.nr_samples == 8) {
  734. /* Calculate the Link Quality by the noise samples. */
  735. average = 0;
  736. for (i = 0; i < 8; i++) {
  737. for (j = 0; j < 4; j++)
  738. average += dev->noisecalc.samples[i][j];
  739. }
  740. average /= (8 * 4);
  741. average *= 125;
  742. average += 64;
  743. average /= 128;
  744. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  745. 0x40C);
  746. tmp = (tmp / 128) & 0x1F;
  747. if (tmp >= 8)
  748. average += 2;
  749. else
  750. average -= 25;
  751. if (tmp == 8)
  752. average -= 72;
  753. else
  754. average -= 48;
  755. dev->stats.link_noise = average;
  756. drop_calculation:
  757. dev->noisecalc.calculation_running = 0;
  758. return;
  759. }
  760. generate_new:
  761. b43legacy_generate_noise_sample(dev);
  762. }
  763. static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
  764. {
  765. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
  766. /* TODO: PS TBTT */
  767. } else {
  768. if (1/*FIXME: the last PSpoll frame was sent successfully */)
  769. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  770. }
  771. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  772. dev->dfq_valid = 1;
  773. }
  774. static void handle_irq_atim_end(struct b43legacy_wldev *dev)
  775. {
  776. if (dev->dfq_valid) {
  777. b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
  778. b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
  779. | B43legacy_MACCMD_DFQ_VALID);
  780. dev->dfq_valid = 0;
  781. }
  782. }
  783. static void handle_irq_pmq(struct b43legacy_wldev *dev)
  784. {
  785. u32 tmp;
  786. /* TODO: AP mode. */
  787. while (1) {
  788. tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
  789. if (!(tmp & 0x00000008))
  790. break;
  791. }
  792. /* 16bit write is odd, but correct. */
  793. b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
  794. }
  795. static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
  796. const u8 *data, u16 size,
  797. u16 ram_offset,
  798. u16 shm_size_offset, u8 rate)
  799. {
  800. u32 i;
  801. u32 tmp;
  802. struct b43legacy_plcp_hdr4 plcp;
  803. plcp.data = 0;
  804. b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  805. b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  806. ram_offset += sizeof(u32);
  807. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  808. * So leave the first two bytes of the next write blank.
  809. */
  810. tmp = (u32)(data[0]) << 16;
  811. tmp |= (u32)(data[1]) << 24;
  812. b43legacy_ram_write(dev, ram_offset, tmp);
  813. ram_offset += sizeof(u32);
  814. for (i = 2; i < size; i += sizeof(u32)) {
  815. tmp = (u32)(data[i + 0]);
  816. if (i + 1 < size)
  817. tmp |= (u32)(data[i + 1]) << 8;
  818. if (i + 2 < size)
  819. tmp |= (u32)(data[i + 2]) << 16;
  820. if (i + 3 < size)
  821. tmp |= (u32)(data[i + 3]) << 24;
  822. b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
  823. }
  824. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
  825. size + sizeof(struct b43legacy_plcp_hdr6));
  826. }
  827. /* Convert a b43legacy antenna number value to the PHY TX control value. */
  828. static u16 b43legacy_antenna_to_phyctl(int antenna)
  829. {
  830. switch (antenna) {
  831. case B43legacy_ANTENNA0:
  832. return B43legacy_TX4_PHY_ANT0;
  833. case B43legacy_ANTENNA1:
  834. return B43legacy_TX4_PHY_ANT1;
  835. }
  836. return B43legacy_TX4_PHY_ANTLAST;
  837. }
  838. static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
  839. u16 ram_offset,
  840. u16 shm_size_offset)
  841. {
  842. unsigned int i, len, variable_len;
  843. const struct ieee80211_mgmt *bcn;
  844. const u8 *ie;
  845. bool tim_found = 0;
  846. unsigned int rate;
  847. u16 ctl;
  848. int antenna;
  849. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
  850. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  851. len = min((size_t)dev->wl->current_beacon->len,
  852. 0x200 - sizeof(struct b43legacy_plcp_hdr6));
  853. rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
  854. b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
  855. shm_size_offset, rate);
  856. /* Write the PHY TX control parameters. */
  857. antenna = B43legacy_ANTENNA_DEFAULT;
  858. antenna = b43legacy_antenna_to_phyctl(antenna);
  859. ctl = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  860. B43legacy_SHM_SH_BEACPHYCTL);
  861. /* We can't send beacons with short preamble. Would get PHY errors. */
  862. ctl &= ~B43legacy_TX4_PHY_SHORTPRMBL;
  863. ctl &= ~B43legacy_TX4_PHY_ANT;
  864. ctl &= ~B43legacy_TX4_PHY_ENC;
  865. ctl |= antenna;
  866. ctl |= B43legacy_TX4_PHY_ENC_CCK;
  867. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  868. B43legacy_SHM_SH_BEACPHYCTL, ctl);
  869. /* Find the position of the TIM and the DTIM_period value
  870. * and write them to SHM. */
  871. ie = bcn->u.beacon.variable;
  872. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  873. for (i = 0; i < variable_len - 2; ) {
  874. uint8_t ie_id, ie_len;
  875. ie_id = ie[i];
  876. ie_len = ie[i + 1];
  877. if (ie_id == 5) {
  878. u16 tim_position;
  879. u16 dtim_period;
  880. /* This is the TIM Information Element */
  881. /* Check whether the ie_len is in the beacon data range. */
  882. if (variable_len < ie_len + 2 + i)
  883. break;
  884. /* A valid TIM is at least 4 bytes long. */
  885. if (ie_len < 4)
  886. break;
  887. tim_found = 1;
  888. tim_position = sizeof(struct b43legacy_plcp_hdr6);
  889. tim_position += offsetof(struct ieee80211_mgmt,
  890. u.beacon.variable);
  891. tim_position += i;
  892. dtim_period = ie[i + 3];
  893. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  894. B43legacy_SHM_SH_TIMPOS, tim_position);
  895. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  896. B43legacy_SHM_SH_DTIMP, dtim_period);
  897. break;
  898. }
  899. i += ie_len + 2;
  900. }
  901. if (!tim_found) {
  902. b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
  903. "beacon template packet. AP or IBSS operation "
  904. "may be broken.\n");
  905. } else
  906. b43legacydbg(dev->wl, "Updated beacon template\n");
  907. }
  908. static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
  909. u16 shm_offset, u16 size,
  910. struct ieee80211_rate *rate)
  911. {
  912. struct b43legacy_plcp_hdr4 plcp;
  913. u32 tmp;
  914. __le16 dur;
  915. plcp.data = 0;
  916. b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
  917. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  918. dev->wl->vif,
  919. size,
  920. rate);
  921. /* Write PLCP in two parts and timing for packet transfer */
  922. tmp = le32_to_cpu(plcp.data);
  923. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
  924. tmp & 0xFFFF);
  925. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
  926. tmp >> 16);
  927. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
  928. le16_to_cpu(dur));
  929. }
  930. /* Instead of using custom probe response template, this function
  931. * just patches custom beacon template by:
  932. * 1) Changing packet type
  933. * 2) Patching duration field
  934. * 3) Stripping TIM
  935. */
  936. static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
  937. u16 *dest_size,
  938. struct ieee80211_rate *rate)
  939. {
  940. const u8 *src_data;
  941. u8 *dest_data;
  942. u16 src_size, elem_size, src_pos, dest_pos;
  943. __le16 dur;
  944. struct ieee80211_hdr *hdr;
  945. size_t ie_start;
  946. src_size = dev->wl->current_beacon->len;
  947. src_data = (const u8 *)dev->wl->current_beacon->data;
  948. /* Get the start offset of the variable IEs in the packet. */
  949. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  950. B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
  951. u.beacon.variable));
  952. if (B43legacy_WARN_ON(src_size < ie_start))
  953. return NULL;
  954. dest_data = kmalloc(src_size, GFP_ATOMIC);
  955. if (unlikely(!dest_data))
  956. return NULL;
  957. /* Copy the static data and all Information Elements, except the TIM. */
  958. memcpy(dest_data, src_data, ie_start);
  959. src_pos = ie_start;
  960. dest_pos = ie_start;
  961. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  962. elem_size = src_data[src_pos + 1] + 2;
  963. if (src_data[src_pos] == 5) {
  964. /* This is the TIM. */
  965. continue;
  966. }
  967. memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
  968. dest_pos += elem_size;
  969. }
  970. *dest_size = dest_pos;
  971. hdr = (struct ieee80211_hdr *)dest_data;
  972. /* Set the frame control. */
  973. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  974. IEEE80211_STYPE_PROBE_RESP);
  975. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  976. dev->wl->vif,
  977. *dest_size,
  978. rate);
  979. hdr->duration_id = dur;
  980. return dest_data;
  981. }
  982. static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
  983. u16 ram_offset,
  984. u16 shm_size_offset,
  985. struct ieee80211_rate *rate)
  986. {
  987. const u8 *probe_resp_data;
  988. u16 size;
  989. size = dev->wl->current_beacon->len;
  990. probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
  991. if (unlikely(!probe_resp_data))
  992. return;
  993. /* Looks like PLCP headers plus packet timings are stored for
  994. * all possible basic rates
  995. */
  996. b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
  997. &b43legacy_b_ratetable[0]);
  998. b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
  999. &b43legacy_b_ratetable[1]);
  1000. b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
  1001. &b43legacy_b_ratetable[2]);
  1002. b43legacy_write_probe_resp_plcp(dev, 0x350, size,
  1003. &b43legacy_b_ratetable[3]);
  1004. size = min((size_t)size,
  1005. 0x200 - sizeof(struct b43legacy_plcp_hdr6));
  1006. b43legacy_write_template_common(dev, probe_resp_data,
  1007. size, ram_offset,
  1008. shm_size_offset, rate->hw_value);
  1009. kfree(probe_resp_data);
  1010. }
  1011. static void b43legacy_upload_beacon0(struct b43legacy_wldev *dev)
  1012. {
  1013. struct b43legacy_wl *wl = dev->wl;
  1014. if (wl->beacon0_uploaded)
  1015. return;
  1016. b43legacy_write_beacon_template(dev, 0x68, 0x18);
  1017. /* FIXME: Probe resp upload doesn't really belong here,
  1018. * but we don't use that feature anyway. */
  1019. b43legacy_write_probe_resp_template(dev, 0x268, 0x4A,
  1020. &__b43legacy_ratetable[3]);
  1021. wl->beacon0_uploaded = 1;
  1022. }
  1023. static void b43legacy_upload_beacon1(struct b43legacy_wldev *dev)
  1024. {
  1025. struct b43legacy_wl *wl = dev->wl;
  1026. if (wl->beacon1_uploaded)
  1027. return;
  1028. b43legacy_write_beacon_template(dev, 0x468, 0x1A);
  1029. wl->beacon1_uploaded = 1;
  1030. }
  1031. static void handle_irq_beacon(struct b43legacy_wldev *dev)
  1032. {
  1033. struct b43legacy_wl *wl = dev->wl;
  1034. u32 cmd, beacon0_valid, beacon1_valid;
  1035. if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1036. return;
  1037. /* This is the bottom half of the asynchronous beacon update. */
  1038. /* Ignore interrupt in the future. */
  1039. dev->irq_mask &= ~B43legacy_IRQ_BEACON;
  1040. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1041. beacon0_valid = (cmd & B43legacy_MACCMD_BEACON0_VALID);
  1042. beacon1_valid = (cmd & B43legacy_MACCMD_BEACON1_VALID);
  1043. /* Schedule interrupt manually, if busy. */
  1044. if (beacon0_valid && beacon1_valid) {
  1045. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, B43legacy_IRQ_BEACON);
  1046. dev->irq_mask |= B43legacy_IRQ_BEACON;
  1047. return;
  1048. }
  1049. if (unlikely(wl->beacon_templates_virgin)) {
  1050. /* We never uploaded a beacon before.
  1051. * Upload both templates now, but only mark one valid. */
  1052. wl->beacon_templates_virgin = 0;
  1053. b43legacy_upload_beacon0(dev);
  1054. b43legacy_upload_beacon1(dev);
  1055. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1056. cmd |= B43legacy_MACCMD_BEACON0_VALID;
  1057. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1058. } else {
  1059. if (!beacon0_valid) {
  1060. b43legacy_upload_beacon0(dev);
  1061. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1062. cmd |= B43legacy_MACCMD_BEACON0_VALID;
  1063. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1064. } else if (!beacon1_valid) {
  1065. b43legacy_upload_beacon1(dev);
  1066. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1067. cmd |= B43legacy_MACCMD_BEACON1_VALID;
  1068. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1069. }
  1070. }
  1071. }
  1072. static void b43legacy_beacon_update_trigger_work(struct work_struct *work)
  1073. {
  1074. struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
  1075. beacon_update_trigger);
  1076. struct b43legacy_wldev *dev;
  1077. mutex_lock(&wl->mutex);
  1078. dev = wl->current_dev;
  1079. if (likely(dev && (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED))) {
  1080. spin_lock_irq(&wl->irq_lock);
  1081. /* Update beacon right away or defer to IRQ. */
  1082. handle_irq_beacon(dev);
  1083. /* The handler might have updated the IRQ mask. */
  1084. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
  1085. dev->irq_mask);
  1086. mmiowb();
  1087. spin_unlock_irq(&wl->irq_lock);
  1088. }
  1089. mutex_unlock(&wl->mutex);
  1090. }
  1091. /* Asynchronously update the packet templates in template RAM.
  1092. * Locking: Requires wl->irq_lock to be locked. */
  1093. static void b43legacy_update_templates(struct b43legacy_wl *wl)
  1094. {
  1095. struct sk_buff *beacon;
  1096. /* This is the top half of the ansynchronous beacon update. The bottom
  1097. * half is the beacon IRQ. Beacon update must be asynchronous to avoid
  1098. * sending an invalid beacon. This can happen for example, if the
  1099. * firmware transmits a beacon while we are updating it. */
  1100. /* We could modify the existing beacon and set the aid bit in the TIM
  1101. * field, but that would probably require resizing and moving of data
  1102. * within the beacon template. Simply request a new beacon and let
  1103. * mac80211 do the hard work. */
  1104. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1105. if (unlikely(!beacon))
  1106. return;
  1107. if (wl->current_beacon)
  1108. dev_kfree_skb_any(wl->current_beacon);
  1109. wl->current_beacon = beacon;
  1110. wl->beacon0_uploaded = 0;
  1111. wl->beacon1_uploaded = 0;
  1112. ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
  1113. }
  1114. static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
  1115. u16 beacon_int)
  1116. {
  1117. b43legacy_time_lock(dev);
  1118. if (dev->dev->id.revision >= 3) {
  1119. b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_REP,
  1120. (beacon_int << 16));
  1121. b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_START,
  1122. (beacon_int << 10));
  1123. } else {
  1124. b43legacy_write16(dev, 0x606, (beacon_int >> 6));
  1125. b43legacy_write16(dev, 0x610, beacon_int);
  1126. }
  1127. b43legacy_time_unlock(dev);
  1128. b43legacydbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
  1129. }
  1130. static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
  1131. {
  1132. }
  1133. /* Interrupt handler bottom-half */
  1134. static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
  1135. {
  1136. u32 reason;
  1137. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1138. u32 merged_dma_reason = 0;
  1139. int i;
  1140. unsigned long flags;
  1141. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1142. B43legacy_WARN_ON(b43legacy_status(dev) <
  1143. B43legacy_STAT_INITIALIZED);
  1144. reason = dev->irq_reason;
  1145. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1146. dma_reason[i] = dev->dma_reason[i];
  1147. merged_dma_reason |= dma_reason[i];
  1148. }
  1149. if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
  1150. b43legacyerr(dev->wl, "MAC transmission error\n");
  1151. if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
  1152. b43legacyerr(dev->wl, "PHY transmission error\n");
  1153. rmb();
  1154. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1155. b43legacyerr(dev->wl, "Too many PHY TX errors, "
  1156. "restarting the controller\n");
  1157. b43legacy_controller_restart(dev, "PHY TX errors");
  1158. }
  1159. }
  1160. if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
  1161. B43legacy_DMAIRQ_NONFATALMASK))) {
  1162. if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
  1163. b43legacyerr(dev->wl, "Fatal DMA error: "
  1164. "0x%08X, 0x%08X, 0x%08X, "
  1165. "0x%08X, 0x%08X, 0x%08X\n",
  1166. dma_reason[0], dma_reason[1],
  1167. dma_reason[2], dma_reason[3],
  1168. dma_reason[4], dma_reason[5]);
  1169. b43legacy_controller_restart(dev, "DMA error");
  1170. mmiowb();
  1171. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1172. return;
  1173. }
  1174. if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
  1175. b43legacyerr(dev->wl, "DMA error: "
  1176. "0x%08X, 0x%08X, 0x%08X, "
  1177. "0x%08X, 0x%08X, 0x%08X\n",
  1178. dma_reason[0], dma_reason[1],
  1179. dma_reason[2], dma_reason[3],
  1180. dma_reason[4], dma_reason[5]);
  1181. }
  1182. if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
  1183. handle_irq_ucode_debug(dev);
  1184. if (reason & B43legacy_IRQ_TBTT_INDI)
  1185. handle_irq_tbtt_indication(dev);
  1186. if (reason & B43legacy_IRQ_ATIM_END)
  1187. handle_irq_atim_end(dev);
  1188. if (reason & B43legacy_IRQ_BEACON)
  1189. handle_irq_beacon(dev);
  1190. if (reason & B43legacy_IRQ_PMQ)
  1191. handle_irq_pmq(dev);
  1192. if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
  1193. ;/*TODO*/
  1194. if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
  1195. handle_irq_noise(dev);
  1196. /* Check the DMA reason registers for received data. */
  1197. if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
  1198. if (b43legacy_using_pio(dev))
  1199. b43legacy_pio_rx(dev->pio.queue0);
  1200. else
  1201. b43legacy_dma_rx(dev->dma.rx_ring0);
  1202. }
  1203. B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
  1204. B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
  1205. if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
  1206. if (b43legacy_using_pio(dev))
  1207. b43legacy_pio_rx(dev->pio.queue3);
  1208. else
  1209. b43legacy_dma_rx(dev->dma.rx_ring3);
  1210. }
  1211. B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
  1212. B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
  1213. if (reason & B43legacy_IRQ_TX_OK)
  1214. handle_irq_transmit_status(dev);
  1215. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  1216. mmiowb();
  1217. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1218. }
  1219. static void pio_irq_workaround(struct b43legacy_wldev *dev,
  1220. u16 base, int queueidx)
  1221. {
  1222. u16 rxctl;
  1223. rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
  1224. if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
  1225. dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
  1226. else
  1227. dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
  1228. }
  1229. static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
  1230. {
  1231. if (b43legacy_using_pio(dev) &&
  1232. (dev->dev->id.revision < 3) &&
  1233. (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
  1234. /* Apply a PIO specific workaround to the dma_reasons */
  1235. pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
  1236. pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
  1237. pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
  1238. pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
  1239. }
  1240. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
  1241. b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
  1242. dev->dma_reason[0]);
  1243. b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
  1244. dev->dma_reason[1]);
  1245. b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
  1246. dev->dma_reason[2]);
  1247. b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
  1248. dev->dma_reason[3]);
  1249. b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
  1250. dev->dma_reason[4]);
  1251. b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
  1252. dev->dma_reason[5]);
  1253. }
  1254. /* Interrupt handler top-half */
  1255. static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
  1256. {
  1257. irqreturn_t ret = IRQ_NONE;
  1258. struct b43legacy_wldev *dev = dev_id;
  1259. u32 reason;
  1260. B43legacy_WARN_ON(!dev);
  1261. spin_lock(&dev->wl->irq_lock);
  1262. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
  1263. /* This can only happen on shared IRQ lines. */
  1264. goto out;
  1265. reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1266. if (reason == 0xffffffff) /* shared IRQ */
  1267. goto out;
  1268. ret = IRQ_HANDLED;
  1269. reason &= dev->irq_mask;
  1270. if (!reason)
  1271. goto out;
  1272. dev->dma_reason[0] = b43legacy_read32(dev,
  1273. B43legacy_MMIO_DMA0_REASON)
  1274. & 0x0001DC00;
  1275. dev->dma_reason[1] = b43legacy_read32(dev,
  1276. B43legacy_MMIO_DMA1_REASON)
  1277. & 0x0000DC00;
  1278. dev->dma_reason[2] = b43legacy_read32(dev,
  1279. B43legacy_MMIO_DMA2_REASON)
  1280. & 0x0000DC00;
  1281. dev->dma_reason[3] = b43legacy_read32(dev,
  1282. B43legacy_MMIO_DMA3_REASON)
  1283. & 0x0001DC00;
  1284. dev->dma_reason[4] = b43legacy_read32(dev,
  1285. B43legacy_MMIO_DMA4_REASON)
  1286. & 0x0000DC00;
  1287. dev->dma_reason[5] = b43legacy_read32(dev,
  1288. B43legacy_MMIO_DMA5_REASON)
  1289. & 0x0000DC00;
  1290. b43legacy_interrupt_ack(dev, reason);
  1291. /* Disable all IRQs. They are enabled again in the bottom half. */
  1292. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
  1293. /* Save the reason code and call our bottom half. */
  1294. dev->irq_reason = reason;
  1295. tasklet_schedule(&dev->isr_tasklet);
  1296. out:
  1297. mmiowb();
  1298. spin_unlock(&dev->wl->irq_lock);
  1299. return ret;
  1300. }
  1301. static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
  1302. {
  1303. release_firmware(dev->fw.ucode);
  1304. dev->fw.ucode = NULL;
  1305. release_firmware(dev->fw.pcm);
  1306. dev->fw.pcm = NULL;
  1307. release_firmware(dev->fw.initvals);
  1308. dev->fw.initvals = NULL;
  1309. release_firmware(dev->fw.initvals_band);
  1310. dev->fw.initvals_band = NULL;
  1311. }
  1312. static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
  1313. {
  1314. b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
  1315. "Drivers/b43#devicefirmware "
  1316. "and download the correct firmware (version 3).\n");
  1317. }
  1318. static int do_request_fw(struct b43legacy_wldev *dev,
  1319. const char *name,
  1320. const struct firmware **fw)
  1321. {
  1322. char path[sizeof(modparam_fwpostfix) + 32];
  1323. struct b43legacy_fw_header *hdr;
  1324. u32 size;
  1325. int err;
  1326. if (!name)
  1327. return 0;
  1328. snprintf(path, ARRAY_SIZE(path),
  1329. "b43legacy%s/%s.fw",
  1330. modparam_fwpostfix, name);
  1331. err = request_firmware(fw, path, dev->dev->dev);
  1332. if (err) {
  1333. b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
  1334. "or load failed.\n", path);
  1335. return err;
  1336. }
  1337. if ((*fw)->size < sizeof(struct b43legacy_fw_header))
  1338. goto err_format;
  1339. hdr = (struct b43legacy_fw_header *)((*fw)->data);
  1340. switch (hdr->type) {
  1341. case B43legacy_FW_TYPE_UCODE:
  1342. case B43legacy_FW_TYPE_PCM:
  1343. size = be32_to_cpu(hdr->size);
  1344. if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
  1345. goto err_format;
  1346. /* fallthrough */
  1347. case B43legacy_FW_TYPE_IV:
  1348. if (hdr->ver != 1)
  1349. goto err_format;
  1350. break;
  1351. default:
  1352. goto err_format;
  1353. }
  1354. return err;
  1355. err_format:
  1356. b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1357. return -EPROTO;
  1358. }
  1359. static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
  1360. {
  1361. struct b43legacy_firmware *fw = &dev->fw;
  1362. const u8 rev = dev->dev->id.revision;
  1363. const char *filename;
  1364. u32 tmshigh;
  1365. int err;
  1366. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1367. if (!fw->ucode) {
  1368. if (rev == 2)
  1369. filename = "ucode2";
  1370. else if (rev == 4)
  1371. filename = "ucode4";
  1372. else
  1373. filename = "ucode5";
  1374. err = do_request_fw(dev, filename, &fw->ucode);
  1375. if (err)
  1376. goto err_load;
  1377. }
  1378. if (!fw->pcm) {
  1379. if (rev < 5)
  1380. filename = "pcm4";
  1381. else
  1382. filename = "pcm5";
  1383. err = do_request_fw(dev, filename, &fw->pcm);
  1384. if (err)
  1385. goto err_load;
  1386. }
  1387. if (!fw->initvals) {
  1388. switch (dev->phy.type) {
  1389. case B43legacy_PHYTYPE_B:
  1390. case B43legacy_PHYTYPE_G:
  1391. if ((rev >= 5) && (rev <= 10))
  1392. filename = "b0g0initvals5";
  1393. else if (rev == 2 || rev == 4)
  1394. filename = "b0g0initvals2";
  1395. else
  1396. goto err_no_initvals;
  1397. break;
  1398. default:
  1399. goto err_no_initvals;
  1400. }
  1401. err = do_request_fw(dev, filename, &fw->initvals);
  1402. if (err)
  1403. goto err_load;
  1404. }
  1405. if (!fw->initvals_band) {
  1406. switch (dev->phy.type) {
  1407. case B43legacy_PHYTYPE_B:
  1408. case B43legacy_PHYTYPE_G:
  1409. if ((rev >= 5) && (rev <= 10))
  1410. filename = "b0g0bsinitvals5";
  1411. else if (rev >= 11)
  1412. filename = NULL;
  1413. else if (rev == 2 || rev == 4)
  1414. filename = NULL;
  1415. else
  1416. goto err_no_initvals;
  1417. break;
  1418. default:
  1419. goto err_no_initvals;
  1420. }
  1421. err = do_request_fw(dev, filename, &fw->initvals_band);
  1422. if (err)
  1423. goto err_load;
  1424. }
  1425. return 0;
  1426. err_load:
  1427. b43legacy_print_fw_helptext(dev->wl);
  1428. goto error;
  1429. err_no_initvals:
  1430. err = -ENODEV;
  1431. b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
  1432. "core rev %u\n", dev->phy.type, rev);
  1433. goto error;
  1434. error:
  1435. b43legacy_release_firmware(dev);
  1436. return err;
  1437. }
  1438. static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
  1439. {
  1440. struct wiphy *wiphy = dev->wl->hw->wiphy;
  1441. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1442. const __be32 *data;
  1443. unsigned int i;
  1444. unsigned int len;
  1445. u16 fwrev;
  1446. u16 fwpatch;
  1447. u16 fwdate;
  1448. u16 fwtime;
  1449. u32 tmp, macctl;
  1450. int err = 0;
  1451. /* Jump the microcode PSM to offset 0 */
  1452. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1453. B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
  1454. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1455. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1456. /* Zero out all microcode PSM registers and shared memory. */
  1457. for (i = 0; i < 64; i++)
  1458. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
  1459. for (i = 0; i < 4096; i += 2)
  1460. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
  1461. /* Upload Microcode. */
  1462. data = (__be32 *) (dev->fw.ucode->data + hdr_len);
  1463. len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
  1464. b43legacy_shm_control_word(dev,
  1465. B43legacy_SHM_UCODE |
  1466. B43legacy_SHM_AUTOINC_W,
  1467. 0x0000);
  1468. for (i = 0; i < len; i++) {
  1469. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1470. be32_to_cpu(data[i]));
  1471. udelay(10);
  1472. }
  1473. if (dev->fw.pcm) {
  1474. /* Upload PCM data. */
  1475. data = (__be32 *) (dev->fw.pcm->data + hdr_len);
  1476. len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
  1477. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
  1478. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
  1479. /* No need for autoinc bit in SHM_HW */
  1480. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
  1481. for (i = 0; i < len; i++) {
  1482. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1483. be32_to_cpu(data[i]));
  1484. udelay(10);
  1485. }
  1486. }
  1487. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1488. B43legacy_IRQ_ALL);
  1489. /* Start the microcode PSM */
  1490. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1491. macctl &= ~B43legacy_MACCTL_PSM_JMP0;
  1492. macctl |= B43legacy_MACCTL_PSM_RUN;
  1493. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1494. /* Wait for the microcode to load and respond */
  1495. i = 0;
  1496. while (1) {
  1497. tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1498. if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
  1499. break;
  1500. i++;
  1501. if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
  1502. b43legacyerr(dev->wl, "Microcode not responding\n");
  1503. b43legacy_print_fw_helptext(dev->wl);
  1504. err = -ENODEV;
  1505. goto error;
  1506. }
  1507. msleep_interruptible(50);
  1508. if (signal_pending(current)) {
  1509. err = -EINTR;
  1510. goto error;
  1511. }
  1512. }
  1513. /* dummy read follows */
  1514. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1515. /* Get and check the revisions. */
  1516. fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1517. B43legacy_SHM_SH_UCODEREV);
  1518. fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1519. B43legacy_SHM_SH_UCODEPATCH);
  1520. fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1521. B43legacy_SHM_SH_UCODEDATE);
  1522. fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1523. B43legacy_SHM_SH_UCODETIME);
  1524. if (fwrev > 0x128) {
  1525. b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
  1526. " Only firmware from binary drivers version 3.x"
  1527. " is supported. You must change your firmware"
  1528. " files.\n");
  1529. b43legacy_print_fw_helptext(dev->wl);
  1530. err = -EOPNOTSUPP;
  1531. goto error;
  1532. }
  1533. b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
  1534. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
  1535. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1536. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
  1537. fwtime & 0x1F);
  1538. dev->fw.rev = fwrev;
  1539. dev->fw.patch = fwpatch;
  1540. snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
  1541. dev->fw.rev, dev->fw.patch);
  1542. wiphy->hw_version = dev->dev->id.coreid;
  1543. return 0;
  1544. error:
  1545. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1546. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  1547. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1548. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1549. return err;
  1550. }
  1551. static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
  1552. const struct b43legacy_iv *ivals,
  1553. size_t count,
  1554. size_t array_size)
  1555. {
  1556. const struct b43legacy_iv *iv;
  1557. u16 offset;
  1558. size_t i;
  1559. bool bit32;
  1560. BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
  1561. iv = ivals;
  1562. for (i = 0; i < count; i++) {
  1563. if (array_size < sizeof(iv->offset_size))
  1564. goto err_format;
  1565. array_size -= sizeof(iv->offset_size);
  1566. offset = be16_to_cpu(iv->offset_size);
  1567. bit32 = !!(offset & B43legacy_IV_32BIT);
  1568. offset &= B43legacy_IV_OFFSET_MASK;
  1569. if (offset >= 0x1000)
  1570. goto err_format;
  1571. if (bit32) {
  1572. u32 value;
  1573. if (array_size < sizeof(iv->data.d32))
  1574. goto err_format;
  1575. array_size -= sizeof(iv->data.d32);
  1576. value = get_unaligned_be32(&iv->data.d32);
  1577. b43legacy_write32(dev, offset, value);
  1578. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1579. sizeof(__be16) +
  1580. sizeof(__be32));
  1581. } else {
  1582. u16 value;
  1583. if (array_size < sizeof(iv->data.d16))
  1584. goto err_format;
  1585. array_size -= sizeof(iv->data.d16);
  1586. value = be16_to_cpu(iv->data.d16);
  1587. b43legacy_write16(dev, offset, value);
  1588. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1589. sizeof(__be16) +
  1590. sizeof(__be16));
  1591. }
  1592. }
  1593. if (array_size)
  1594. goto err_format;
  1595. return 0;
  1596. err_format:
  1597. b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
  1598. b43legacy_print_fw_helptext(dev->wl);
  1599. return -EPROTO;
  1600. }
  1601. static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
  1602. {
  1603. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1604. const struct b43legacy_fw_header *hdr;
  1605. struct b43legacy_firmware *fw = &dev->fw;
  1606. const struct b43legacy_iv *ivals;
  1607. size_t count;
  1608. int err;
  1609. hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
  1610. ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
  1611. count = be32_to_cpu(hdr->size);
  1612. err = b43legacy_write_initvals(dev, ivals, count,
  1613. fw->initvals->size - hdr_len);
  1614. if (err)
  1615. goto out;
  1616. if (fw->initvals_band) {
  1617. hdr = (const struct b43legacy_fw_header *)
  1618. (fw->initvals_band->data);
  1619. ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
  1620. + hdr_len);
  1621. count = be32_to_cpu(hdr->size);
  1622. err = b43legacy_write_initvals(dev, ivals, count,
  1623. fw->initvals_band->size - hdr_len);
  1624. if (err)
  1625. goto out;
  1626. }
  1627. out:
  1628. return err;
  1629. }
  1630. /* Initialize the GPIOs
  1631. * http://bcm-specs.sipsolutions.net/GPIO
  1632. */
  1633. static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
  1634. {
  1635. struct ssb_bus *bus = dev->dev->bus;
  1636. struct ssb_device *gpiodev, *pcidev = NULL;
  1637. u32 mask;
  1638. u32 set;
  1639. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1640. b43legacy_read32(dev,
  1641. B43legacy_MMIO_MACCTL)
  1642. & 0xFFFF3FFF);
  1643. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1644. b43legacy_read16(dev,
  1645. B43legacy_MMIO_GPIO_MASK)
  1646. | 0x000F);
  1647. mask = 0x0000001F;
  1648. set = 0x0000000F;
  1649. if (dev->dev->bus->chip_id == 0x4301) {
  1650. mask |= 0x0060;
  1651. set |= 0x0060;
  1652. }
  1653. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
  1654. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1655. b43legacy_read16(dev,
  1656. B43legacy_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, B43legacy_GPIO_CONTROL,
  1670. (ssb_read32(gpiodev, B43legacy_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 b43legacy_gpio_cleanup(struct b43legacy_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, B43legacy_GPIO_CONTROL, 0);
  1686. }
  1687. /* http://bcm-specs.sipsolutions.net/EnableMac */
  1688. void b43legacy_mac_enable(struct b43legacy_wldev *dev)
  1689. {
  1690. dev->mac_suspended--;
  1691. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1692. B43legacy_WARN_ON(irqs_disabled());
  1693. if (dev->mac_suspended == 0) {
  1694. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1695. b43legacy_read32(dev,
  1696. B43legacy_MMIO_MACCTL)
  1697. | B43legacy_MACCTL_ENABLED);
  1698. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1699. B43legacy_IRQ_MAC_SUSPENDED);
  1700. /* the next two are dummy reads */
  1701. b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1702. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1703. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1704. /* Re-enable IRQs. */
  1705. spin_lock_irq(&dev->wl->irq_lock);
  1706. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
  1707. dev->irq_mask);
  1708. spin_unlock_irq(&dev->wl->irq_lock);
  1709. }
  1710. }
  1711. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1712. void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
  1713. {
  1714. int i;
  1715. u32 tmp;
  1716. might_sleep();
  1717. B43legacy_WARN_ON(irqs_disabled());
  1718. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1719. if (dev->mac_suspended == 0) {
  1720. /* Mask IRQs before suspending MAC. Otherwise
  1721. * the MAC stays busy and won't suspend. */
  1722. spin_lock_irq(&dev->wl->irq_lock);
  1723. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
  1724. spin_unlock_irq(&dev->wl->irq_lock);
  1725. b43legacy_synchronize_irq(dev);
  1726. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1727. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1728. b43legacy_read32(dev,
  1729. B43legacy_MMIO_MACCTL)
  1730. & ~B43legacy_MACCTL_ENABLED);
  1731. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1732. for (i = 40; i; i--) {
  1733. tmp = b43legacy_read32(dev,
  1734. B43legacy_MMIO_GEN_IRQ_REASON);
  1735. if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
  1736. goto out;
  1737. msleep(1);
  1738. }
  1739. b43legacyerr(dev->wl, "MAC suspend failed\n");
  1740. }
  1741. out:
  1742. dev->mac_suspended++;
  1743. }
  1744. static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
  1745. {
  1746. struct b43legacy_wl *wl = dev->wl;
  1747. u32 ctl;
  1748. u16 cfp_pretbtt;
  1749. ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1750. /* Reset status to STA infrastructure mode. */
  1751. ctl &= ~B43legacy_MACCTL_AP;
  1752. ctl &= ~B43legacy_MACCTL_KEEP_CTL;
  1753. ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
  1754. ctl &= ~B43legacy_MACCTL_KEEP_BAD;
  1755. ctl &= ~B43legacy_MACCTL_PROMISC;
  1756. ctl &= ~B43legacy_MACCTL_BEACPROMISC;
  1757. ctl |= B43legacy_MACCTL_INFRA;
  1758. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1759. ctl |= B43legacy_MACCTL_AP;
  1760. else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
  1761. ctl &= ~B43legacy_MACCTL_INFRA;
  1762. if (wl->filter_flags & FIF_CONTROL)
  1763. ctl |= B43legacy_MACCTL_KEEP_CTL;
  1764. if (wl->filter_flags & FIF_FCSFAIL)
  1765. ctl |= B43legacy_MACCTL_KEEP_BAD;
  1766. if (wl->filter_flags & FIF_PLCPFAIL)
  1767. ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
  1768. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1769. ctl |= B43legacy_MACCTL_PROMISC;
  1770. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1771. ctl |= B43legacy_MACCTL_BEACPROMISC;
  1772. /* Workaround: On old hardware the HW-MAC-address-filter
  1773. * doesn't work properly, so always run promisc in filter
  1774. * it in software. */
  1775. if (dev->dev->id.revision <= 4)
  1776. ctl |= B43legacy_MACCTL_PROMISC;
  1777. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
  1778. cfp_pretbtt = 2;
  1779. if ((ctl & B43legacy_MACCTL_INFRA) &&
  1780. !(ctl & B43legacy_MACCTL_AP)) {
  1781. if (dev->dev->bus->chip_id == 0x4306 &&
  1782. dev->dev->bus->chip_rev == 3)
  1783. cfp_pretbtt = 100;
  1784. else
  1785. cfp_pretbtt = 50;
  1786. }
  1787. b43legacy_write16(dev, 0x612, cfp_pretbtt);
  1788. }
  1789. static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
  1790. u16 rate,
  1791. int is_ofdm)
  1792. {
  1793. u16 offset;
  1794. if (is_ofdm) {
  1795. offset = 0x480;
  1796. offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1797. } else {
  1798. offset = 0x4C0;
  1799. offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1800. }
  1801. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
  1802. b43legacy_shm_read16(dev,
  1803. B43legacy_SHM_SHARED, offset));
  1804. }
  1805. static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
  1806. {
  1807. switch (dev->phy.type) {
  1808. case B43legacy_PHYTYPE_G:
  1809. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
  1810. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
  1811. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
  1812. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
  1813. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
  1814. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
  1815. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
  1816. /* fallthrough */
  1817. case B43legacy_PHYTYPE_B:
  1818. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
  1819. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
  1820. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
  1821. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
  1822. break;
  1823. default:
  1824. B43legacy_BUG_ON(1);
  1825. }
  1826. }
  1827. /* Set the TX-Antenna for management frames sent by firmware. */
  1828. static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
  1829. int antenna)
  1830. {
  1831. u16 ant = 0;
  1832. u16 tmp;
  1833. switch (antenna) {
  1834. case B43legacy_ANTENNA0:
  1835. ant |= B43legacy_TX4_PHY_ANT0;
  1836. break;
  1837. case B43legacy_ANTENNA1:
  1838. ant |= B43legacy_TX4_PHY_ANT1;
  1839. break;
  1840. case B43legacy_ANTENNA_AUTO:
  1841. ant |= B43legacy_TX4_PHY_ANTLAST;
  1842. break;
  1843. default:
  1844. B43legacy_BUG_ON(1);
  1845. }
  1846. /* FIXME We also need to set the other flags of the PHY control
  1847. * field somewhere. */
  1848. /* For Beacons */
  1849. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1850. B43legacy_SHM_SH_BEACPHYCTL);
  1851. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1852. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1853. B43legacy_SHM_SH_BEACPHYCTL, tmp);
  1854. /* For ACK/CTS */
  1855. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1856. B43legacy_SHM_SH_ACKCTSPHYCTL);
  1857. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1858. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1859. B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
  1860. /* For Probe Resposes */
  1861. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1862. B43legacy_SHM_SH_PRPHYCTL);
  1863. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1864. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1865. B43legacy_SHM_SH_PRPHYCTL, tmp);
  1866. }
  1867. /* This is the opposite of b43legacy_chip_init() */
  1868. static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
  1869. {
  1870. b43legacy_radio_turn_off(dev, 1);
  1871. b43legacy_gpio_cleanup(dev);
  1872. /* firmware is released later */
  1873. }
  1874. /* Initialize the chip
  1875. * http://bcm-specs.sipsolutions.net/ChipInit
  1876. */
  1877. static int b43legacy_chip_init(struct b43legacy_wldev *dev)
  1878. {
  1879. struct b43legacy_phy *phy = &dev->phy;
  1880. int err;
  1881. int tmp;
  1882. u32 value32, macctl;
  1883. u16 value16;
  1884. /* Initialize the MAC control */
  1885. macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
  1886. if (dev->phy.gmode)
  1887. macctl |= B43legacy_MACCTL_GMODE;
  1888. macctl |= B43legacy_MACCTL_INFRA;
  1889. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1890. err = b43legacy_request_firmware(dev);
  1891. if (err)
  1892. goto out;
  1893. err = b43legacy_upload_microcode(dev);
  1894. if (err)
  1895. goto out; /* firmware is released later */
  1896. err = b43legacy_gpio_init(dev);
  1897. if (err)
  1898. goto out; /* firmware is released later */
  1899. err = b43legacy_upload_initvals(dev);
  1900. if (err)
  1901. goto err_gpio_clean;
  1902. b43legacy_radio_turn_on(dev);
  1903. b43legacy_write16(dev, 0x03E6, 0x0000);
  1904. err = b43legacy_phy_init(dev);
  1905. if (err)
  1906. goto err_radio_off;
  1907. /* Select initial Interference Mitigation. */
  1908. tmp = phy->interfmode;
  1909. phy->interfmode = B43legacy_INTERFMODE_NONE;
  1910. b43legacy_radio_set_interference_mitigation(dev, tmp);
  1911. b43legacy_phy_set_antenna_diversity(dev);
  1912. b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
  1913. if (phy->type == B43legacy_PHYTYPE_B) {
  1914. value16 = b43legacy_read16(dev, 0x005E);
  1915. value16 |= 0x0004;
  1916. b43legacy_write16(dev, 0x005E, value16);
  1917. }
  1918. b43legacy_write32(dev, 0x0100, 0x01000000);
  1919. if (dev->dev->id.revision < 5)
  1920. b43legacy_write32(dev, 0x010C, 0x01000000);
  1921. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1922. value32 &= ~B43legacy_MACCTL_INFRA;
  1923. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1924. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1925. value32 |= B43legacy_MACCTL_INFRA;
  1926. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1927. if (b43legacy_using_pio(dev)) {
  1928. b43legacy_write32(dev, 0x0210, 0x00000100);
  1929. b43legacy_write32(dev, 0x0230, 0x00000100);
  1930. b43legacy_write32(dev, 0x0250, 0x00000100);
  1931. b43legacy_write32(dev, 0x0270, 0x00000100);
  1932. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
  1933. 0x0000);
  1934. }
  1935. /* Probe Response Timeout value */
  1936. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  1937. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
  1938. /* Initially set the wireless operation mode. */
  1939. b43legacy_adjust_opmode(dev);
  1940. if (dev->dev->id.revision < 3) {
  1941. b43legacy_write16(dev, 0x060E, 0x0000);
  1942. b43legacy_write16(dev, 0x0610, 0x8000);
  1943. b43legacy_write16(dev, 0x0604, 0x0000);
  1944. b43legacy_write16(dev, 0x0606, 0x0200);
  1945. } else {
  1946. b43legacy_write32(dev, 0x0188, 0x80000000);
  1947. b43legacy_write32(dev, 0x018C, 0x02000000);
  1948. }
  1949. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
  1950. b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  1951. b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  1952. b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  1953. b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  1954. b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  1955. b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  1956. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  1957. value32 |= 0x00100000;
  1958. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  1959. b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
  1960. dev->dev->bus->chipco.fast_pwrup_delay);
  1961. /* PHY TX errors counter. */
  1962. atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  1963. B43legacy_WARN_ON(err != 0);
  1964. b43legacydbg(dev->wl, "Chip initialized\n");
  1965. out:
  1966. return err;
  1967. err_radio_off:
  1968. b43legacy_radio_turn_off(dev, 1);
  1969. err_gpio_clean:
  1970. b43legacy_gpio_cleanup(dev);
  1971. goto out;
  1972. }
  1973. static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
  1974. {
  1975. struct b43legacy_phy *phy = &dev->phy;
  1976. if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
  1977. return;
  1978. b43legacy_mac_suspend(dev);
  1979. b43legacy_phy_lo_g_measure(dev);
  1980. b43legacy_mac_enable(dev);
  1981. }
  1982. static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
  1983. {
  1984. b43legacy_phy_lo_mark_all_unused(dev);
  1985. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
  1986. b43legacy_mac_suspend(dev);
  1987. b43legacy_calc_nrssi_slope(dev);
  1988. b43legacy_mac_enable(dev);
  1989. }
  1990. }
  1991. static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
  1992. {
  1993. /* Update device statistics. */
  1994. b43legacy_calculate_link_quality(dev);
  1995. }
  1996. static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
  1997. {
  1998. b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
  1999. atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  2000. wmb();
  2001. }
  2002. static void do_periodic_work(struct b43legacy_wldev *dev)
  2003. {
  2004. unsigned int state;
  2005. state = dev->periodic_state;
  2006. if (state % 8 == 0)
  2007. b43legacy_periodic_every120sec(dev);
  2008. if (state % 4 == 0)
  2009. b43legacy_periodic_every60sec(dev);
  2010. if (state % 2 == 0)
  2011. b43legacy_periodic_every30sec(dev);
  2012. b43legacy_periodic_every15sec(dev);
  2013. }
  2014. /* Periodic work locking policy:
  2015. * The whole periodic work handler is protected by
  2016. * wl->mutex. If another lock is needed somewhere in the
  2017. * pwork callchain, it's acquired in-place, where it's needed.
  2018. */
  2019. static void b43legacy_periodic_work_handler(struct work_struct *work)
  2020. {
  2021. struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
  2022. periodic_work.work);
  2023. struct b43legacy_wl *wl = dev->wl;
  2024. unsigned long delay;
  2025. mutex_lock(&wl->mutex);
  2026. if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
  2027. goto out;
  2028. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
  2029. goto out_requeue;
  2030. do_periodic_work(dev);
  2031. dev->periodic_state++;
  2032. out_requeue:
  2033. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
  2034. delay = msecs_to_jiffies(50);
  2035. else
  2036. delay = round_jiffies_relative(HZ * 15);
  2037. ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
  2038. out:
  2039. mutex_unlock(&wl->mutex);
  2040. }
  2041. static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
  2042. {
  2043. struct delayed_work *work = &dev->periodic_work;
  2044. dev->periodic_state = 0;
  2045. INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
  2046. ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
  2047. }
  2048. /* Validate access to the chip (SHM) */
  2049. static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
  2050. {
  2051. u32 value;
  2052. u32 shm_backup;
  2053. shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
  2054. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
  2055. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  2056. 0xAA5555AA)
  2057. goto error;
  2058. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
  2059. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  2060. 0x55AAAA55)
  2061. goto error;
  2062. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
  2063. value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2064. if ((value | B43legacy_MACCTL_GMODE) !=
  2065. (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
  2066. goto error;
  2067. value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  2068. if (value)
  2069. goto error;
  2070. return 0;
  2071. error:
  2072. b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
  2073. return -ENODEV;
  2074. }
  2075. static void b43legacy_security_init(struct b43legacy_wldev *dev)
  2076. {
  2077. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2078. B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2079. dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2080. 0x0056);
  2081. /* KTP is a word address, but we address SHM bytewise.
  2082. * So multiply by two.
  2083. */
  2084. dev->ktp *= 2;
  2085. if (dev->dev->id.revision >= 5)
  2086. /* Number of RCMTA address slots */
  2087. b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
  2088. dev->max_nr_keys - 8);
  2089. }
  2090. #ifdef CONFIG_B43LEGACY_HWRNG
  2091. static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
  2092. {
  2093. struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
  2094. unsigned long flags;
  2095. /* Don't take wl->mutex here, as it could deadlock with
  2096. * hwrng internal locking. It's not needed to take
  2097. * wl->mutex here, anyway. */
  2098. spin_lock_irqsave(&wl->irq_lock, flags);
  2099. *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
  2100. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2101. return (sizeof(u16));
  2102. }
  2103. #endif
  2104. static void b43legacy_rng_exit(struct b43legacy_wl *wl)
  2105. {
  2106. #ifdef CONFIG_B43LEGACY_HWRNG
  2107. if (wl->rng_initialized)
  2108. hwrng_unregister(&wl->rng);
  2109. #endif
  2110. }
  2111. static int b43legacy_rng_init(struct b43legacy_wl *wl)
  2112. {
  2113. int err = 0;
  2114. #ifdef CONFIG_B43LEGACY_HWRNG
  2115. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2116. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2117. wl->rng.name = wl->rng_name;
  2118. wl->rng.data_read = b43legacy_rng_read;
  2119. wl->rng.priv = (unsigned long)wl;
  2120. wl->rng_initialized = 1;
  2121. err = hwrng_register(&wl->rng);
  2122. if (err) {
  2123. wl->rng_initialized = 0;
  2124. b43legacyerr(wl, "Failed to register the random "
  2125. "number generator (%d)\n", err);
  2126. }
  2127. #endif
  2128. return err;
  2129. }
  2130. static int b43legacy_op_tx(struct ieee80211_hw *hw,
  2131. struct sk_buff *skb)
  2132. {
  2133. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2134. struct b43legacy_wldev *dev = wl->current_dev;
  2135. int err = -ENODEV;
  2136. unsigned long flags;
  2137. if (unlikely(!dev))
  2138. goto out;
  2139. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
  2140. goto out;
  2141. /* DMA-TX is done without a global lock. */
  2142. if (b43legacy_using_pio(dev)) {
  2143. spin_lock_irqsave(&wl->irq_lock, flags);
  2144. err = b43legacy_pio_tx(dev, skb);
  2145. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2146. } else
  2147. err = b43legacy_dma_tx(dev, skb);
  2148. out:
  2149. if (unlikely(err)) {
  2150. /* Drop the packet. */
  2151. dev_kfree_skb_any(skb);
  2152. }
  2153. return NETDEV_TX_OK;
  2154. }
  2155. static int b43legacy_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2156. const struct ieee80211_tx_queue_params *params)
  2157. {
  2158. return 0;
  2159. }
  2160. static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
  2161. struct ieee80211_low_level_stats *stats)
  2162. {
  2163. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2164. unsigned long flags;
  2165. spin_lock_irqsave(&wl->irq_lock, flags);
  2166. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2167. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2168. return 0;
  2169. }
  2170. static const char *phymode_to_string(unsigned int phymode)
  2171. {
  2172. switch (phymode) {
  2173. case B43legacy_PHYMODE_B:
  2174. return "B";
  2175. case B43legacy_PHYMODE_G:
  2176. return "G";
  2177. default:
  2178. B43legacy_BUG_ON(1);
  2179. }
  2180. return "";
  2181. }
  2182. static int find_wldev_for_phymode(struct b43legacy_wl *wl,
  2183. unsigned int phymode,
  2184. struct b43legacy_wldev **dev,
  2185. bool *gmode)
  2186. {
  2187. struct b43legacy_wldev *d;
  2188. list_for_each_entry(d, &wl->devlist, list) {
  2189. if (d->phy.possible_phymodes & phymode) {
  2190. /* Ok, this device supports the PHY-mode.
  2191. * Set the gmode bit. */
  2192. *gmode = 1;
  2193. *dev = d;
  2194. return 0;
  2195. }
  2196. }
  2197. return -ESRCH;
  2198. }
  2199. static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
  2200. {
  2201. struct ssb_device *sdev = dev->dev;
  2202. u32 tmslow;
  2203. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2204. tmslow &= ~B43legacy_TMSLOW_GMODE;
  2205. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2206. tmslow |= SSB_TMSLOW_FGC;
  2207. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2208. msleep(1);
  2209. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2210. tmslow &= ~SSB_TMSLOW_FGC;
  2211. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2212. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2213. msleep(1);
  2214. }
  2215. /* Expects wl->mutex locked */
  2216. static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
  2217. unsigned int new_mode)
  2218. {
  2219. struct b43legacy_wldev *uninitialized_var(up_dev);
  2220. struct b43legacy_wldev *down_dev;
  2221. int err;
  2222. bool gmode = 0;
  2223. int prev_status;
  2224. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2225. if (err) {
  2226. b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
  2227. phymode_to_string(new_mode));
  2228. return err;
  2229. }
  2230. if ((up_dev == wl->current_dev) &&
  2231. (!!wl->current_dev->phy.gmode == !!gmode))
  2232. /* This device is already running. */
  2233. return 0;
  2234. b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2235. phymode_to_string(new_mode));
  2236. down_dev = wl->current_dev;
  2237. prev_status = b43legacy_status(down_dev);
  2238. /* Shutdown the currently running core. */
  2239. if (prev_status >= B43legacy_STAT_STARTED)
  2240. b43legacy_wireless_core_stop(down_dev);
  2241. if (prev_status >= B43legacy_STAT_INITIALIZED)
  2242. b43legacy_wireless_core_exit(down_dev);
  2243. if (down_dev != up_dev)
  2244. /* We switch to a different core, so we put PHY into
  2245. * RESET on the old core. */
  2246. b43legacy_put_phy_into_reset(down_dev);
  2247. /* Now start the new core. */
  2248. up_dev->phy.gmode = gmode;
  2249. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  2250. err = b43legacy_wireless_core_init(up_dev);
  2251. if (err) {
  2252. b43legacyerr(wl, "Fatal: Could not initialize device"
  2253. " for newly selected %s-PHY mode\n",
  2254. phymode_to_string(new_mode));
  2255. goto init_failure;
  2256. }
  2257. }
  2258. if (prev_status >= B43legacy_STAT_STARTED) {
  2259. err = b43legacy_wireless_core_start(up_dev);
  2260. if (err) {
  2261. b43legacyerr(wl, "Fatal: Coult not start device for "
  2262. "newly selected %s-PHY mode\n",
  2263. phymode_to_string(new_mode));
  2264. b43legacy_wireless_core_exit(up_dev);
  2265. goto init_failure;
  2266. }
  2267. }
  2268. B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
  2269. b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
  2270. wl->current_dev = up_dev;
  2271. return 0;
  2272. init_failure:
  2273. /* Whoops, failed to init the new core. No core is operating now. */
  2274. wl->current_dev = NULL;
  2275. return err;
  2276. }
  2277. /* Write the short and long frame retry limit values. */
  2278. static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
  2279. unsigned int short_retry,
  2280. unsigned int long_retry)
  2281. {
  2282. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2283. * the chip-internal counter. */
  2284. short_retry = min(short_retry, (unsigned int)0xF);
  2285. long_retry = min(long_retry, (unsigned int)0xF);
  2286. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
  2287. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
  2288. }
  2289. static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
  2290. u32 changed)
  2291. {
  2292. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2293. struct b43legacy_wldev *dev;
  2294. struct b43legacy_phy *phy;
  2295. struct ieee80211_conf *conf = &hw->conf;
  2296. unsigned long flags;
  2297. unsigned int new_phymode = 0xFFFF;
  2298. int antenna_tx;
  2299. int antenna_rx;
  2300. int err = 0;
  2301. antenna_tx = B43legacy_ANTENNA_DEFAULT;
  2302. antenna_rx = B43legacy_ANTENNA_DEFAULT;
  2303. mutex_lock(&wl->mutex);
  2304. dev = wl->current_dev;
  2305. phy = &dev->phy;
  2306. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  2307. b43legacy_set_retry_limits(dev,
  2308. conf->short_frame_max_tx_count,
  2309. conf->long_frame_max_tx_count);
  2310. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  2311. if (!changed)
  2312. goto out_unlock_mutex;
  2313. /* Switch the PHY mode (if necessary). */
  2314. switch (conf->channel->band) {
  2315. case IEEE80211_BAND_2GHZ:
  2316. if (phy->type == B43legacy_PHYTYPE_B)
  2317. new_phymode = B43legacy_PHYMODE_B;
  2318. else
  2319. new_phymode = B43legacy_PHYMODE_G;
  2320. break;
  2321. default:
  2322. B43legacy_WARN_ON(1);
  2323. }
  2324. err = b43legacy_switch_phymode(wl, new_phymode);
  2325. if (err)
  2326. goto out_unlock_mutex;
  2327. /* Disable IRQs while reconfiguring the device.
  2328. * This makes it possible to drop the spinlock throughout
  2329. * the reconfiguration process. */
  2330. spin_lock_irqsave(&wl->irq_lock, flags);
  2331. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2332. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2333. goto out_unlock_mutex;
  2334. }
  2335. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
  2336. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2337. b43legacy_synchronize_irq(dev);
  2338. /* Switch to the requested channel.
  2339. * The firmware takes care of races with the TX handler. */
  2340. if (conf->channel->hw_value != phy->channel)
  2341. b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
  2342. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
  2343. /* Adjust the desired TX power level. */
  2344. if (conf->power_level != 0) {
  2345. if (conf->power_level != phy->power_level) {
  2346. phy->power_level = conf->power_level;
  2347. b43legacy_phy_xmitpower(dev);
  2348. }
  2349. }
  2350. /* Antennas for RX and management frame TX. */
  2351. b43legacy_mgmtframe_txantenna(dev, antenna_tx);
  2352. if (wl->radio_enabled != phy->radio_on) {
  2353. if (wl->radio_enabled) {
  2354. b43legacy_radio_turn_on(dev);
  2355. b43legacyinfo(dev->wl, "Radio turned on by software\n");
  2356. if (!dev->radio_hw_enable)
  2357. b43legacyinfo(dev->wl, "The hardware RF-kill"
  2358. " button still turns the radio"
  2359. " physically off. Press the"
  2360. " button to turn it on.\n");
  2361. } else {
  2362. b43legacy_radio_turn_off(dev, 0);
  2363. b43legacyinfo(dev->wl, "Radio turned off by"
  2364. " software\n");
  2365. }
  2366. }
  2367. spin_lock_irqsave(&wl->irq_lock, flags);
  2368. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  2369. mmiowb();
  2370. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2371. out_unlock_mutex:
  2372. mutex_unlock(&wl->mutex);
  2373. return err;
  2374. }
  2375. static void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
  2376. {
  2377. struct ieee80211_supported_band *sband =
  2378. dev->wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  2379. struct ieee80211_rate *rate;
  2380. int i;
  2381. u16 basic, direct, offset, basic_offset, rateptr;
  2382. for (i = 0; i < sband->n_bitrates; i++) {
  2383. rate = &sband->bitrates[i];
  2384. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2385. direct = B43legacy_SHM_SH_CCKDIRECT;
  2386. basic = B43legacy_SHM_SH_CCKBASIC;
  2387. offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2388. offset &= 0xF;
  2389. } else {
  2390. direct = B43legacy_SHM_SH_OFDMDIRECT;
  2391. basic = B43legacy_SHM_SH_OFDMBASIC;
  2392. offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2393. offset &= 0xF;
  2394. }
  2395. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  2396. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2397. basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2398. basic_offset &= 0xF;
  2399. } else {
  2400. basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2401. basic_offset &= 0xF;
  2402. }
  2403. /*
  2404. * Get the pointer that we need to point to
  2405. * from the direct map
  2406. */
  2407. rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2408. direct + 2 * basic_offset);
  2409. /* and write it to the basic map */
  2410. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2411. basic + 2 * offset, rateptr);
  2412. }
  2413. }
  2414. static void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
  2415. struct ieee80211_vif *vif,
  2416. struct ieee80211_bss_conf *conf,
  2417. u32 changed)
  2418. {
  2419. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2420. struct b43legacy_wldev *dev;
  2421. struct b43legacy_phy *phy;
  2422. unsigned long flags;
  2423. mutex_lock(&wl->mutex);
  2424. B43legacy_WARN_ON(wl->vif != vif);
  2425. dev = wl->current_dev;
  2426. phy = &dev->phy;
  2427. /* Disable IRQs while reconfiguring the device.
  2428. * This makes it possible to drop the spinlock throughout
  2429. * the reconfiguration process. */
  2430. spin_lock_irqsave(&wl->irq_lock, flags);
  2431. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2432. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2433. goto out_unlock_mutex;
  2434. }
  2435. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
  2436. if (changed & BSS_CHANGED_BSSID) {
  2437. b43legacy_synchronize_irq(dev);
  2438. if (conf->bssid)
  2439. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2440. else
  2441. memset(wl->bssid, 0, ETH_ALEN);
  2442. }
  2443. if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
  2444. if (changed & BSS_CHANGED_BEACON &&
  2445. (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
  2446. b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
  2447. b43legacy_update_templates(wl);
  2448. if (changed & BSS_CHANGED_BSSID)
  2449. b43legacy_write_mac_bssid_templates(dev);
  2450. }
  2451. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2452. b43legacy_mac_suspend(dev);
  2453. if (changed & BSS_CHANGED_BEACON_INT &&
  2454. (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
  2455. b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
  2456. b43legacy_set_beacon_int(dev, conf->beacon_int);
  2457. if (changed & BSS_CHANGED_BASIC_RATES)
  2458. b43legacy_update_basic_rates(dev, conf->basic_rates);
  2459. if (changed & BSS_CHANGED_ERP_SLOT) {
  2460. if (conf->use_short_slot)
  2461. b43legacy_short_slot_timing_enable(dev);
  2462. else
  2463. b43legacy_short_slot_timing_disable(dev);
  2464. }
  2465. b43legacy_mac_enable(dev);
  2466. spin_lock_irqsave(&wl->irq_lock, flags);
  2467. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  2468. /* XXX: why? */
  2469. mmiowb();
  2470. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2471. out_unlock_mutex:
  2472. mutex_unlock(&wl->mutex);
  2473. }
  2474. static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
  2475. unsigned int changed,
  2476. unsigned int *fflags,u64 multicast)
  2477. {
  2478. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2479. struct b43legacy_wldev *dev = wl->current_dev;
  2480. unsigned long flags;
  2481. if (!dev) {
  2482. *fflags = 0;
  2483. return;
  2484. }
  2485. spin_lock_irqsave(&wl->irq_lock, flags);
  2486. *fflags &= FIF_PROMISC_IN_BSS |
  2487. FIF_ALLMULTI |
  2488. FIF_FCSFAIL |
  2489. FIF_PLCPFAIL |
  2490. FIF_CONTROL |
  2491. FIF_OTHER_BSS |
  2492. FIF_BCN_PRBRESP_PROMISC;
  2493. changed &= FIF_PROMISC_IN_BSS |
  2494. FIF_ALLMULTI |
  2495. FIF_FCSFAIL |
  2496. FIF_PLCPFAIL |
  2497. FIF_CONTROL |
  2498. FIF_OTHER_BSS |
  2499. FIF_BCN_PRBRESP_PROMISC;
  2500. wl->filter_flags = *fflags;
  2501. if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
  2502. b43legacy_adjust_opmode(dev);
  2503. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2504. }
  2505. /* Locking: wl->mutex */
  2506. static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
  2507. {
  2508. struct b43legacy_wl *wl = dev->wl;
  2509. unsigned long flags;
  2510. if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
  2511. return;
  2512. /* Disable and sync interrupts. We must do this before than
  2513. * setting the status to INITIALIZED, as the interrupt handler
  2514. * won't care about IRQs then. */
  2515. spin_lock_irqsave(&wl->irq_lock, flags);
  2516. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
  2517. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
  2518. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2519. b43legacy_synchronize_irq(dev);
  2520. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2521. mutex_unlock(&wl->mutex);
  2522. /* Must unlock as it would otherwise deadlock. No races here.
  2523. * Cancel the possibly running self-rearming periodic work. */
  2524. cancel_delayed_work_sync(&dev->periodic_work);
  2525. mutex_lock(&wl->mutex);
  2526. ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
  2527. b43legacy_mac_suspend(dev);
  2528. free_irq(dev->dev->irq, dev);
  2529. b43legacydbg(wl, "Wireless interface stopped\n");
  2530. }
  2531. /* Locking: wl->mutex */
  2532. static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
  2533. {
  2534. int err;
  2535. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
  2536. drain_txstatus_queue(dev);
  2537. err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
  2538. IRQF_SHARED, KBUILD_MODNAME, dev);
  2539. if (err) {
  2540. b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
  2541. dev->dev->irq);
  2542. goto out;
  2543. }
  2544. /* We are ready to run. */
  2545. ieee80211_wake_queues(dev->wl->hw);
  2546. b43legacy_set_status(dev, B43legacy_STAT_STARTED);
  2547. /* Start data flow (TX/RX) */
  2548. b43legacy_mac_enable(dev);
  2549. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
  2550. /* Start maintenance work */
  2551. b43legacy_periodic_tasks_setup(dev);
  2552. b43legacydbg(dev->wl, "Wireless interface started\n");
  2553. out:
  2554. return err;
  2555. }
  2556. /* Get PHY and RADIO versioning numbers */
  2557. static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
  2558. {
  2559. struct b43legacy_phy *phy = &dev->phy;
  2560. u32 tmp;
  2561. u8 analog_type;
  2562. u8 phy_type;
  2563. u8 phy_rev;
  2564. u16 radio_manuf;
  2565. u16 radio_ver;
  2566. u16 radio_rev;
  2567. int unsupported = 0;
  2568. /* Get PHY versioning */
  2569. tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
  2570. analog_type = (tmp & B43legacy_PHYVER_ANALOG)
  2571. >> B43legacy_PHYVER_ANALOG_SHIFT;
  2572. phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
  2573. phy_rev = (tmp & B43legacy_PHYVER_VERSION);
  2574. switch (phy_type) {
  2575. case B43legacy_PHYTYPE_B:
  2576. if (phy_rev != 2 && phy_rev != 4
  2577. && phy_rev != 6 && phy_rev != 7)
  2578. unsupported = 1;
  2579. break;
  2580. case B43legacy_PHYTYPE_G:
  2581. if (phy_rev > 8)
  2582. unsupported = 1;
  2583. break;
  2584. default:
  2585. unsupported = 1;
  2586. };
  2587. if (unsupported) {
  2588. b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
  2589. "(Analog %u, Type %u, Revision %u)\n",
  2590. analog_type, phy_type, phy_rev);
  2591. return -EOPNOTSUPP;
  2592. }
  2593. b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2594. analog_type, phy_type, phy_rev);
  2595. /* Get RADIO versioning */
  2596. if (dev->dev->bus->chip_id == 0x4317) {
  2597. if (dev->dev->bus->chip_rev == 0)
  2598. tmp = 0x3205017F;
  2599. else if (dev->dev->bus->chip_rev == 1)
  2600. tmp = 0x4205017F;
  2601. else
  2602. tmp = 0x5205017F;
  2603. } else {
  2604. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2605. B43legacy_RADIOCTL_ID);
  2606. tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
  2607. tmp <<= 16;
  2608. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2609. B43legacy_RADIOCTL_ID);
  2610. tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
  2611. }
  2612. radio_manuf = (tmp & 0x00000FFF);
  2613. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2614. radio_rev = (tmp & 0xF0000000) >> 28;
  2615. switch (phy_type) {
  2616. case B43legacy_PHYTYPE_B:
  2617. if ((radio_ver & 0xFFF0) != 0x2050)
  2618. unsupported = 1;
  2619. break;
  2620. case B43legacy_PHYTYPE_G:
  2621. if (radio_ver != 0x2050)
  2622. unsupported = 1;
  2623. break;
  2624. default:
  2625. B43legacy_BUG_ON(1);
  2626. }
  2627. if (unsupported) {
  2628. b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
  2629. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2630. radio_manuf, radio_ver, radio_rev);
  2631. return -EOPNOTSUPP;
  2632. }
  2633. b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
  2634. " Revision %u\n", radio_manuf, radio_ver, radio_rev);
  2635. phy->radio_manuf = radio_manuf;
  2636. phy->radio_ver = radio_ver;
  2637. phy->radio_rev = radio_rev;
  2638. phy->analog = analog_type;
  2639. phy->type = phy_type;
  2640. phy->rev = phy_rev;
  2641. return 0;
  2642. }
  2643. static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
  2644. struct b43legacy_phy *phy)
  2645. {
  2646. struct b43legacy_lopair *lo;
  2647. int i;
  2648. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2649. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2650. /* Assume the radio is enabled. If it's not enabled, the state will
  2651. * immediately get fixed on the first periodic work run. */
  2652. dev->radio_hw_enable = 1;
  2653. phy->savedpctlreg = 0xFFFF;
  2654. phy->aci_enable = 0;
  2655. phy->aci_wlan_automatic = 0;
  2656. phy->aci_hw_rssi = 0;
  2657. lo = phy->_lo_pairs;
  2658. if (lo)
  2659. memset(lo, 0, sizeof(struct b43legacy_lopair) *
  2660. B43legacy_LO_COUNT);
  2661. phy->max_lb_gain = 0;
  2662. phy->trsw_rx_gain = 0;
  2663. /* Set default attenuation values. */
  2664. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2665. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2666. phy->txctl1 = b43legacy_default_txctl1(dev);
  2667. phy->txpwr_offset = 0;
  2668. /* NRSSI */
  2669. phy->nrssislope = 0;
  2670. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2671. phy->nrssi[i] = -1000;
  2672. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2673. phy->nrssi_lt[i] = i;
  2674. phy->lofcal = 0xFFFF;
  2675. phy->initval = 0xFFFF;
  2676. phy->interfmode = B43legacy_INTERFMODE_NONE;
  2677. phy->channel = 0xFF;
  2678. }
  2679. static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
  2680. {
  2681. /* Flags */
  2682. dev->dfq_valid = 0;
  2683. /* Stats */
  2684. memset(&dev->stats, 0, sizeof(dev->stats));
  2685. setup_struct_phy_for_init(dev, &dev->phy);
  2686. /* IRQ related flags */
  2687. dev->irq_reason = 0;
  2688. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2689. dev->irq_mask = B43legacy_IRQ_MASKTEMPLATE;
  2690. dev->mac_suspended = 1;
  2691. /* Noise calculation context */
  2692. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2693. }
  2694. static void b43legacy_imcfglo_timeouts_workaround(struct b43legacy_wldev *dev)
  2695. {
  2696. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2697. struct ssb_bus *bus = dev->dev->bus;
  2698. u32 tmp;
  2699. if (bus->pcicore.dev &&
  2700. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2701. bus->pcicore.dev->id.revision <= 5) {
  2702. /* IMCFGLO timeouts workaround. */
  2703. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2704. switch (bus->bustype) {
  2705. case SSB_BUSTYPE_PCI:
  2706. case SSB_BUSTYPE_PCMCIA:
  2707. tmp &= ~SSB_IMCFGLO_REQTO;
  2708. tmp &= ~SSB_IMCFGLO_SERTO;
  2709. tmp |= 0x32;
  2710. break;
  2711. case SSB_BUSTYPE_SSB:
  2712. tmp &= ~SSB_IMCFGLO_REQTO;
  2713. tmp &= ~SSB_IMCFGLO_SERTO;
  2714. tmp |= 0x53;
  2715. break;
  2716. default:
  2717. break;
  2718. }
  2719. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2720. }
  2721. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2722. }
  2723. static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
  2724. bool idle) {
  2725. u16 pu_delay = 1050;
  2726. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  2727. pu_delay = 500;
  2728. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  2729. pu_delay = max(pu_delay, (u16)2400);
  2730. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2731. B43legacy_SHM_SH_SPUWKUP, pu_delay);
  2732. }
  2733. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  2734. static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
  2735. {
  2736. u16 pretbtt;
  2737. /* The time value is in microseconds. */
  2738. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  2739. pretbtt = 2;
  2740. else
  2741. pretbtt = 250;
  2742. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2743. B43legacy_SHM_SH_PRETBTT, pretbtt);
  2744. b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
  2745. }
  2746. /* Shutdown a wireless core */
  2747. /* Locking: wl->mutex */
  2748. static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
  2749. {
  2750. struct b43legacy_phy *phy = &dev->phy;
  2751. u32 macctl;
  2752. B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
  2753. if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
  2754. return;
  2755. b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
  2756. /* Stop the microcode PSM. */
  2757. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2758. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  2759. macctl |= B43legacy_MACCTL_PSM_JMP0;
  2760. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  2761. b43legacy_leds_exit(dev);
  2762. b43legacy_rng_exit(dev->wl);
  2763. b43legacy_pio_free(dev);
  2764. b43legacy_dma_free(dev);
  2765. b43legacy_chip_exit(dev);
  2766. b43legacy_radio_turn_off(dev, 1);
  2767. b43legacy_switch_analog(dev, 0);
  2768. if (phy->dyn_tssi_tbl)
  2769. kfree(phy->tssi2dbm);
  2770. kfree(phy->lo_control);
  2771. phy->lo_control = NULL;
  2772. if (dev->wl->current_beacon) {
  2773. dev_kfree_skb_any(dev->wl->current_beacon);
  2774. dev->wl->current_beacon = NULL;
  2775. }
  2776. ssb_device_disable(dev->dev, 0);
  2777. ssb_bus_may_powerdown(dev->dev->bus);
  2778. }
  2779. static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
  2780. {
  2781. struct b43legacy_phy *phy = &dev->phy;
  2782. int i;
  2783. /* Set default attenuation values. */
  2784. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2785. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2786. phy->txctl1 = b43legacy_default_txctl1(dev);
  2787. phy->txctl2 = 0xFFFF;
  2788. phy->txpwr_offset = 0;
  2789. /* NRSSI */
  2790. phy->nrssislope = 0;
  2791. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2792. phy->nrssi[i] = -1000;
  2793. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2794. phy->nrssi_lt[i] = i;
  2795. phy->lofcal = 0xFFFF;
  2796. phy->initval = 0xFFFF;
  2797. phy->aci_enable = 0;
  2798. phy->aci_wlan_automatic = 0;
  2799. phy->aci_hw_rssi = 0;
  2800. phy->antenna_diversity = 0xFFFF;
  2801. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2802. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2803. /* Flags */
  2804. phy->calibrated = 0;
  2805. if (phy->_lo_pairs)
  2806. memset(phy->_lo_pairs, 0,
  2807. sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
  2808. memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
  2809. }
  2810. /* Initialize a wireless core */
  2811. static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
  2812. {
  2813. struct b43legacy_wl *wl = dev->wl;
  2814. struct ssb_bus *bus = dev->dev->bus;
  2815. struct b43legacy_phy *phy = &dev->phy;
  2816. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2817. int err;
  2818. u32 hf;
  2819. u32 tmp;
  2820. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2821. err = ssb_bus_powerup(bus, 0);
  2822. if (err)
  2823. goto out;
  2824. if (!ssb_device_is_enabled(dev->dev)) {
  2825. tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
  2826. b43legacy_wireless_core_reset(dev, tmp);
  2827. }
  2828. if ((phy->type == B43legacy_PHYTYPE_B) ||
  2829. (phy->type == B43legacy_PHYTYPE_G)) {
  2830. phy->_lo_pairs = kzalloc(sizeof(struct b43legacy_lopair)
  2831. * B43legacy_LO_COUNT,
  2832. GFP_KERNEL);
  2833. if (!phy->_lo_pairs)
  2834. return -ENOMEM;
  2835. }
  2836. setup_struct_wldev_for_init(dev);
  2837. err = b43legacy_phy_init_tssi2dbm_table(dev);
  2838. if (err)
  2839. goto err_kfree_lo_control;
  2840. /* Enable IRQ routing to this device. */
  2841. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  2842. b43legacy_imcfglo_timeouts_workaround(dev);
  2843. prepare_phy_data_for_init(dev);
  2844. b43legacy_phy_calibrate(dev);
  2845. err = b43legacy_chip_init(dev);
  2846. if (err)
  2847. goto err_kfree_tssitbl;
  2848. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2849. B43legacy_SHM_SH_WLCOREREV,
  2850. dev->dev->id.revision);
  2851. hf = b43legacy_hf_read(dev);
  2852. if (phy->type == B43legacy_PHYTYPE_G) {
  2853. hf |= B43legacy_HF_SYMW;
  2854. if (phy->rev == 1)
  2855. hf |= B43legacy_HF_GDCW;
  2856. if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
  2857. hf |= B43legacy_HF_OFDMPABOOST;
  2858. } else if (phy->type == B43legacy_PHYTYPE_B) {
  2859. hf |= B43legacy_HF_SYMW;
  2860. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  2861. hf &= ~B43legacy_HF_GDCW;
  2862. }
  2863. b43legacy_hf_write(dev, hf);
  2864. b43legacy_set_retry_limits(dev,
  2865. B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
  2866. B43legacy_DEFAULT_LONG_RETRY_LIMIT);
  2867. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2868. 0x0044, 3);
  2869. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2870. 0x0046, 2);
  2871. /* Disable sending probe responses from firmware.
  2872. * Setting the MaxTime to one usec will always trigger
  2873. * a timeout, so we never send any probe resp.
  2874. * A timeout of zero is infinite. */
  2875. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2876. B43legacy_SHM_SH_PRMAXTIME, 1);
  2877. b43legacy_rate_memory_init(dev);
  2878. /* Minimum Contention Window */
  2879. if (phy->type == B43legacy_PHYTYPE_B)
  2880. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2881. 0x0003, 31);
  2882. else
  2883. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2884. 0x0003, 15);
  2885. /* Maximum Contention Window */
  2886. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2887. 0x0004, 1023);
  2888. do {
  2889. if (b43legacy_using_pio(dev))
  2890. err = b43legacy_pio_init(dev);
  2891. else {
  2892. err = b43legacy_dma_init(dev);
  2893. if (!err)
  2894. b43legacy_qos_init(dev);
  2895. }
  2896. } while (err == -EAGAIN);
  2897. if (err)
  2898. goto err_chip_exit;
  2899. b43legacy_set_synth_pu_delay(dev, 1);
  2900. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  2901. b43legacy_upload_card_macaddress(dev);
  2902. b43legacy_security_init(dev);
  2903. b43legacy_rng_init(wl);
  2904. ieee80211_wake_queues(dev->wl->hw);
  2905. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2906. b43legacy_leds_init(dev);
  2907. out:
  2908. return err;
  2909. err_chip_exit:
  2910. b43legacy_chip_exit(dev);
  2911. err_kfree_tssitbl:
  2912. if (phy->dyn_tssi_tbl)
  2913. kfree(phy->tssi2dbm);
  2914. err_kfree_lo_control:
  2915. kfree(phy->lo_control);
  2916. phy->lo_control = NULL;
  2917. ssb_bus_may_powerdown(bus);
  2918. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2919. return err;
  2920. }
  2921. static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
  2922. struct ieee80211_vif *vif)
  2923. {
  2924. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2925. struct b43legacy_wldev *dev;
  2926. unsigned long flags;
  2927. int err = -EOPNOTSUPP;
  2928. /* TODO: allow WDS/AP devices to coexist */
  2929. if (vif->type != NL80211_IFTYPE_AP &&
  2930. vif->type != NL80211_IFTYPE_STATION &&
  2931. vif->type != NL80211_IFTYPE_WDS &&
  2932. vif->type != NL80211_IFTYPE_ADHOC)
  2933. return -EOPNOTSUPP;
  2934. mutex_lock(&wl->mutex);
  2935. if (wl->operating)
  2936. goto out_mutex_unlock;
  2937. b43legacydbg(wl, "Adding Interface type %d\n", vif->type);
  2938. dev = wl->current_dev;
  2939. wl->operating = 1;
  2940. wl->vif = vif;
  2941. wl->if_type = vif->type;
  2942. memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
  2943. spin_lock_irqsave(&wl->irq_lock, flags);
  2944. b43legacy_adjust_opmode(dev);
  2945. b43legacy_set_pretbtt(dev);
  2946. b43legacy_set_synth_pu_delay(dev, 0);
  2947. b43legacy_upload_card_macaddress(dev);
  2948. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2949. err = 0;
  2950. out_mutex_unlock:
  2951. mutex_unlock(&wl->mutex);
  2952. return err;
  2953. }
  2954. static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
  2955. struct ieee80211_vif *vif)
  2956. {
  2957. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2958. struct b43legacy_wldev *dev = wl->current_dev;
  2959. unsigned long flags;
  2960. b43legacydbg(wl, "Removing Interface type %d\n", vif->type);
  2961. mutex_lock(&wl->mutex);
  2962. B43legacy_WARN_ON(!wl->operating);
  2963. B43legacy_WARN_ON(wl->vif != vif);
  2964. wl->vif = NULL;
  2965. wl->operating = 0;
  2966. spin_lock_irqsave(&wl->irq_lock, flags);
  2967. b43legacy_adjust_opmode(dev);
  2968. memset(wl->mac_addr, 0, ETH_ALEN);
  2969. b43legacy_upload_card_macaddress(dev);
  2970. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2971. mutex_unlock(&wl->mutex);
  2972. }
  2973. static int b43legacy_op_start(struct ieee80211_hw *hw)
  2974. {
  2975. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2976. struct b43legacy_wldev *dev = wl->current_dev;
  2977. int did_init = 0;
  2978. int err = 0;
  2979. /* Kill all old instance specific information to make sure
  2980. * the card won't use it in the short timeframe between start
  2981. * and mac80211 reconfiguring it. */
  2982. memset(wl->bssid, 0, ETH_ALEN);
  2983. memset(wl->mac_addr, 0, ETH_ALEN);
  2984. wl->filter_flags = 0;
  2985. wl->beacon0_uploaded = 0;
  2986. wl->beacon1_uploaded = 0;
  2987. wl->beacon_templates_virgin = 1;
  2988. wl->radio_enabled = 1;
  2989. mutex_lock(&wl->mutex);
  2990. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
  2991. err = b43legacy_wireless_core_init(dev);
  2992. if (err)
  2993. goto out_mutex_unlock;
  2994. did_init = 1;
  2995. }
  2996. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2997. err = b43legacy_wireless_core_start(dev);
  2998. if (err) {
  2999. if (did_init)
  3000. b43legacy_wireless_core_exit(dev);
  3001. goto out_mutex_unlock;
  3002. }
  3003. }
  3004. wiphy_rfkill_start_polling(hw->wiphy);
  3005. out_mutex_unlock:
  3006. mutex_unlock(&wl->mutex);
  3007. return err;
  3008. }
  3009. static void b43legacy_op_stop(struct ieee80211_hw *hw)
  3010. {
  3011. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3012. struct b43legacy_wldev *dev = wl->current_dev;
  3013. cancel_work_sync(&(wl->beacon_update_trigger));
  3014. mutex_lock(&wl->mutex);
  3015. if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
  3016. b43legacy_wireless_core_stop(dev);
  3017. b43legacy_wireless_core_exit(dev);
  3018. wl->radio_enabled = 0;
  3019. mutex_unlock(&wl->mutex);
  3020. }
  3021. static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
  3022. struct ieee80211_sta *sta, bool set)
  3023. {
  3024. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3025. unsigned long flags;
  3026. spin_lock_irqsave(&wl->irq_lock, flags);
  3027. b43legacy_update_templates(wl);
  3028. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3029. return 0;
  3030. }
  3031. static int b43legacy_op_get_survey(struct ieee80211_hw *hw, int idx,
  3032. struct survey_info *survey)
  3033. {
  3034. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  3035. struct b43legacy_wldev *dev = wl->current_dev;
  3036. struct ieee80211_conf *conf = &hw->conf;
  3037. if (idx != 0)
  3038. return -ENOENT;
  3039. survey->channel = conf->channel;
  3040. survey->filled = SURVEY_INFO_NOISE_DBM;
  3041. survey->noise = dev->stats.link_noise;
  3042. return 0;
  3043. }
  3044. static const struct ieee80211_ops b43legacy_hw_ops = {
  3045. .tx = b43legacy_op_tx,
  3046. .conf_tx = b43legacy_op_conf_tx,
  3047. .add_interface = b43legacy_op_add_interface,
  3048. .remove_interface = b43legacy_op_remove_interface,
  3049. .config = b43legacy_op_dev_config,
  3050. .bss_info_changed = b43legacy_op_bss_info_changed,
  3051. .configure_filter = b43legacy_op_configure_filter,
  3052. .get_stats = b43legacy_op_get_stats,
  3053. .start = b43legacy_op_start,
  3054. .stop = b43legacy_op_stop,
  3055. .set_tim = b43legacy_op_beacon_set_tim,
  3056. .get_survey = b43legacy_op_get_survey,
  3057. .rfkill_poll = b43legacy_rfkill_poll,
  3058. };
  3059. /* Hard-reset the chip. Do not call this directly.
  3060. * Use b43legacy_controller_restart()
  3061. */
  3062. static void b43legacy_chip_reset(struct work_struct *work)
  3063. {
  3064. struct b43legacy_wldev *dev =
  3065. container_of(work, struct b43legacy_wldev, restart_work);
  3066. struct b43legacy_wl *wl = dev->wl;
  3067. int err = 0;
  3068. int prev_status;
  3069. mutex_lock(&wl->mutex);
  3070. prev_status = b43legacy_status(dev);
  3071. /* Bring the device down... */
  3072. if (prev_status >= B43legacy_STAT_STARTED)
  3073. b43legacy_wireless_core_stop(dev);
  3074. if (prev_status >= B43legacy_STAT_INITIALIZED)
  3075. b43legacy_wireless_core_exit(dev);
  3076. /* ...and up again. */
  3077. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  3078. err = b43legacy_wireless_core_init(dev);
  3079. if (err)
  3080. goto out;
  3081. }
  3082. if (prev_status >= B43legacy_STAT_STARTED) {
  3083. err = b43legacy_wireless_core_start(dev);
  3084. if (err) {
  3085. b43legacy_wireless_core_exit(dev);
  3086. goto out;
  3087. }
  3088. }
  3089. out:
  3090. if (err)
  3091. wl->current_dev = NULL; /* Failed to init the dev. */
  3092. mutex_unlock(&wl->mutex);
  3093. if (err)
  3094. b43legacyerr(wl, "Controller restart FAILED\n");
  3095. else
  3096. b43legacyinfo(wl, "Controller restarted\n");
  3097. }
  3098. static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
  3099. int have_bphy,
  3100. int have_gphy)
  3101. {
  3102. struct ieee80211_hw *hw = dev->wl->hw;
  3103. struct b43legacy_phy *phy = &dev->phy;
  3104. phy->possible_phymodes = 0;
  3105. if (have_bphy) {
  3106. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3107. &b43legacy_band_2GHz_BPHY;
  3108. phy->possible_phymodes |= B43legacy_PHYMODE_B;
  3109. }
  3110. if (have_gphy) {
  3111. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3112. &b43legacy_band_2GHz_GPHY;
  3113. phy->possible_phymodes |= B43legacy_PHYMODE_G;
  3114. }
  3115. return 0;
  3116. }
  3117. static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
  3118. {
  3119. /* We release firmware that late to not be required to re-request
  3120. * is all the time when we reinit the core. */
  3121. b43legacy_release_firmware(dev);
  3122. }
  3123. static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
  3124. {
  3125. struct b43legacy_wl *wl = dev->wl;
  3126. struct ssb_bus *bus = dev->dev->bus;
  3127. struct pci_dev *pdev = (bus->bustype == SSB_BUSTYPE_PCI) ? bus->host_pci : NULL;
  3128. int err;
  3129. int have_bphy = 0;
  3130. int have_gphy = 0;
  3131. u32 tmp;
  3132. /* Do NOT do any device initialization here.
  3133. * Do it in wireless_core_init() instead.
  3134. * This function is for gathering basic information about the HW, only.
  3135. * Also some structs may be set up here. But most likely you want to
  3136. * have that in core_init(), too.
  3137. */
  3138. err = ssb_bus_powerup(bus, 0);
  3139. if (err) {
  3140. b43legacyerr(wl, "Bus powerup failed\n");
  3141. goto out;
  3142. }
  3143. /* Get the PHY type. */
  3144. if (dev->dev->id.revision >= 5) {
  3145. u32 tmshigh;
  3146. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3147. have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
  3148. if (!have_gphy)
  3149. have_bphy = 1;
  3150. } else if (dev->dev->id.revision == 4)
  3151. have_gphy = 1;
  3152. else
  3153. have_bphy = 1;
  3154. dev->phy.gmode = (have_gphy || have_bphy);
  3155. dev->phy.radio_on = 1;
  3156. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3157. b43legacy_wireless_core_reset(dev, tmp);
  3158. err = b43legacy_phy_versioning(dev);
  3159. if (err)
  3160. goto err_powerdown;
  3161. /* Check if this device supports multiband. */
  3162. if (!pdev ||
  3163. (pdev->device != 0x4312 &&
  3164. pdev->device != 0x4319 &&
  3165. pdev->device != 0x4324)) {
  3166. /* No multiband support. */
  3167. have_bphy = 0;
  3168. have_gphy = 0;
  3169. switch (dev->phy.type) {
  3170. case B43legacy_PHYTYPE_B:
  3171. have_bphy = 1;
  3172. break;
  3173. case B43legacy_PHYTYPE_G:
  3174. have_gphy = 1;
  3175. break;
  3176. default:
  3177. B43legacy_BUG_ON(1);
  3178. }
  3179. }
  3180. dev->phy.gmode = (have_gphy || have_bphy);
  3181. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3182. b43legacy_wireless_core_reset(dev, tmp);
  3183. err = b43legacy_validate_chipaccess(dev);
  3184. if (err)
  3185. goto err_powerdown;
  3186. err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
  3187. if (err)
  3188. goto err_powerdown;
  3189. /* Now set some default "current_dev" */
  3190. if (!wl->current_dev)
  3191. wl->current_dev = dev;
  3192. INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
  3193. b43legacy_radio_turn_off(dev, 1);
  3194. b43legacy_switch_analog(dev, 0);
  3195. ssb_device_disable(dev->dev, 0);
  3196. ssb_bus_may_powerdown(bus);
  3197. out:
  3198. return err;
  3199. err_powerdown:
  3200. ssb_bus_may_powerdown(bus);
  3201. return err;
  3202. }
  3203. static void b43legacy_one_core_detach(struct ssb_device *dev)
  3204. {
  3205. struct b43legacy_wldev *wldev;
  3206. struct b43legacy_wl *wl;
  3207. /* Do not cancel ieee80211-workqueue based work here.
  3208. * See comment in b43legacy_remove(). */
  3209. wldev = ssb_get_drvdata(dev);
  3210. wl = wldev->wl;
  3211. b43legacy_debugfs_remove_device(wldev);
  3212. b43legacy_wireless_core_detach(wldev);
  3213. list_del(&wldev->list);
  3214. wl->nr_devs--;
  3215. ssb_set_drvdata(dev, NULL);
  3216. kfree(wldev);
  3217. }
  3218. static int b43legacy_one_core_attach(struct ssb_device *dev,
  3219. struct b43legacy_wl *wl)
  3220. {
  3221. struct b43legacy_wldev *wldev;
  3222. struct pci_dev *pdev;
  3223. int err = -ENOMEM;
  3224. if (!list_empty(&wl->devlist)) {
  3225. /* We are not the first core on this chip. */
  3226. pdev = (dev->bus->bustype == SSB_BUSTYPE_PCI) ? dev->bus->host_pci : NULL;
  3227. /* Only special chips support more than one wireless
  3228. * core, although some of the other chips have more than
  3229. * one wireless core as well. Check for this and
  3230. * bail out early.
  3231. */
  3232. if (!pdev ||
  3233. ((pdev->device != 0x4321) &&
  3234. (pdev->device != 0x4313) &&
  3235. (pdev->device != 0x431A))) {
  3236. b43legacydbg(wl, "Ignoring unconnected 802.11 core\n");
  3237. return -ENODEV;
  3238. }
  3239. }
  3240. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3241. if (!wldev)
  3242. goto out;
  3243. wldev->dev = dev;
  3244. wldev->wl = wl;
  3245. b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
  3246. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3247. tasklet_init(&wldev->isr_tasklet,
  3248. (void (*)(unsigned long))b43legacy_interrupt_tasklet,
  3249. (unsigned long)wldev);
  3250. if (modparam_pio)
  3251. wldev->__using_pio = 1;
  3252. INIT_LIST_HEAD(&wldev->list);
  3253. err = b43legacy_wireless_core_attach(wldev);
  3254. if (err)
  3255. goto err_kfree_wldev;
  3256. list_add(&wldev->list, &wl->devlist);
  3257. wl->nr_devs++;
  3258. ssb_set_drvdata(dev, wldev);
  3259. b43legacy_debugfs_add_device(wldev);
  3260. out:
  3261. return err;
  3262. err_kfree_wldev:
  3263. kfree(wldev);
  3264. return err;
  3265. }
  3266. static void b43legacy_sprom_fixup(struct ssb_bus *bus)
  3267. {
  3268. /* boardflags workarounds */
  3269. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3270. bus->boardinfo.type == 0x4E &&
  3271. bus->boardinfo.rev > 0x40)
  3272. bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
  3273. }
  3274. static void b43legacy_wireless_exit(struct ssb_device *dev,
  3275. struct b43legacy_wl *wl)
  3276. {
  3277. struct ieee80211_hw *hw = wl->hw;
  3278. ssb_set_devtypedata(dev, NULL);
  3279. ieee80211_free_hw(hw);
  3280. }
  3281. static int b43legacy_wireless_init(struct ssb_device *dev)
  3282. {
  3283. struct ssb_sprom *sprom = &dev->bus->sprom;
  3284. struct ieee80211_hw *hw;
  3285. struct b43legacy_wl *wl;
  3286. int err = -ENOMEM;
  3287. b43legacy_sprom_fixup(dev->bus);
  3288. hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
  3289. if (!hw) {
  3290. b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
  3291. goto out;
  3292. }
  3293. /* fill hw info */
  3294. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  3295. IEEE80211_HW_SIGNAL_DBM;
  3296. hw->wiphy->interface_modes =
  3297. BIT(NL80211_IFTYPE_AP) |
  3298. BIT(NL80211_IFTYPE_STATION) |
  3299. BIT(NL80211_IFTYPE_WDS) |
  3300. BIT(NL80211_IFTYPE_ADHOC);
  3301. hw->queues = 1; /* FIXME: hardware has more queues */
  3302. hw->max_rates = 2;
  3303. SET_IEEE80211_DEV(hw, dev->dev);
  3304. if (is_valid_ether_addr(sprom->et1mac))
  3305. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  3306. else
  3307. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  3308. /* Get and initialize struct b43legacy_wl */
  3309. wl = hw_to_b43legacy_wl(hw);
  3310. memset(wl, 0, sizeof(*wl));
  3311. wl->hw = hw;
  3312. spin_lock_init(&wl->irq_lock);
  3313. spin_lock_init(&wl->leds_lock);
  3314. mutex_init(&wl->mutex);
  3315. INIT_LIST_HEAD(&wl->devlist);
  3316. INIT_WORK(&wl->beacon_update_trigger, b43legacy_beacon_update_trigger_work);
  3317. ssb_set_devtypedata(dev, wl);
  3318. b43legacyinfo(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3319. err = 0;
  3320. out:
  3321. return err;
  3322. }
  3323. static int b43legacy_probe(struct ssb_device *dev,
  3324. const struct ssb_device_id *id)
  3325. {
  3326. struct b43legacy_wl *wl;
  3327. int err;
  3328. int first = 0;
  3329. wl = ssb_get_devtypedata(dev);
  3330. if (!wl) {
  3331. /* Probing the first core - setup common struct b43legacy_wl */
  3332. first = 1;
  3333. err = b43legacy_wireless_init(dev);
  3334. if (err)
  3335. goto out;
  3336. wl = ssb_get_devtypedata(dev);
  3337. B43legacy_WARN_ON(!wl);
  3338. }
  3339. err = b43legacy_one_core_attach(dev, wl);
  3340. if (err)
  3341. goto err_wireless_exit;
  3342. if (first) {
  3343. err = ieee80211_register_hw(wl->hw);
  3344. if (err)
  3345. goto err_one_core_detach;
  3346. }
  3347. out:
  3348. return err;
  3349. err_one_core_detach:
  3350. b43legacy_one_core_detach(dev);
  3351. err_wireless_exit:
  3352. if (first)
  3353. b43legacy_wireless_exit(dev, wl);
  3354. return err;
  3355. }
  3356. static void b43legacy_remove(struct ssb_device *dev)
  3357. {
  3358. struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
  3359. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3360. /* We must cancel any work here before unregistering from ieee80211,
  3361. * as the ieee80211 unreg will destroy the workqueue. */
  3362. cancel_work_sync(&wldev->restart_work);
  3363. B43legacy_WARN_ON(!wl);
  3364. if (wl->current_dev == wldev)
  3365. ieee80211_unregister_hw(wl->hw);
  3366. b43legacy_one_core_detach(dev);
  3367. if (list_empty(&wl->devlist))
  3368. /* Last core on the chip unregistered.
  3369. * We can destroy common struct b43legacy_wl.
  3370. */
  3371. b43legacy_wireless_exit(dev, wl);
  3372. }
  3373. /* Perform a hardware reset. This can be called from any context. */
  3374. void b43legacy_controller_restart(struct b43legacy_wldev *dev,
  3375. const char *reason)
  3376. {
  3377. /* Must avoid requeueing, if we are in shutdown. */
  3378. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
  3379. return;
  3380. b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
  3381. ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
  3382. }
  3383. #ifdef CONFIG_PM
  3384. static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
  3385. {
  3386. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3387. struct b43legacy_wl *wl = wldev->wl;
  3388. b43legacydbg(wl, "Suspending...\n");
  3389. mutex_lock(&wl->mutex);
  3390. wldev->suspend_init_status = b43legacy_status(wldev);
  3391. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
  3392. b43legacy_wireless_core_stop(wldev);
  3393. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
  3394. b43legacy_wireless_core_exit(wldev);
  3395. mutex_unlock(&wl->mutex);
  3396. b43legacydbg(wl, "Device suspended.\n");
  3397. return 0;
  3398. }
  3399. static int b43legacy_resume(struct ssb_device *dev)
  3400. {
  3401. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3402. struct b43legacy_wl *wl = wldev->wl;
  3403. int err = 0;
  3404. b43legacydbg(wl, "Resuming...\n");
  3405. mutex_lock(&wl->mutex);
  3406. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
  3407. err = b43legacy_wireless_core_init(wldev);
  3408. if (err) {
  3409. b43legacyerr(wl, "Resume failed at core init\n");
  3410. goto out;
  3411. }
  3412. }
  3413. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
  3414. err = b43legacy_wireless_core_start(wldev);
  3415. if (err) {
  3416. b43legacy_wireless_core_exit(wldev);
  3417. b43legacyerr(wl, "Resume failed at core start\n");
  3418. goto out;
  3419. }
  3420. }
  3421. b43legacydbg(wl, "Device resumed.\n");
  3422. out:
  3423. mutex_unlock(&wl->mutex);
  3424. return err;
  3425. }
  3426. #else /* CONFIG_PM */
  3427. # define b43legacy_suspend NULL
  3428. # define b43legacy_resume NULL
  3429. #endif /* CONFIG_PM */
  3430. static struct ssb_driver b43legacy_ssb_driver = {
  3431. .name = KBUILD_MODNAME,
  3432. .id_table = b43legacy_ssb_tbl,
  3433. .probe = b43legacy_probe,
  3434. .remove = b43legacy_remove,
  3435. .suspend = b43legacy_suspend,
  3436. .resume = b43legacy_resume,
  3437. };
  3438. static void b43legacy_print_driverinfo(void)
  3439. {
  3440. const char *feat_pci = "", *feat_leds = "",
  3441. *feat_pio = "", *feat_dma = "";
  3442. #ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
  3443. feat_pci = "P";
  3444. #endif
  3445. #ifdef CONFIG_B43LEGACY_LEDS
  3446. feat_leds = "L";
  3447. #endif
  3448. #ifdef CONFIG_B43LEGACY_PIO
  3449. feat_pio = "I";
  3450. #endif
  3451. #ifdef CONFIG_B43LEGACY_DMA
  3452. feat_dma = "D";
  3453. #endif
  3454. printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
  3455. "[ Features: %s%s%s%s, Firmware-ID: "
  3456. B43legacy_SUPPORTED_FIRMWARE_ID " ]\n",
  3457. feat_pci, feat_leds, feat_pio, feat_dma);
  3458. }
  3459. static int __init b43legacy_init(void)
  3460. {
  3461. int err;
  3462. b43legacy_debugfs_init();
  3463. err = ssb_driver_register(&b43legacy_ssb_driver);
  3464. if (err)
  3465. goto err_dfs_exit;
  3466. b43legacy_print_driverinfo();
  3467. return err;
  3468. err_dfs_exit:
  3469. b43legacy_debugfs_exit();
  3470. return err;
  3471. }
  3472. static void __exit b43legacy_exit(void)
  3473. {
  3474. ssb_driver_unregister(&b43legacy_ssb_driver);
  3475. b43legacy_debugfs_exit();
  3476. }
  3477. module_init(b43legacy_init)
  3478. module_exit(b43legacy_exit)