main.c 108 KB

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