main.c 108 KB

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