main.c 109 KB

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