main.c 107 KB

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