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