main.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961
  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. }
  888. }
  889. static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
  890. u16 shm_offset, u16 size,
  891. struct ieee80211_rate *rate)
  892. {
  893. struct b43legacy_plcp_hdr4 plcp;
  894. u32 tmp;
  895. __le16 dur;
  896. plcp.data = 0;
  897. b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->bitrate);
  898. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  899. dev->wl->vif,
  900. size,
  901. rate);
  902. /* Write PLCP in two parts and timing for packet transfer */
  903. tmp = le32_to_cpu(plcp.data);
  904. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
  905. tmp & 0xFFFF);
  906. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
  907. tmp >> 16);
  908. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
  909. le16_to_cpu(dur));
  910. }
  911. /* Instead of using custom probe response template, this function
  912. * just patches custom beacon template by:
  913. * 1) Changing packet type
  914. * 2) Patching duration field
  915. * 3) Stripping TIM
  916. */
  917. static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
  918. u16 *dest_size,
  919. struct ieee80211_rate *rate)
  920. {
  921. const u8 *src_data;
  922. u8 *dest_data;
  923. u16 src_size, elem_size, src_pos, dest_pos;
  924. __le16 dur;
  925. struct ieee80211_hdr *hdr;
  926. size_t ie_start;
  927. src_size = dev->wl->current_beacon->len;
  928. src_data = (const u8 *)dev->wl->current_beacon->data;
  929. /* Get the start offset of the variable IEs in the packet. */
  930. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  931. B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
  932. u.beacon.variable));
  933. if (B43legacy_WARN_ON(src_size < ie_start))
  934. return NULL;
  935. dest_data = kmalloc(src_size, GFP_ATOMIC);
  936. if (unlikely(!dest_data))
  937. return NULL;
  938. /* Copy the static data and all Information Elements, except the TIM. */
  939. memcpy(dest_data, src_data, ie_start);
  940. src_pos = ie_start;
  941. dest_pos = ie_start;
  942. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  943. elem_size = src_data[src_pos + 1] + 2;
  944. if (src_data[src_pos] == 5) {
  945. /* This is the TIM. */
  946. continue;
  947. }
  948. memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
  949. dest_pos += elem_size;
  950. }
  951. *dest_size = dest_pos;
  952. hdr = (struct ieee80211_hdr *)dest_data;
  953. /* Set the frame control. */
  954. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  955. IEEE80211_STYPE_PROBE_RESP);
  956. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  957. dev->wl->vif,
  958. *dest_size,
  959. rate);
  960. hdr->duration_id = dur;
  961. return dest_data;
  962. }
  963. static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
  964. u16 ram_offset,
  965. u16 shm_size_offset,
  966. struct ieee80211_rate *rate)
  967. {
  968. const u8 *probe_resp_data;
  969. u16 size;
  970. size = dev->wl->current_beacon->len;
  971. probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
  972. if (unlikely(!probe_resp_data))
  973. return;
  974. /* Looks like PLCP headers plus packet timings are stored for
  975. * all possible basic rates
  976. */
  977. b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
  978. &b43legacy_b_ratetable[0]);
  979. b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
  980. &b43legacy_b_ratetable[1]);
  981. b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
  982. &b43legacy_b_ratetable[2]);
  983. b43legacy_write_probe_resp_plcp(dev, 0x350, size,
  984. &b43legacy_b_ratetable[3]);
  985. size = min((size_t)size,
  986. 0x200 - sizeof(struct b43legacy_plcp_hdr6));
  987. b43legacy_write_template_common(dev, probe_resp_data,
  988. size, ram_offset,
  989. shm_size_offset, rate->bitrate);
  990. kfree(probe_resp_data);
  991. }
  992. /* Asynchronously update the packet templates in template RAM.
  993. * Locking: Requires wl->irq_lock to be locked. */
  994. static void b43legacy_update_templates(struct b43legacy_wl *wl)
  995. {
  996. struct sk_buff *beacon;
  997. /* This is the top half of the ansynchronous beacon update. The bottom
  998. * half is the beacon IRQ. Beacon update must be asynchronous to avoid
  999. * sending an invalid beacon. This can happen for example, if the
  1000. * firmware transmits a beacon while we are updating it. */
  1001. /* We could modify the existing beacon and set the aid bit in the TIM
  1002. * field, but that would probably require resizing and moving of data
  1003. * within the beacon template. Simply request a new beacon and let
  1004. * mac80211 do the hard work. */
  1005. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1006. if (unlikely(!beacon))
  1007. return;
  1008. if (wl->current_beacon)
  1009. dev_kfree_skb_any(wl->current_beacon);
  1010. wl->current_beacon = beacon;
  1011. wl->beacon0_uploaded = 0;
  1012. wl->beacon1_uploaded = 0;
  1013. }
  1014. static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
  1015. u16 beacon_int)
  1016. {
  1017. b43legacy_time_lock(dev);
  1018. if (dev->dev->id.revision >= 3)
  1019. b43legacy_write32(dev, 0x188, (beacon_int << 16));
  1020. else {
  1021. b43legacy_write16(dev, 0x606, (beacon_int >> 6));
  1022. b43legacy_write16(dev, 0x610, beacon_int);
  1023. }
  1024. b43legacy_time_unlock(dev);
  1025. }
  1026. static void handle_irq_beacon(struct b43legacy_wldev *dev)
  1027. {
  1028. struct b43legacy_wl *wl = dev->wl;
  1029. u32 cmd;
  1030. if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1031. return;
  1032. /* This is the bottom half of the asynchronous beacon update. */
  1033. cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
  1034. if (!(cmd & B43legacy_MACCMD_BEACON0_VALID)) {
  1035. if (!wl->beacon0_uploaded) {
  1036. b43legacy_write_beacon_template(dev, 0x68,
  1037. B43legacy_SHM_SH_BTL0,
  1038. B43legacy_CCK_RATE_1MB);
  1039. b43legacy_write_probe_resp_template(dev, 0x268,
  1040. B43legacy_SHM_SH_PRTLEN,
  1041. &__b43legacy_ratetable[3]);
  1042. wl->beacon0_uploaded = 1;
  1043. }
  1044. cmd |= B43legacy_MACCMD_BEACON0_VALID;
  1045. }
  1046. if (!(cmd & B43legacy_MACCMD_BEACON1_VALID)) {
  1047. if (!wl->beacon1_uploaded) {
  1048. b43legacy_write_beacon_template(dev, 0x468,
  1049. B43legacy_SHM_SH_BTL1,
  1050. B43legacy_CCK_RATE_1MB);
  1051. wl->beacon1_uploaded = 1;
  1052. }
  1053. cmd |= B43legacy_MACCMD_BEACON1_VALID;
  1054. }
  1055. b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
  1056. }
  1057. static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
  1058. {
  1059. }
  1060. /* Interrupt handler bottom-half */
  1061. static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
  1062. {
  1063. u32 reason;
  1064. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1065. u32 merged_dma_reason = 0;
  1066. int i;
  1067. unsigned long flags;
  1068. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1069. B43legacy_WARN_ON(b43legacy_status(dev) <
  1070. B43legacy_STAT_INITIALIZED);
  1071. reason = dev->irq_reason;
  1072. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1073. dma_reason[i] = dev->dma_reason[i];
  1074. merged_dma_reason |= dma_reason[i];
  1075. }
  1076. if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
  1077. b43legacyerr(dev->wl, "MAC transmission error\n");
  1078. if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
  1079. b43legacyerr(dev->wl, "PHY transmission error\n");
  1080. rmb();
  1081. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1082. b43legacyerr(dev->wl, "Too many PHY TX errors, "
  1083. "restarting the controller\n");
  1084. b43legacy_controller_restart(dev, "PHY TX errors");
  1085. }
  1086. }
  1087. if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
  1088. B43legacy_DMAIRQ_NONFATALMASK))) {
  1089. if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
  1090. b43legacyerr(dev->wl, "Fatal DMA error: "
  1091. "0x%08X, 0x%08X, 0x%08X, "
  1092. "0x%08X, 0x%08X, 0x%08X\n",
  1093. dma_reason[0], dma_reason[1],
  1094. dma_reason[2], dma_reason[3],
  1095. dma_reason[4], dma_reason[5]);
  1096. b43legacy_controller_restart(dev, "DMA error");
  1097. mmiowb();
  1098. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1099. return;
  1100. }
  1101. if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
  1102. b43legacyerr(dev->wl, "DMA error: "
  1103. "0x%08X, 0x%08X, 0x%08X, "
  1104. "0x%08X, 0x%08X, 0x%08X\n",
  1105. dma_reason[0], dma_reason[1],
  1106. dma_reason[2], dma_reason[3],
  1107. dma_reason[4], dma_reason[5]);
  1108. }
  1109. if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
  1110. handle_irq_ucode_debug(dev);
  1111. if (reason & B43legacy_IRQ_TBTT_INDI)
  1112. handle_irq_tbtt_indication(dev);
  1113. if (reason & B43legacy_IRQ_ATIM_END)
  1114. handle_irq_atim_end(dev);
  1115. if (reason & B43legacy_IRQ_BEACON)
  1116. handle_irq_beacon(dev);
  1117. if (reason & B43legacy_IRQ_PMQ)
  1118. handle_irq_pmq(dev);
  1119. if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
  1120. ;/*TODO*/
  1121. if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
  1122. handle_irq_noise(dev);
  1123. /* Check the DMA reason registers for received data. */
  1124. if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
  1125. if (b43legacy_using_pio(dev))
  1126. b43legacy_pio_rx(dev->pio.queue0);
  1127. else
  1128. b43legacy_dma_rx(dev->dma.rx_ring0);
  1129. }
  1130. B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
  1131. B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
  1132. if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
  1133. if (b43legacy_using_pio(dev))
  1134. b43legacy_pio_rx(dev->pio.queue3);
  1135. else
  1136. b43legacy_dma_rx(dev->dma.rx_ring3);
  1137. }
  1138. B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
  1139. B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
  1140. if (reason & B43legacy_IRQ_TX_OK)
  1141. handle_irq_transmit_status(dev);
  1142. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  1143. mmiowb();
  1144. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1145. }
  1146. static void pio_irq_workaround(struct b43legacy_wldev *dev,
  1147. u16 base, int queueidx)
  1148. {
  1149. u16 rxctl;
  1150. rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
  1151. if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
  1152. dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
  1153. else
  1154. dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
  1155. }
  1156. static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
  1157. {
  1158. if (b43legacy_using_pio(dev) &&
  1159. (dev->dev->id.revision < 3) &&
  1160. (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
  1161. /* Apply a PIO specific workaround to the dma_reasons */
  1162. pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
  1163. pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
  1164. pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
  1165. pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
  1166. }
  1167. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
  1168. b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
  1169. dev->dma_reason[0]);
  1170. b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
  1171. dev->dma_reason[1]);
  1172. b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
  1173. dev->dma_reason[2]);
  1174. b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
  1175. dev->dma_reason[3]);
  1176. b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
  1177. dev->dma_reason[4]);
  1178. b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
  1179. dev->dma_reason[5]);
  1180. }
  1181. /* Interrupt handler top-half */
  1182. static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
  1183. {
  1184. irqreturn_t ret = IRQ_NONE;
  1185. struct b43legacy_wldev *dev = dev_id;
  1186. u32 reason;
  1187. if (!dev)
  1188. return IRQ_NONE;
  1189. spin_lock(&dev->wl->irq_lock);
  1190. if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
  1191. goto out;
  1192. reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1193. if (reason == 0xffffffff) /* shared IRQ */
  1194. goto out;
  1195. ret = IRQ_HANDLED;
  1196. reason &= b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
  1197. if (!reason)
  1198. goto out;
  1199. dev->dma_reason[0] = b43legacy_read32(dev,
  1200. B43legacy_MMIO_DMA0_REASON)
  1201. & 0x0001DC00;
  1202. dev->dma_reason[1] = b43legacy_read32(dev,
  1203. B43legacy_MMIO_DMA1_REASON)
  1204. & 0x0000DC00;
  1205. dev->dma_reason[2] = b43legacy_read32(dev,
  1206. B43legacy_MMIO_DMA2_REASON)
  1207. & 0x0000DC00;
  1208. dev->dma_reason[3] = b43legacy_read32(dev,
  1209. B43legacy_MMIO_DMA3_REASON)
  1210. & 0x0001DC00;
  1211. dev->dma_reason[4] = b43legacy_read32(dev,
  1212. B43legacy_MMIO_DMA4_REASON)
  1213. & 0x0000DC00;
  1214. dev->dma_reason[5] = b43legacy_read32(dev,
  1215. B43legacy_MMIO_DMA5_REASON)
  1216. & 0x0000DC00;
  1217. b43legacy_interrupt_ack(dev, reason);
  1218. /* disable all IRQs. They are enabled again in the bottom half. */
  1219. dev->irq_savedstate = b43legacy_interrupt_disable(dev,
  1220. B43legacy_IRQ_ALL);
  1221. /* save the reason code and call our bottom half. */
  1222. dev->irq_reason = reason;
  1223. tasklet_schedule(&dev->isr_tasklet);
  1224. out:
  1225. mmiowb();
  1226. spin_unlock(&dev->wl->irq_lock);
  1227. return ret;
  1228. }
  1229. static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
  1230. {
  1231. release_firmware(dev->fw.ucode);
  1232. dev->fw.ucode = NULL;
  1233. release_firmware(dev->fw.pcm);
  1234. dev->fw.pcm = NULL;
  1235. release_firmware(dev->fw.initvals);
  1236. dev->fw.initvals = NULL;
  1237. release_firmware(dev->fw.initvals_band);
  1238. dev->fw.initvals_band = NULL;
  1239. }
  1240. static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
  1241. {
  1242. b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
  1243. "Drivers/b43#devicefirmware "
  1244. "and download the correct firmware (version 3).\n");
  1245. }
  1246. static int do_request_fw(struct b43legacy_wldev *dev,
  1247. const char *name,
  1248. const struct firmware **fw)
  1249. {
  1250. char path[sizeof(modparam_fwpostfix) + 32];
  1251. struct b43legacy_fw_header *hdr;
  1252. u32 size;
  1253. int err;
  1254. if (!name)
  1255. return 0;
  1256. snprintf(path, ARRAY_SIZE(path),
  1257. "b43legacy%s/%s.fw",
  1258. modparam_fwpostfix, name);
  1259. err = request_firmware(fw, path, dev->dev->dev);
  1260. if (err) {
  1261. b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
  1262. "or load failed.\n", path);
  1263. return err;
  1264. }
  1265. if ((*fw)->size < sizeof(struct b43legacy_fw_header))
  1266. goto err_format;
  1267. hdr = (struct b43legacy_fw_header *)((*fw)->data);
  1268. switch (hdr->type) {
  1269. case B43legacy_FW_TYPE_UCODE:
  1270. case B43legacy_FW_TYPE_PCM:
  1271. size = be32_to_cpu(hdr->size);
  1272. if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
  1273. goto err_format;
  1274. /* fallthrough */
  1275. case B43legacy_FW_TYPE_IV:
  1276. if (hdr->ver != 1)
  1277. goto err_format;
  1278. break;
  1279. default:
  1280. goto err_format;
  1281. }
  1282. return err;
  1283. err_format:
  1284. b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1285. return -EPROTO;
  1286. }
  1287. static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
  1288. {
  1289. struct b43legacy_firmware *fw = &dev->fw;
  1290. const u8 rev = dev->dev->id.revision;
  1291. const char *filename;
  1292. u32 tmshigh;
  1293. int err;
  1294. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1295. if (!fw->ucode) {
  1296. if (rev == 2)
  1297. filename = "ucode2";
  1298. else if (rev == 4)
  1299. filename = "ucode4";
  1300. else
  1301. filename = "ucode5";
  1302. err = do_request_fw(dev, filename, &fw->ucode);
  1303. if (err)
  1304. goto err_load;
  1305. }
  1306. if (!fw->pcm) {
  1307. if (rev < 5)
  1308. filename = "pcm4";
  1309. else
  1310. filename = "pcm5";
  1311. err = do_request_fw(dev, filename, &fw->pcm);
  1312. if (err)
  1313. goto err_load;
  1314. }
  1315. if (!fw->initvals) {
  1316. switch (dev->phy.type) {
  1317. case B43legacy_PHYTYPE_B:
  1318. case B43legacy_PHYTYPE_G:
  1319. if ((rev >= 5) && (rev <= 10))
  1320. filename = "b0g0initvals5";
  1321. else if (rev == 2 || rev == 4)
  1322. filename = "b0g0initvals2";
  1323. else
  1324. goto err_no_initvals;
  1325. break;
  1326. default:
  1327. goto err_no_initvals;
  1328. }
  1329. err = do_request_fw(dev, filename, &fw->initvals);
  1330. if (err)
  1331. goto err_load;
  1332. }
  1333. if (!fw->initvals_band) {
  1334. switch (dev->phy.type) {
  1335. case B43legacy_PHYTYPE_B:
  1336. case B43legacy_PHYTYPE_G:
  1337. if ((rev >= 5) && (rev <= 10))
  1338. filename = "b0g0bsinitvals5";
  1339. else if (rev >= 11)
  1340. filename = NULL;
  1341. else if (rev == 2 || rev == 4)
  1342. filename = NULL;
  1343. else
  1344. goto err_no_initvals;
  1345. break;
  1346. default:
  1347. goto err_no_initvals;
  1348. }
  1349. err = do_request_fw(dev, filename, &fw->initvals_band);
  1350. if (err)
  1351. goto err_load;
  1352. }
  1353. return 0;
  1354. err_load:
  1355. b43legacy_print_fw_helptext(dev->wl);
  1356. goto error;
  1357. err_no_initvals:
  1358. err = -ENODEV;
  1359. b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
  1360. "core rev %u\n", dev->phy.type, rev);
  1361. goto error;
  1362. error:
  1363. b43legacy_release_firmware(dev);
  1364. return err;
  1365. }
  1366. static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
  1367. {
  1368. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1369. const __be32 *data;
  1370. unsigned int i;
  1371. unsigned int len;
  1372. u16 fwrev;
  1373. u16 fwpatch;
  1374. u16 fwdate;
  1375. u16 fwtime;
  1376. u32 tmp, macctl;
  1377. int err = 0;
  1378. /* Jump the microcode PSM to offset 0 */
  1379. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1380. B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
  1381. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1382. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1383. /* Zero out all microcode PSM registers and shared memory. */
  1384. for (i = 0; i < 64; i++)
  1385. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
  1386. for (i = 0; i < 4096; i += 2)
  1387. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
  1388. /* Upload Microcode. */
  1389. data = (__be32 *) (dev->fw.ucode->data + hdr_len);
  1390. len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
  1391. b43legacy_shm_control_word(dev,
  1392. B43legacy_SHM_UCODE |
  1393. B43legacy_SHM_AUTOINC_W,
  1394. 0x0000);
  1395. for (i = 0; i < len; i++) {
  1396. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1397. be32_to_cpu(data[i]));
  1398. udelay(10);
  1399. }
  1400. if (dev->fw.pcm) {
  1401. /* Upload PCM data. */
  1402. data = (__be32 *) (dev->fw.pcm->data + hdr_len);
  1403. len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
  1404. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
  1405. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
  1406. /* No need for autoinc bit in SHM_HW */
  1407. b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
  1408. for (i = 0; i < len; i++) {
  1409. b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
  1410. be32_to_cpu(data[i]));
  1411. udelay(10);
  1412. }
  1413. }
  1414. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1415. B43legacy_IRQ_ALL);
  1416. /* Start the microcode PSM */
  1417. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1418. macctl &= ~B43legacy_MACCTL_PSM_JMP0;
  1419. macctl |= B43legacy_MACCTL_PSM_RUN;
  1420. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1421. /* Wait for the microcode to load and respond */
  1422. i = 0;
  1423. while (1) {
  1424. tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1425. if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
  1426. break;
  1427. i++;
  1428. if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
  1429. b43legacyerr(dev->wl, "Microcode not responding\n");
  1430. b43legacy_print_fw_helptext(dev->wl);
  1431. err = -ENODEV;
  1432. goto error;
  1433. }
  1434. msleep_interruptible(50);
  1435. if (signal_pending(current)) {
  1436. err = -EINTR;
  1437. goto error;
  1438. }
  1439. }
  1440. /* dummy read follows */
  1441. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1442. /* Get and check the revisions. */
  1443. fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1444. B43legacy_SHM_SH_UCODEREV);
  1445. fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1446. B43legacy_SHM_SH_UCODEPATCH);
  1447. fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1448. B43legacy_SHM_SH_UCODEDATE);
  1449. fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1450. B43legacy_SHM_SH_UCODETIME);
  1451. if (fwrev > 0x128) {
  1452. b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
  1453. " Only firmware from binary drivers version 3.x"
  1454. " is supported. You must change your firmware"
  1455. " files.\n");
  1456. b43legacy_print_fw_helptext(dev->wl);
  1457. err = -EOPNOTSUPP;
  1458. goto error;
  1459. }
  1460. b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
  1461. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
  1462. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1463. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
  1464. fwtime & 0x1F);
  1465. dev->fw.rev = fwrev;
  1466. dev->fw.patch = fwpatch;
  1467. return 0;
  1468. error:
  1469. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1470. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  1471. macctl |= B43legacy_MACCTL_PSM_JMP0;
  1472. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1473. return err;
  1474. }
  1475. static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
  1476. const struct b43legacy_iv *ivals,
  1477. size_t count,
  1478. size_t array_size)
  1479. {
  1480. const struct b43legacy_iv *iv;
  1481. u16 offset;
  1482. size_t i;
  1483. bool bit32;
  1484. BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
  1485. iv = ivals;
  1486. for (i = 0; i < count; i++) {
  1487. if (array_size < sizeof(iv->offset_size))
  1488. goto err_format;
  1489. array_size -= sizeof(iv->offset_size);
  1490. offset = be16_to_cpu(iv->offset_size);
  1491. bit32 = !!(offset & B43legacy_IV_32BIT);
  1492. offset &= B43legacy_IV_OFFSET_MASK;
  1493. if (offset >= 0x1000)
  1494. goto err_format;
  1495. if (bit32) {
  1496. u32 value;
  1497. if (array_size < sizeof(iv->data.d32))
  1498. goto err_format;
  1499. array_size -= sizeof(iv->data.d32);
  1500. value = get_unaligned_be32(&iv->data.d32);
  1501. b43legacy_write32(dev, offset, value);
  1502. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1503. sizeof(__be16) +
  1504. sizeof(__be32));
  1505. } else {
  1506. u16 value;
  1507. if (array_size < sizeof(iv->data.d16))
  1508. goto err_format;
  1509. array_size -= sizeof(iv->data.d16);
  1510. value = be16_to_cpu(iv->data.d16);
  1511. b43legacy_write16(dev, offset, value);
  1512. iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
  1513. sizeof(__be16) +
  1514. sizeof(__be16));
  1515. }
  1516. }
  1517. if (array_size)
  1518. goto err_format;
  1519. return 0;
  1520. err_format:
  1521. b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
  1522. b43legacy_print_fw_helptext(dev->wl);
  1523. return -EPROTO;
  1524. }
  1525. static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
  1526. {
  1527. const size_t hdr_len = sizeof(struct b43legacy_fw_header);
  1528. const struct b43legacy_fw_header *hdr;
  1529. struct b43legacy_firmware *fw = &dev->fw;
  1530. const struct b43legacy_iv *ivals;
  1531. size_t count;
  1532. int err;
  1533. hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
  1534. ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
  1535. count = be32_to_cpu(hdr->size);
  1536. err = b43legacy_write_initvals(dev, ivals, count,
  1537. fw->initvals->size - hdr_len);
  1538. if (err)
  1539. goto out;
  1540. if (fw->initvals_band) {
  1541. hdr = (const struct b43legacy_fw_header *)
  1542. (fw->initvals_band->data);
  1543. ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
  1544. + hdr_len);
  1545. count = be32_to_cpu(hdr->size);
  1546. err = b43legacy_write_initvals(dev, ivals, count,
  1547. fw->initvals_band->size - hdr_len);
  1548. if (err)
  1549. goto out;
  1550. }
  1551. out:
  1552. return err;
  1553. }
  1554. /* Initialize the GPIOs
  1555. * http://bcm-specs.sipsolutions.net/GPIO
  1556. */
  1557. static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
  1558. {
  1559. struct ssb_bus *bus = dev->dev->bus;
  1560. struct ssb_device *gpiodev, *pcidev = NULL;
  1561. u32 mask;
  1562. u32 set;
  1563. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1564. b43legacy_read32(dev,
  1565. B43legacy_MMIO_MACCTL)
  1566. & 0xFFFF3FFF);
  1567. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1568. b43legacy_read16(dev,
  1569. B43legacy_MMIO_GPIO_MASK)
  1570. | 0x000F);
  1571. mask = 0x0000001F;
  1572. set = 0x0000000F;
  1573. if (dev->dev->bus->chip_id == 0x4301) {
  1574. mask |= 0x0060;
  1575. set |= 0x0060;
  1576. }
  1577. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
  1578. b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
  1579. b43legacy_read16(dev,
  1580. B43legacy_MMIO_GPIO_MASK)
  1581. | 0x0200);
  1582. mask |= 0x0200;
  1583. set |= 0x0200;
  1584. }
  1585. if (dev->dev->id.revision >= 2)
  1586. mask |= 0x0010; /* FIXME: This is redundant. */
  1587. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1588. pcidev = bus->pcicore.dev;
  1589. #endif
  1590. gpiodev = bus->chipco.dev ? : pcidev;
  1591. if (!gpiodev)
  1592. return 0;
  1593. ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
  1594. (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
  1595. & mask) | set);
  1596. return 0;
  1597. }
  1598. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  1599. static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
  1600. {
  1601. struct ssb_bus *bus = dev->dev->bus;
  1602. struct ssb_device *gpiodev, *pcidev = NULL;
  1603. #ifdef CONFIG_SSB_DRIVER_PCICORE
  1604. pcidev = bus->pcicore.dev;
  1605. #endif
  1606. gpiodev = bus->chipco.dev ? : pcidev;
  1607. if (!gpiodev)
  1608. return;
  1609. ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
  1610. }
  1611. /* http://bcm-specs.sipsolutions.net/EnableMac */
  1612. void b43legacy_mac_enable(struct b43legacy_wldev *dev)
  1613. {
  1614. dev->mac_suspended--;
  1615. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1616. B43legacy_WARN_ON(irqs_disabled());
  1617. if (dev->mac_suspended == 0) {
  1618. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1619. b43legacy_read32(dev,
  1620. B43legacy_MMIO_MACCTL)
  1621. | B43legacy_MACCTL_ENABLED);
  1622. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
  1623. B43legacy_IRQ_MAC_SUSPENDED);
  1624. /* the next two are dummy reads */
  1625. b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1626. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1627. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1628. /* Re-enable IRQs. */
  1629. spin_lock_irq(&dev->wl->irq_lock);
  1630. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  1631. spin_unlock_irq(&dev->wl->irq_lock);
  1632. }
  1633. }
  1634. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  1635. void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
  1636. {
  1637. int i;
  1638. u32 tmp;
  1639. might_sleep();
  1640. B43legacy_WARN_ON(irqs_disabled());
  1641. B43legacy_WARN_ON(dev->mac_suspended < 0);
  1642. if (dev->mac_suspended == 0) {
  1643. /* Mask IRQs before suspending MAC. Otherwise
  1644. * the MAC stays busy and won't suspend. */
  1645. spin_lock_irq(&dev->wl->irq_lock);
  1646. tmp = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  1647. spin_unlock_irq(&dev->wl->irq_lock);
  1648. b43legacy_synchronize_irq(dev);
  1649. dev->irq_savedstate = tmp;
  1650. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1651. b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
  1652. b43legacy_read32(dev,
  1653. B43legacy_MMIO_MACCTL)
  1654. & ~B43legacy_MACCTL_ENABLED);
  1655. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1656. for (i = 40; i; i--) {
  1657. tmp = b43legacy_read32(dev,
  1658. B43legacy_MMIO_GEN_IRQ_REASON);
  1659. if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
  1660. goto out;
  1661. msleep(1);
  1662. }
  1663. b43legacyerr(dev->wl, "MAC suspend failed\n");
  1664. }
  1665. out:
  1666. dev->mac_suspended++;
  1667. }
  1668. static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
  1669. {
  1670. struct b43legacy_wl *wl = dev->wl;
  1671. u32 ctl;
  1672. u16 cfp_pretbtt;
  1673. ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1674. /* Reset status to STA infrastructure mode. */
  1675. ctl &= ~B43legacy_MACCTL_AP;
  1676. ctl &= ~B43legacy_MACCTL_KEEP_CTL;
  1677. ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
  1678. ctl &= ~B43legacy_MACCTL_KEEP_BAD;
  1679. ctl &= ~B43legacy_MACCTL_PROMISC;
  1680. ctl &= ~B43legacy_MACCTL_BEACPROMISC;
  1681. ctl |= B43legacy_MACCTL_INFRA;
  1682. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  1683. ctl |= B43legacy_MACCTL_AP;
  1684. else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
  1685. ctl &= ~B43legacy_MACCTL_INFRA;
  1686. if (wl->filter_flags & FIF_CONTROL)
  1687. ctl |= B43legacy_MACCTL_KEEP_CTL;
  1688. if (wl->filter_flags & FIF_FCSFAIL)
  1689. ctl |= B43legacy_MACCTL_KEEP_BAD;
  1690. if (wl->filter_flags & FIF_PLCPFAIL)
  1691. ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
  1692. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  1693. ctl |= B43legacy_MACCTL_PROMISC;
  1694. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  1695. ctl |= B43legacy_MACCTL_BEACPROMISC;
  1696. /* Workaround: On old hardware the HW-MAC-address-filter
  1697. * doesn't work properly, so always run promisc in filter
  1698. * it in software. */
  1699. if (dev->dev->id.revision <= 4)
  1700. ctl |= B43legacy_MACCTL_PROMISC;
  1701. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
  1702. cfp_pretbtt = 2;
  1703. if ((ctl & B43legacy_MACCTL_INFRA) &&
  1704. !(ctl & B43legacy_MACCTL_AP)) {
  1705. if (dev->dev->bus->chip_id == 0x4306 &&
  1706. dev->dev->bus->chip_rev == 3)
  1707. cfp_pretbtt = 100;
  1708. else
  1709. cfp_pretbtt = 50;
  1710. }
  1711. b43legacy_write16(dev, 0x612, cfp_pretbtt);
  1712. }
  1713. static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
  1714. u16 rate,
  1715. int is_ofdm)
  1716. {
  1717. u16 offset;
  1718. if (is_ofdm) {
  1719. offset = 0x480;
  1720. offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  1721. } else {
  1722. offset = 0x4C0;
  1723. offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  1724. }
  1725. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
  1726. b43legacy_shm_read16(dev,
  1727. B43legacy_SHM_SHARED, offset));
  1728. }
  1729. static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
  1730. {
  1731. switch (dev->phy.type) {
  1732. case B43legacy_PHYTYPE_G:
  1733. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
  1734. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
  1735. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
  1736. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
  1737. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
  1738. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
  1739. b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
  1740. /* fallthrough */
  1741. case B43legacy_PHYTYPE_B:
  1742. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
  1743. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
  1744. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
  1745. b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
  1746. break;
  1747. default:
  1748. B43legacy_BUG_ON(1);
  1749. }
  1750. }
  1751. /* Set the TX-Antenna for management frames sent by firmware. */
  1752. static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
  1753. int antenna)
  1754. {
  1755. u16 ant = 0;
  1756. u16 tmp;
  1757. switch (antenna) {
  1758. case B43legacy_ANTENNA0:
  1759. ant |= B43legacy_TX4_PHY_ANT0;
  1760. break;
  1761. case B43legacy_ANTENNA1:
  1762. ant |= B43legacy_TX4_PHY_ANT1;
  1763. break;
  1764. case B43legacy_ANTENNA_AUTO:
  1765. ant |= B43legacy_TX4_PHY_ANTLAST;
  1766. break;
  1767. default:
  1768. B43legacy_BUG_ON(1);
  1769. }
  1770. /* FIXME We also need to set the other flags of the PHY control
  1771. * field somewhere. */
  1772. /* For Beacons */
  1773. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1774. B43legacy_SHM_SH_BEACPHYCTL);
  1775. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1776. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1777. B43legacy_SHM_SH_BEACPHYCTL, tmp);
  1778. /* For ACK/CTS */
  1779. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1780. B43legacy_SHM_SH_ACKCTSPHYCTL);
  1781. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1782. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1783. B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
  1784. /* For Probe Resposes */
  1785. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1786. B43legacy_SHM_SH_PRPHYCTL);
  1787. tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
  1788. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  1789. B43legacy_SHM_SH_PRPHYCTL, tmp);
  1790. }
  1791. /* This is the opposite of b43legacy_chip_init() */
  1792. static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
  1793. {
  1794. b43legacy_radio_turn_off(dev, 1);
  1795. b43legacy_gpio_cleanup(dev);
  1796. /* firmware is released later */
  1797. }
  1798. /* Initialize the chip
  1799. * http://bcm-specs.sipsolutions.net/ChipInit
  1800. */
  1801. static int b43legacy_chip_init(struct b43legacy_wldev *dev)
  1802. {
  1803. struct b43legacy_phy *phy = &dev->phy;
  1804. int err;
  1805. int tmp;
  1806. u32 value32, macctl;
  1807. u16 value16;
  1808. /* Initialize the MAC control */
  1809. macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
  1810. if (dev->phy.gmode)
  1811. macctl |= B43legacy_MACCTL_GMODE;
  1812. macctl |= B43legacy_MACCTL_INFRA;
  1813. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  1814. err = b43legacy_request_firmware(dev);
  1815. if (err)
  1816. goto out;
  1817. err = b43legacy_upload_microcode(dev);
  1818. if (err)
  1819. goto out; /* firmware is released later */
  1820. err = b43legacy_gpio_init(dev);
  1821. if (err)
  1822. goto out; /* firmware is released later */
  1823. err = b43legacy_upload_initvals(dev);
  1824. if (err)
  1825. goto err_gpio_clean;
  1826. b43legacy_radio_turn_on(dev);
  1827. b43legacy_write16(dev, 0x03E6, 0x0000);
  1828. err = b43legacy_phy_init(dev);
  1829. if (err)
  1830. goto err_radio_off;
  1831. /* Select initial Interference Mitigation. */
  1832. tmp = phy->interfmode;
  1833. phy->interfmode = B43legacy_INTERFMODE_NONE;
  1834. b43legacy_radio_set_interference_mitigation(dev, tmp);
  1835. b43legacy_phy_set_antenna_diversity(dev);
  1836. b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
  1837. if (phy->type == B43legacy_PHYTYPE_B) {
  1838. value16 = b43legacy_read16(dev, 0x005E);
  1839. value16 |= 0x0004;
  1840. b43legacy_write16(dev, 0x005E, value16);
  1841. }
  1842. b43legacy_write32(dev, 0x0100, 0x01000000);
  1843. if (dev->dev->id.revision < 5)
  1844. b43legacy_write32(dev, 0x010C, 0x01000000);
  1845. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1846. value32 &= ~B43legacy_MACCTL_INFRA;
  1847. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1848. value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1849. value32 |= B43legacy_MACCTL_INFRA;
  1850. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
  1851. if (b43legacy_using_pio(dev)) {
  1852. b43legacy_write32(dev, 0x0210, 0x00000100);
  1853. b43legacy_write32(dev, 0x0230, 0x00000100);
  1854. b43legacy_write32(dev, 0x0250, 0x00000100);
  1855. b43legacy_write32(dev, 0x0270, 0x00000100);
  1856. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
  1857. 0x0000);
  1858. }
  1859. /* Probe Response Timeout value */
  1860. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  1861. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
  1862. /* Initially set the wireless operation mode. */
  1863. b43legacy_adjust_opmode(dev);
  1864. if (dev->dev->id.revision < 3) {
  1865. b43legacy_write16(dev, 0x060E, 0x0000);
  1866. b43legacy_write16(dev, 0x0610, 0x8000);
  1867. b43legacy_write16(dev, 0x0604, 0x0000);
  1868. b43legacy_write16(dev, 0x0606, 0x0200);
  1869. } else {
  1870. b43legacy_write32(dev, 0x0188, 0x80000000);
  1871. b43legacy_write32(dev, 0x018C, 0x02000000);
  1872. }
  1873. b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
  1874. b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  1875. b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  1876. b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  1877. b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  1878. b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  1879. b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  1880. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  1881. value32 |= 0x00100000;
  1882. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  1883. b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
  1884. dev->dev->bus->chipco.fast_pwrup_delay);
  1885. /* PHY TX errors counter. */
  1886. atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  1887. B43legacy_WARN_ON(err != 0);
  1888. b43legacydbg(dev->wl, "Chip initialized\n");
  1889. out:
  1890. return err;
  1891. err_radio_off:
  1892. b43legacy_radio_turn_off(dev, 1);
  1893. err_gpio_clean:
  1894. b43legacy_gpio_cleanup(dev);
  1895. goto out;
  1896. }
  1897. static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
  1898. {
  1899. struct b43legacy_phy *phy = &dev->phy;
  1900. if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
  1901. return;
  1902. b43legacy_mac_suspend(dev);
  1903. b43legacy_phy_lo_g_measure(dev);
  1904. b43legacy_mac_enable(dev);
  1905. }
  1906. static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
  1907. {
  1908. b43legacy_phy_lo_mark_all_unused(dev);
  1909. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
  1910. b43legacy_mac_suspend(dev);
  1911. b43legacy_calc_nrssi_slope(dev);
  1912. b43legacy_mac_enable(dev);
  1913. }
  1914. }
  1915. static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
  1916. {
  1917. /* Update device statistics. */
  1918. b43legacy_calculate_link_quality(dev);
  1919. }
  1920. static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
  1921. {
  1922. b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
  1923. atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
  1924. wmb();
  1925. }
  1926. static void do_periodic_work(struct b43legacy_wldev *dev)
  1927. {
  1928. unsigned int state;
  1929. state = dev->periodic_state;
  1930. if (state % 8 == 0)
  1931. b43legacy_periodic_every120sec(dev);
  1932. if (state % 4 == 0)
  1933. b43legacy_periodic_every60sec(dev);
  1934. if (state % 2 == 0)
  1935. b43legacy_periodic_every30sec(dev);
  1936. b43legacy_periodic_every15sec(dev);
  1937. }
  1938. /* Periodic work locking policy:
  1939. * The whole periodic work handler is protected by
  1940. * wl->mutex. If another lock is needed somewhere in the
  1941. * pwork callchain, it's aquired in-place, where it's needed.
  1942. */
  1943. static void b43legacy_periodic_work_handler(struct work_struct *work)
  1944. {
  1945. struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
  1946. periodic_work.work);
  1947. struct b43legacy_wl *wl = dev->wl;
  1948. unsigned long delay;
  1949. mutex_lock(&wl->mutex);
  1950. if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
  1951. goto out;
  1952. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
  1953. goto out_requeue;
  1954. do_periodic_work(dev);
  1955. dev->periodic_state++;
  1956. out_requeue:
  1957. if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
  1958. delay = msecs_to_jiffies(50);
  1959. else
  1960. delay = round_jiffies_relative(HZ * 15);
  1961. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  1962. out:
  1963. mutex_unlock(&wl->mutex);
  1964. }
  1965. static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
  1966. {
  1967. struct delayed_work *work = &dev->periodic_work;
  1968. dev->periodic_state = 0;
  1969. INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
  1970. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  1971. }
  1972. /* Validate access to the chip (SHM) */
  1973. static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
  1974. {
  1975. u32 value;
  1976. u32 shm_backup;
  1977. shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
  1978. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
  1979. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  1980. 0xAA5555AA)
  1981. goto error;
  1982. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
  1983. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
  1984. 0x55AAAA55)
  1985. goto error;
  1986. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
  1987. value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  1988. if ((value | B43legacy_MACCTL_GMODE) !=
  1989. (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
  1990. goto error;
  1991. value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
  1992. if (value)
  1993. goto error;
  1994. return 0;
  1995. error:
  1996. b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
  1997. return -ENODEV;
  1998. }
  1999. static void b43legacy_security_init(struct b43legacy_wldev *dev)
  2000. {
  2001. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2002. B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2003. dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2004. 0x0056);
  2005. /* KTP is a word address, but we address SHM bytewise.
  2006. * So multiply by two.
  2007. */
  2008. dev->ktp *= 2;
  2009. if (dev->dev->id.revision >= 5)
  2010. /* Number of RCMTA address slots */
  2011. b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
  2012. dev->max_nr_keys - 8);
  2013. }
  2014. static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
  2015. {
  2016. struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
  2017. unsigned long flags;
  2018. /* Don't take wl->mutex here, as it could deadlock with
  2019. * hwrng internal locking. It's not needed to take
  2020. * wl->mutex here, anyway. */
  2021. spin_lock_irqsave(&wl->irq_lock, flags);
  2022. *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
  2023. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2024. return (sizeof(u16));
  2025. }
  2026. static void b43legacy_rng_exit(struct b43legacy_wl *wl)
  2027. {
  2028. if (wl->rng_initialized)
  2029. hwrng_unregister(&wl->rng);
  2030. }
  2031. static int b43legacy_rng_init(struct b43legacy_wl *wl)
  2032. {
  2033. int err;
  2034. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2035. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2036. wl->rng.name = wl->rng_name;
  2037. wl->rng.data_read = b43legacy_rng_read;
  2038. wl->rng.priv = (unsigned long)wl;
  2039. wl->rng_initialized = 1;
  2040. err = hwrng_register(&wl->rng);
  2041. if (err) {
  2042. wl->rng_initialized = 0;
  2043. b43legacyerr(wl, "Failed to register the random "
  2044. "number generator (%d)\n", err);
  2045. }
  2046. return err;
  2047. }
  2048. static int b43legacy_op_tx(struct ieee80211_hw *hw,
  2049. struct sk_buff *skb)
  2050. {
  2051. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2052. struct b43legacy_wldev *dev = wl->current_dev;
  2053. int err = -ENODEV;
  2054. unsigned long flags;
  2055. if (unlikely(!dev))
  2056. goto out;
  2057. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
  2058. goto out;
  2059. /* DMA-TX is done without a global lock. */
  2060. if (b43legacy_using_pio(dev)) {
  2061. spin_lock_irqsave(&wl->irq_lock, flags);
  2062. err = b43legacy_pio_tx(dev, skb);
  2063. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2064. } else
  2065. err = b43legacy_dma_tx(dev, skb);
  2066. out:
  2067. if (unlikely(err)) {
  2068. /* Drop the packet. */
  2069. dev_kfree_skb_any(skb);
  2070. }
  2071. return NETDEV_TX_OK;
  2072. }
  2073. static int b43legacy_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2074. const struct ieee80211_tx_queue_params *params)
  2075. {
  2076. return 0;
  2077. }
  2078. static int b43legacy_op_get_tx_stats(struct ieee80211_hw *hw,
  2079. struct ieee80211_tx_queue_stats *stats)
  2080. {
  2081. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2082. struct b43legacy_wldev *dev = wl->current_dev;
  2083. unsigned long flags;
  2084. int err = -ENODEV;
  2085. if (!dev)
  2086. goto out;
  2087. spin_lock_irqsave(&wl->irq_lock, flags);
  2088. if (likely(b43legacy_status(dev) >= B43legacy_STAT_STARTED)) {
  2089. if (b43legacy_using_pio(dev))
  2090. b43legacy_pio_get_tx_stats(dev, stats);
  2091. else
  2092. b43legacy_dma_get_tx_stats(dev, stats);
  2093. err = 0;
  2094. }
  2095. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2096. out:
  2097. return err;
  2098. }
  2099. static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
  2100. struct ieee80211_low_level_stats *stats)
  2101. {
  2102. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2103. unsigned long flags;
  2104. spin_lock_irqsave(&wl->irq_lock, flags);
  2105. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2106. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2107. return 0;
  2108. }
  2109. static const char *phymode_to_string(unsigned int phymode)
  2110. {
  2111. switch (phymode) {
  2112. case B43legacy_PHYMODE_B:
  2113. return "B";
  2114. case B43legacy_PHYMODE_G:
  2115. return "G";
  2116. default:
  2117. B43legacy_BUG_ON(1);
  2118. }
  2119. return "";
  2120. }
  2121. static int find_wldev_for_phymode(struct b43legacy_wl *wl,
  2122. unsigned int phymode,
  2123. struct b43legacy_wldev **dev,
  2124. bool *gmode)
  2125. {
  2126. struct b43legacy_wldev *d;
  2127. list_for_each_entry(d, &wl->devlist, list) {
  2128. if (d->phy.possible_phymodes & phymode) {
  2129. /* Ok, this device supports the PHY-mode.
  2130. * Set the gmode bit. */
  2131. *gmode = 1;
  2132. *dev = d;
  2133. return 0;
  2134. }
  2135. }
  2136. return -ESRCH;
  2137. }
  2138. static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
  2139. {
  2140. struct ssb_device *sdev = dev->dev;
  2141. u32 tmslow;
  2142. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2143. tmslow &= ~B43legacy_TMSLOW_GMODE;
  2144. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2145. tmslow |= SSB_TMSLOW_FGC;
  2146. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2147. msleep(1);
  2148. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2149. tmslow &= ~SSB_TMSLOW_FGC;
  2150. tmslow |= B43legacy_TMSLOW_PHYRESET;
  2151. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2152. msleep(1);
  2153. }
  2154. /* Expects wl->mutex locked */
  2155. static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
  2156. unsigned int new_mode)
  2157. {
  2158. struct b43legacy_wldev *uninitialized_var(up_dev);
  2159. struct b43legacy_wldev *down_dev;
  2160. int err;
  2161. bool gmode = 0;
  2162. int prev_status;
  2163. err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
  2164. if (err) {
  2165. b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
  2166. phymode_to_string(new_mode));
  2167. return err;
  2168. }
  2169. if ((up_dev == wl->current_dev) &&
  2170. (!!wl->current_dev->phy.gmode == !!gmode))
  2171. /* This device is already running. */
  2172. return 0;
  2173. b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
  2174. phymode_to_string(new_mode));
  2175. down_dev = wl->current_dev;
  2176. prev_status = b43legacy_status(down_dev);
  2177. /* Shutdown the currently running core. */
  2178. if (prev_status >= B43legacy_STAT_STARTED)
  2179. b43legacy_wireless_core_stop(down_dev);
  2180. if (prev_status >= B43legacy_STAT_INITIALIZED)
  2181. b43legacy_wireless_core_exit(down_dev);
  2182. if (down_dev != up_dev)
  2183. /* We switch to a different core, so we put PHY into
  2184. * RESET on the old core. */
  2185. b43legacy_put_phy_into_reset(down_dev);
  2186. /* Now start the new core. */
  2187. up_dev->phy.gmode = gmode;
  2188. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  2189. err = b43legacy_wireless_core_init(up_dev);
  2190. if (err) {
  2191. b43legacyerr(wl, "Fatal: Could not initialize device"
  2192. " for newly selected %s-PHY mode\n",
  2193. phymode_to_string(new_mode));
  2194. goto init_failure;
  2195. }
  2196. }
  2197. if (prev_status >= B43legacy_STAT_STARTED) {
  2198. err = b43legacy_wireless_core_start(up_dev);
  2199. if (err) {
  2200. b43legacyerr(wl, "Fatal: Coult not start device for "
  2201. "newly selected %s-PHY mode\n",
  2202. phymode_to_string(new_mode));
  2203. b43legacy_wireless_core_exit(up_dev);
  2204. goto init_failure;
  2205. }
  2206. }
  2207. B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
  2208. b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
  2209. wl->current_dev = up_dev;
  2210. return 0;
  2211. init_failure:
  2212. /* Whoops, failed to init the new core. No core is operating now. */
  2213. wl->current_dev = NULL;
  2214. return err;
  2215. }
  2216. /* Write the short and long frame retry limit values. */
  2217. static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
  2218. unsigned int short_retry,
  2219. unsigned int long_retry)
  2220. {
  2221. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2222. * the chip-internal counter. */
  2223. short_retry = min(short_retry, (unsigned int)0xF);
  2224. long_retry = min(long_retry, (unsigned int)0xF);
  2225. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
  2226. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
  2227. }
  2228. static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
  2229. u32 changed)
  2230. {
  2231. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2232. struct b43legacy_wldev *dev;
  2233. struct b43legacy_phy *phy;
  2234. struct ieee80211_conf *conf = &hw->conf;
  2235. unsigned long flags;
  2236. unsigned int new_phymode = 0xFFFF;
  2237. int antenna_tx;
  2238. int antenna_rx;
  2239. int err = 0;
  2240. u32 savedirqs;
  2241. antenna_tx = B43legacy_ANTENNA_DEFAULT;
  2242. antenna_rx = B43legacy_ANTENNA_DEFAULT;
  2243. mutex_lock(&wl->mutex);
  2244. dev = wl->current_dev;
  2245. phy = &dev->phy;
  2246. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  2247. b43legacy_set_retry_limits(dev,
  2248. conf->short_frame_max_tx_count,
  2249. conf->long_frame_max_tx_count);
  2250. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  2251. if (!changed)
  2252. goto out_unlock_mutex;
  2253. /* Switch the PHY mode (if necessary). */
  2254. switch (conf->channel->band) {
  2255. case IEEE80211_BAND_2GHZ:
  2256. if (phy->type == B43legacy_PHYTYPE_B)
  2257. new_phymode = B43legacy_PHYMODE_B;
  2258. else
  2259. new_phymode = B43legacy_PHYMODE_G;
  2260. break;
  2261. default:
  2262. B43legacy_WARN_ON(1);
  2263. }
  2264. err = b43legacy_switch_phymode(wl, new_phymode);
  2265. if (err)
  2266. goto out_unlock_mutex;
  2267. /* Disable IRQs while reconfiguring the device.
  2268. * This makes it possible to drop the spinlock throughout
  2269. * the reconfiguration process. */
  2270. spin_lock_irqsave(&wl->irq_lock, flags);
  2271. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2272. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2273. goto out_unlock_mutex;
  2274. }
  2275. savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  2276. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2277. b43legacy_synchronize_irq(dev);
  2278. /* Switch to the requested channel.
  2279. * The firmware takes care of races with the TX handler. */
  2280. if (conf->channel->hw_value != phy->channel)
  2281. b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
  2282. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  2283. /* Adjust the desired TX power level. */
  2284. if (conf->power_level != 0) {
  2285. if (conf->power_level != phy->power_level) {
  2286. phy->power_level = conf->power_level;
  2287. b43legacy_phy_xmitpower(dev);
  2288. }
  2289. }
  2290. /* Antennas for RX and management frame TX. */
  2291. b43legacy_mgmtframe_txantenna(dev, antenna_tx);
  2292. /* Update templates for AP mode. */
  2293. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
  2294. b43legacy_set_beacon_int(dev, conf->beacon_int);
  2295. if (!!conf->radio_enabled != phy->radio_on) {
  2296. if (conf->radio_enabled) {
  2297. b43legacy_radio_turn_on(dev);
  2298. b43legacyinfo(dev->wl, "Radio turned on by software\n");
  2299. if (!dev->radio_hw_enable)
  2300. b43legacyinfo(dev->wl, "The hardware RF-kill"
  2301. " button still turns the radio"
  2302. " physically off. Press the"
  2303. " button to turn it on.\n");
  2304. } else {
  2305. b43legacy_radio_turn_off(dev, 0);
  2306. b43legacyinfo(dev->wl, "Radio turned off by"
  2307. " software\n");
  2308. }
  2309. }
  2310. spin_lock_irqsave(&wl->irq_lock, flags);
  2311. b43legacy_interrupt_enable(dev, savedirqs);
  2312. mmiowb();
  2313. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2314. out_unlock_mutex:
  2315. mutex_unlock(&wl->mutex);
  2316. return err;
  2317. }
  2318. static void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
  2319. {
  2320. struct ieee80211_supported_band *sband =
  2321. dev->wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  2322. struct ieee80211_rate *rate;
  2323. int i;
  2324. u16 basic, direct, offset, basic_offset, rateptr;
  2325. for (i = 0; i < sband->n_bitrates; i++) {
  2326. rate = &sband->bitrates[i];
  2327. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2328. direct = B43legacy_SHM_SH_CCKDIRECT;
  2329. basic = B43legacy_SHM_SH_CCKBASIC;
  2330. offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2331. offset &= 0xF;
  2332. } else {
  2333. direct = B43legacy_SHM_SH_OFDMDIRECT;
  2334. basic = B43legacy_SHM_SH_OFDMBASIC;
  2335. offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2336. offset &= 0xF;
  2337. }
  2338. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  2339. if (b43legacy_is_cck_rate(rate->hw_value)) {
  2340. basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
  2341. basic_offset &= 0xF;
  2342. } else {
  2343. basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
  2344. basic_offset &= 0xF;
  2345. }
  2346. /*
  2347. * Get the pointer that we need to point to
  2348. * from the direct map
  2349. */
  2350. rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  2351. direct + 2 * basic_offset);
  2352. /* and write it to the basic map */
  2353. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2354. basic + 2 * offset, rateptr);
  2355. }
  2356. }
  2357. static void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
  2358. struct ieee80211_vif *vif,
  2359. struct ieee80211_bss_conf *conf,
  2360. u32 changed)
  2361. {
  2362. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2363. struct b43legacy_wldev *dev;
  2364. struct b43legacy_phy *phy;
  2365. unsigned long flags;
  2366. u32 savedirqs;
  2367. mutex_lock(&wl->mutex);
  2368. dev = wl->current_dev;
  2369. phy = &dev->phy;
  2370. /* Disable IRQs while reconfiguring the device.
  2371. * This makes it possible to drop the spinlock throughout
  2372. * the reconfiguration process. */
  2373. spin_lock_irqsave(&wl->irq_lock, flags);
  2374. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2375. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2376. goto out_unlock_mutex;
  2377. }
  2378. savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
  2379. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2380. b43legacy_synchronize_irq(dev);
  2381. b43legacy_mac_suspend(dev);
  2382. if (changed & BSS_CHANGED_BASIC_RATES)
  2383. b43legacy_update_basic_rates(dev, conf->basic_rates);
  2384. if (changed & BSS_CHANGED_ERP_SLOT) {
  2385. if (conf->use_short_slot)
  2386. b43legacy_short_slot_timing_enable(dev);
  2387. else
  2388. b43legacy_short_slot_timing_disable(dev);
  2389. }
  2390. b43legacy_mac_enable(dev);
  2391. spin_lock_irqsave(&wl->irq_lock, flags);
  2392. b43legacy_interrupt_enable(dev, savedirqs);
  2393. /* XXX: why? */
  2394. mmiowb();
  2395. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2396. out_unlock_mutex:
  2397. mutex_unlock(&wl->mutex);
  2398. return;
  2399. }
  2400. static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
  2401. unsigned int changed,
  2402. unsigned int *fflags,
  2403. int mc_count,
  2404. struct dev_addr_list *mc_list)
  2405. {
  2406. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2407. struct b43legacy_wldev *dev = wl->current_dev;
  2408. unsigned long flags;
  2409. if (!dev) {
  2410. *fflags = 0;
  2411. return;
  2412. }
  2413. spin_lock_irqsave(&wl->irq_lock, flags);
  2414. *fflags &= FIF_PROMISC_IN_BSS |
  2415. FIF_ALLMULTI |
  2416. FIF_FCSFAIL |
  2417. FIF_PLCPFAIL |
  2418. FIF_CONTROL |
  2419. FIF_OTHER_BSS |
  2420. FIF_BCN_PRBRESP_PROMISC;
  2421. changed &= FIF_PROMISC_IN_BSS |
  2422. FIF_ALLMULTI |
  2423. FIF_FCSFAIL |
  2424. FIF_PLCPFAIL |
  2425. FIF_CONTROL |
  2426. FIF_OTHER_BSS |
  2427. FIF_BCN_PRBRESP_PROMISC;
  2428. wl->filter_flags = *fflags;
  2429. if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
  2430. b43legacy_adjust_opmode(dev);
  2431. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2432. }
  2433. static int b43legacy_op_config_interface(struct ieee80211_hw *hw,
  2434. struct ieee80211_vif *vif,
  2435. struct ieee80211_if_conf *conf)
  2436. {
  2437. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2438. struct b43legacy_wldev *dev = wl->current_dev;
  2439. unsigned long flags;
  2440. if (!dev)
  2441. return -ENODEV;
  2442. mutex_lock(&wl->mutex);
  2443. spin_lock_irqsave(&wl->irq_lock, flags);
  2444. B43legacy_WARN_ON(wl->vif != vif);
  2445. if (conf->bssid)
  2446. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  2447. else
  2448. memset(wl->bssid, 0, ETH_ALEN);
  2449. if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
  2450. if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP)) {
  2451. B43legacy_WARN_ON(vif->type != NL80211_IFTYPE_AP);
  2452. if (conf->changed & IEEE80211_IFCC_BEACON)
  2453. b43legacy_update_templates(wl);
  2454. } else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
  2455. if (conf->changed & IEEE80211_IFCC_BEACON)
  2456. b43legacy_update_templates(wl);
  2457. }
  2458. b43legacy_write_mac_bssid_templates(dev);
  2459. }
  2460. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2461. mutex_unlock(&wl->mutex);
  2462. return 0;
  2463. }
  2464. /* Locking: wl->mutex */
  2465. static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
  2466. {
  2467. struct b43legacy_wl *wl = dev->wl;
  2468. unsigned long flags;
  2469. if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
  2470. return;
  2471. /* Disable and sync interrupts. We must do this before than
  2472. * setting the status to INITIALIZED, as the interrupt handler
  2473. * won't care about IRQs then. */
  2474. spin_lock_irqsave(&wl->irq_lock, flags);
  2475. dev->irq_savedstate = b43legacy_interrupt_disable(dev,
  2476. B43legacy_IRQ_ALL);
  2477. b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
  2478. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2479. b43legacy_synchronize_irq(dev);
  2480. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2481. mutex_unlock(&wl->mutex);
  2482. /* Must unlock as it would otherwise deadlock. No races here.
  2483. * Cancel the possibly running self-rearming periodic work. */
  2484. cancel_delayed_work_sync(&dev->periodic_work);
  2485. mutex_lock(&wl->mutex);
  2486. ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
  2487. b43legacy_mac_suspend(dev);
  2488. free_irq(dev->dev->irq, dev);
  2489. b43legacydbg(wl, "Wireless interface stopped\n");
  2490. }
  2491. /* Locking: wl->mutex */
  2492. static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
  2493. {
  2494. int err;
  2495. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
  2496. drain_txstatus_queue(dev);
  2497. err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
  2498. IRQF_SHARED, KBUILD_MODNAME, dev);
  2499. if (err) {
  2500. b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
  2501. dev->dev->irq);
  2502. goto out;
  2503. }
  2504. /* We are ready to run. */
  2505. b43legacy_set_status(dev, B43legacy_STAT_STARTED);
  2506. /* Start data flow (TX/RX) */
  2507. b43legacy_mac_enable(dev);
  2508. b43legacy_interrupt_enable(dev, dev->irq_savedstate);
  2509. /* Start maintenance work */
  2510. b43legacy_periodic_tasks_setup(dev);
  2511. b43legacydbg(dev->wl, "Wireless interface started\n");
  2512. out:
  2513. return err;
  2514. }
  2515. /* Get PHY and RADIO versioning numbers */
  2516. static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
  2517. {
  2518. struct b43legacy_phy *phy = &dev->phy;
  2519. u32 tmp;
  2520. u8 analog_type;
  2521. u8 phy_type;
  2522. u8 phy_rev;
  2523. u16 radio_manuf;
  2524. u16 radio_ver;
  2525. u16 radio_rev;
  2526. int unsupported = 0;
  2527. /* Get PHY versioning */
  2528. tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
  2529. analog_type = (tmp & B43legacy_PHYVER_ANALOG)
  2530. >> B43legacy_PHYVER_ANALOG_SHIFT;
  2531. phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
  2532. phy_rev = (tmp & B43legacy_PHYVER_VERSION);
  2533. switch (phy_type) {
  2534. case B43legacy_PHYTYPE_B:
  2535. if (phy_rev != 2 && phy_rev != 4
  2536. && phy_rev != 6 && phy_rev != 7)
  2537. unsupported = 1;
  2538. break;
  2539. case B43legacy_PHYTYPE_G:
  2540. if (phy_rev > 8)
  2541. unsupported = 1;
  2542. break;
  2543. default:
  2544. unsupported = 1;
  2545. };
  2546. if (unsupported) {
  2547. b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
  2548. "(Analog %u, Type %u, Revision %u)\n",
  2549. analog_type, phy_type, phy_rev);
  2550. return -EOPNOTSUPP;
  2551. }
  2552. b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  2553. analog_type, phy_type, phy_rev);
  2554. /* Get RADIO versioning */
  2555. if (dev->dev->bus->chip_id == 0x4317) {
  2556. if (dev->dev->bus->chip_rev == 0)
  2557. tmp = 0x3205017F;
  2558. else if (dev->dev->bus->chip_rev == 1)
  2559. tmp = 0x4205017F;
  2560. else
  2561. tmp = 0x5205017F;
  2562. } else {
  2563. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2564. B43legacy_RADIOCTL_ID);
  2565. tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
  2566. tmp <<= 16;
  2567. b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
  2568. B43legacy_RADIOCTL_ID);
  2569. tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
  2570. }
  2571. radio_manuf = (tmp & 0x00000FFF);
  2572. radio_ver = (tmp & 0x0FFFF000) >> 12;
  2573. radio_rev = (tmp & 0xF0000000) >> 28;
  2574. switch (phy_type) {
  2575. case B43legacy_PHYTYPE_B:
  2576. if ((radio_ver & 0xFFF0) != 0x2050)
  2577. unsupported = 1;
  2578. break;
  2579. case B43legacy_PHYTYPE_G:
  2580. if (radio_ver != 0x2050)
  2581. unsupported = 1;
  2582. break;
  2583. default:
  2584. B43legacy_BUG_ON(1);
  2585. }
  2586. if (unsupported) {
  2587. b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
  2588. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  2589. radio_manuf, radio_ver, radio_rev);
  2590. return -EOPNOTSUPP;
  2591. }
  2592. b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
  2593. " Revision %u\n", radio_manuf, radio_ver, radio_rev);
  2594. phy->radio_manuf = radio_manuf;
  2595. phy->radio_ver = radio_ver;
  2596. phy->radio_rev = radio_rev;
  2597. phy->analog = analog_type;
  2598. phy->type = phy_type;
  2599. phy->rev = phy_rev;
  2600. return 0;
  2601. }
  2602. static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
  2603. struct b43legacy_phy *phy)
  2604. {
  2605. struct b43legacy_lopair *lo;
  2606. int i;
  2607. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2608. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2609. /* Assume the radio is enabled. If it's not enabled, the state will
  2610. * immediately get fixed on the first periodic work run. */
  2611. dev->radio_hw_enable = 1;
  2612. phy->savedpctlreg = 0xFFFF;
  2613. phy->aci_enable = 0;
  2614. phy->aci_wlan_automatic = 0;
  2615. phy->aci_hw_rssi = 0;
  2616. lo = phy->_lo_pairs;
  2617. if (lo)
  2618. memset(lo, 0, sizeof(struct b43legacy_lopair) *
  2619. B43legacy_LO_COUNT);
  2620. phy->max_lb_gain = 0;
  2621. phy->trsw_rx_gain = 0;
  2622. /* Set default attenuation values. */
  2623. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2624. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2625. phy->txctl1 = b43legacy_default_txctl1(dev);
  2626. phy->txpwr_offset = 0;
  2627. /* NRSSI */
  2628. phy->nrssislope = 0;
  2629. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2630. phy->nrssi[i] = -1000;
  2631. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2632. phy->nrssi_lt[i] = i;
  2633. phy->lofcal = 0xFFFF;
  2634. phy->initval = 0xFFFF;
  2635. phy->interfmode = B43legacy_INTERFMODE_NONE;
  2636. phy->channel = 0xFF;
  2637. }
  2638. static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
  2639. {
  2640. /* Flags */
  2641. dev->dfq_valid = 0;
  2642. /* Stats */
  2643. memset(&dev->stats, 0, sizeof(dev->stats));
  2644. setup_struct_phy_for_init(dev, &dev->phy);
  2645. /* IRQ related flags */
  2646. dev->irq_reason = 0;
  2647. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  2648. dev->irq_savedstate = B43legacy_IRQ_MASKTEMPLATE;
  2649. dev->mac_suspended = 1;
  2650. /* Noise calculation context */
  2651. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  2652. }
  2653. static void b43legacy_imcfglo_timeouts_workaround(struct b43legacy_wldev *dev)
  2654. {
  2655. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2656. struct ssb_bus *bus = dev->dev->bus;
  2657. u32 tmp;
  2658. if (bus->pcicore.dev &&
  2659. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  2660. bus->pcicore.dev->id.revision <= 5) {
  2661. /* IMCFGLO timeouts workaround. */
  2662. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  2663. tmp &= ~SSB_IMCFGLO_REQTO;
  2664. tmp &= ~SSB_IMCFGLO_SERTO;
  2665. switch (bus->bustype) {
  2666. case SSB_BUSTYPE_PCI:
  2667. case SSB_BUSTYPE_PCMCIA:
  2668. tmp |= 0x32;
  2669. break;
  2670. case SSB_BUSTYPE_SSB:
  2671. tmp |= 0x53;
  2672. break;
  2673. }
  2674. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  2675. }
  2676. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  2677. }
  2678. static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
  2679. bool idle) {
  2680. u16 pu_delay = 1050;
  2681. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  2682. pu_delay = 500;
  2683. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  2684. pu_delay = max(pu_delay, (u16)2400);
  2685. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2686. B43legacy_SHM_SH_SPUWKUP, pu_delay);
  2687. }
  2688. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  2689. static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
  2690. {
  2691. u16 pretbtt;
  2692. /* The time value is in microseconds. */
  2693. if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  2694. pretbtt = 2;
  2695. else
  2696. pretbtt = 250;
  2697. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2698. B43legacy_SHM_SH_PRETBTT, pretbtt);
  2699. b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
  2700. }
  2701. /* Shutdown a wireless core */
  2702. /* Locking: wl->mutex */
  2703. static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
  2704. {
  2705. struct b43legacy_phy *phy = &dev->phy;
  2706. u32 macctl;
  2707. B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
  2708. if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
  2709. return;
  2710. b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
  2711. /* Stop the microcode PSM. */
  2712. macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2713. macctl &= ~B43legacy_MACCTL_PSM_RUN;
  2714. macctl |= B43legacy_MACCTL_PSM_JMP0;
  2715. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
  2716. b43legacy_leds_exit(dev);
  2717. b43legacy_rng_exit(dev->wl);
  2718. b43legacy_pio_free(dev);
  2719. b43legacy_dma_free(dev);
  2720. b43legacy_chip_exit(dev);
  2721. b43legacy_radio_turn_off(dev, 1);
  2722. b43legacy_switch_analog(dev, 0);
  2723. if (phy->dyn_tssi_tbl)
  2724. kfree(phy->tssi2dbm);
  2725. kfree(phy->lo_control);
  2726. phy->lo_control = NULL;
  2727. if (dev->wl->current_beacon) {
  2728. dev_kfree_skb_any(dev->wl->current_beacon);
  2729. dev->wl->current_beacon = NULL;
  2730. }
  2731. ssb_device_disable(dev->dev, 0);
  2732. ssb_bus_may_powerdown(dev->dev->bus);
  2733. }
  2734. static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
  2735. {
  2736. struct b43legacy_phy *phy = &dev->phy;
  2737. int i;
  2738. /* Set default attenuation values. */
  2739. phy->bbatt = b43legacy_default_baseband_attenuation(dev);
  2740. phy->rfatt = b43legacy_default_radio_attenuation(dev);
  2741. phy->txctl1 = b43legacy_default_txctl1(dev);
  2742. phy->txctl2 = 0xFFFF;
  2743. phy->txpwr_offset = 0;
  2744. /* NRSSI */
  2745. phy->nrssislope = 0;
  2746. for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
  2747. phy->nrssi[i] = -1000;
  2748. for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
  2749. phy->nrssi_lt[i] = i;
  2750. phy->lofcal = 0xFFFF;
  2751. phy->initval = 0xFFFF;
  2752. phy->aci_enable = 0;
  2753. phy->aci_wlan_automatic = 0;
  2754. phy->aci_hw_rssi = 0;
  2755. phy->antenna_diversity = 0xFFFF;
  2756. memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
  2757. memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
  2758. /* Flags */
  2759. phy->calibrated = 0;
  2760. if (phy->_lo_pairs)
  2761. memset(phy->_lo_pairs, 0,
  2762. sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
  2763. memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
  2764. }
  2765. /* Initialize a wireless core */
  2766. static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
  2767. {
  2768. struct b43legacy_wl *wl = dev->wl;
  2769. struct ssb_bus *bus = dev->dev->bus;
  2770. struct b43legacy_phy *phy = &dev->phy;
  2771. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  2772. int err;
  2773. u32 hf;
  2774. u32 tmp;
  2775. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2776. err = ssb_bus_powerup(bus, 0);
  2777. if (err)
  2778. goto out;
  2779. if (!ssb_device_is_enabled(dev->dev)) {
  2780. tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
  2781. b43legacy_wireless_core_reset(dev, tmp);
  2782. }
  2783. if ((phy->type == B43legacy_PHYTYPE_B) ||
  2784. (phy->type == B43legacy_PHYTYPE_G)) {
  2785. phy->_lo_pairs = kzalloc(sizeof(struct b43legacy_lopair)
  2786. * B43legacy_LO_COUNT,
  2787. GFP_KERNEL);
  2788. if (!phy->_lo_pairs)
  2789. return -ENOMEM;
  2790. }
  2791. setup_struct_wldev_for_init(dev);
  2792. err = b43legacy_phy_init_tssi2dbm_table(dev);
  2793. if (err)
  2794. goto err_kfree_lo_control;
  2795. /* Enable IRQ routing to this device. */
  2796. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  2797. b43legacy_imcfglo_timeouts_workaround(dev);
  2798. prepare_phy_data_for_init(dev);
  2799. b43legacy_phy_calibrate(dev);
  2800. err = b43legacy_chip_init(dev);
  2801. if (err)
  2802. goto err_kfree_tssitbl;
  2803. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2804. B43legacy_SHM_SH_WLCOREREV,
  2805. dev->dev->id.revision);
  2806. hf = b43legacy_hf_read(dev);
  2807. if (phy->type == B43legacy_PHYTYPE_G) {
  2808. hf |= B43legacy_HF_SYMW;
  2809. if (phy->rev == 1)
  2810. hf |= B43legacy_HF_GDCW;
  2811. if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
  2812. hf |= B43legacy_HF_OFDMPABOOST;
  2813. } else if (phy->type == B43legacy_PHYTYPE_B) {
  2814. hf |= B43legacy_HF_SYMW;
  2815. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  2816. hf &= ~B43legacy_HF_GDCW;
  2817. }
  2818. b43legacy_hf_write(dev, hf);
  2819. b43legacy_set_retry_limits(dev,
  2820. B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
  2821. B43legacy_DEFAULT_LONG_RETRY_LIMIT);
  2822. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2823. 0x0044, 3);
  2824. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2825. 0x0046, 2);
  2826. /* Disable sending probe responses from firmware.
  2827. * Setting the MaxTime to one usec will always trigger
  2828. * a timeout, so we never send any probe resp.
  2829. * A timeout of zero is infinite. */
  2830. b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
  2831. B43legacy_SHM_SH_PRMAXTIME, 1);
  2832. b43legacy_rate_memory_init(dev);
  2833. /* Minimum Contention Window */
  2834. if (phy->type == B43legacy_PHYTYPE_B)
  2835. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2836. 0x0003, 31);
  2837. else
  2838. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2839. 0x0003, 15);
  2840. /* Maximum Contention Window */
  2841. b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
  2842. 0x0004, 1023);
  2843. do {
  2844. if (b43legacy_using_pio(dev))
  2845. err = b43legacy_pio_init(dev);
  2846. else {
  2847. err = b43legacy_dma_init(dev);
  2848. if (!err)
  2849. b43legacy_qos_init(dev);
  2850. }
  2851. } while (err == -EAGAIN);
  2852. if (err)
  2853. goto err_chip_exit;
  2854. b43legacy_set_synth_pu_delay(dev, 1);
  2855. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  2856. b43legacy_upload_card_macaddress(dev);
  2857. b43legacy_security_init(dev);
  2858. b43legacy_rng_init(wl);
  2859. b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
  2860. b43legacy_leds_init(dev);
  2861. out:
  2862. return err;
  2863. err_chip_exit:
  2864. b43legacy_chip_exit(dev);
  2865. err_kfree_tssitbl:
  2866. if (phy->dyn_tssi_tbl)
  2867. kfree(phy->tssi2dbm);
  2868. err_kfree_lo_control:
  2869. kfree(phy->lo_control);
  2870. phy->lo_control = NULL;
  2871. ssb_bus_may_powerdown(bus);
  2872. B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
  2873. return err;
  2874. }
  2875. static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
  2876. struct ieee80211_if_init_conf *conf)
  2877. {
  2878. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2879. struct b43legacy_wldev *dev;
  2880. unsigned long flags;
  2881. int err = -EOPNOTSUPP;
  2882. /* TODO: allow WDS/AP devices to coexist */
  2883. if (conf->type != NL80211_IFTYPE_AP &&
  2884. conf->type != NL80211_IFTYPE_STATION &&
  2885. conf->type != NL80211_IFTYPE_WDS &&
  2886. conf->type != NL80211_IFTYPE_ADHOC)
  2887. return -EOPNOTSUPP;
  2888. mutex_lock(&wl->mutex);
  2889. if (wl->operating)
  2890. goto out_mutex_unlock;
  2891. b43legacydbg(wl, "Adding Interface type %d\n", conf->type);
  2892. dev = wl->current_dev;
  2893. wl->operating = 1;
  2894. wl->vif = conf->vif;
  2895. wl->if_type = conf->type;
  2896. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  2897. spin_lock_irqsave(&wl->irq_lock, flags);
  2898. b43legacy_adjust_opmode(dev);
  2899. b43legacy_set_pretbtt(dev);
  2900. b43legacy_set_synth_pu_delay(dev, 0);
  2901. b43legacy_upload_card_macaddress(dev);
  2902. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2903. err = 0;
  2904. out_mutex_unlock:
  2905. mutex_unlock(&wl->mutex);
  2906. return err;
  2907. }
  2908. static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
  2909. struct ieee80211_if_init_conf *conf)
  2910. {
  2911. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2912. struct b43legacy_wldev *dev = wl->current_dev;
  2913. unsigned long flags;
  2914. b43legacydbg(wl, "Removing Interface type %d\n", conf->type);
  2915. mutex_lock(&wl->mutex);
  2916. B43legacy_WARN_ON(!wl->operating);
  2917. B43legacy_WARN_ON(wl->vif != conf->vif);
  2918. wl->vif = NULL;
  2919. wl->operating = 0;
  2920. spin_lock_irqsave(&wl->irq_lock, flags);
  2921. b43legacy_adjust_opmode(dev);
  2922. memset(wl->mac_addr, 0, ETH_ALEN);
  2923. b43legacy_upload_card_macaddress(dev);
  2924. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2925. mutex_unlock(&wl->mutex);
  2926. }
  2927. static int b43legacy_op_start(struct ieee80211_hw *hw)
  2928. {
  2929. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2930. struct b43legacy_wldev *dev = wl->current_dev;
  2931. int did_init = 0;
  2932. int err = 0;
  2933. bool do_rfkill_exit = 0;
  2934. /* First register RFkill.
  2935. * LEDs that are registered later depend on it. */
  2936. b43legacy_rfkill_init(dev);
  2937. /* Kill all old instance specific information to make sure
  2938. * the card won't use it in the short timeframe between start
  2939. * and mac80211 reconfiguring it. */
  2940. memset(wl->bssid, 0, ETH_ALEN);
  2941. memset(wl->mac_addr, 0, ETH_ALEN);
  2942. wl->filter_flags = 0;
  2943. mutex_lock(&wl->mutex);
  2944. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
  2945. err = b43legacy_wireless_core_init(dev);
  2946. if (err) {
  2947. do_rfkill_exit = 1;
  2948. goto out_mutex_unlock;
  2949. }
  2950. did_init = 1;
  2951. }
  2952. if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
  2953. err = b43legacy_wireless_core_start(dev);
  2954. if (err) {
  2955. if (did_init)
  2956. b43legacy_wireless_core_exit(dev);
  2957. do_rfkill_exit = 1;
  2958. goto out_mutex_unlock;
  2959. }
  2960. }
  2961. out_mutex_unlock:
  2962. mutex_unlock(&wl->mutex);
  2963. if (do_rfkill_exit)
  2964. b43legacy_rfkill_exit(dev);
  2965. return err;
  2966. }
  2967. static void b43legacy_op_stop(struct ieee80211_hw *hw)
  2968. {
  2969. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2970. struct b43legacy_wldev *dev = wl->current_dev;
  2971. b43legacy_rfkill_exit(dev);
  2972. mutex_lock(&wl->mutex);
  2973. if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
  2974. b43legacy_wireless_core_stop(dev);
  2975. b43legacy_wireless_core_exit(dev);
  2976. mutex_unlock(&wl->mutex);
  2977. }
  2978. static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
  2979. struct ieee80211_sta *sta, bool set)
  2980. {
  2981. struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
  2982. unsigned long flags;
  2983. spin_lock_irqsave(&wl->irq_lock, flags);
  2984. b43legacy_update_templates(wl);
  2985. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2986. return 0;
  2987. }
  2988. static const struct ieee80211_ops b43legacy_hw_ops = {
  2989. .tx = b43legacy_op_tx,
  2990. .conf_tx = b43legacy_op_conf_tx,
  2991. .add_interface = b43legacy_op_add_interface,
  2992. .remove_interface = b43legacy_op_remove_interface,
  2993. .config = b43legacy_op_dev_config,
  2994. .bss_info_changed = b43legacy_op_bss_info_changed,
  2995. .config_interface = b43legacy_op_config_interface,
  2996. .configure_filter = b43legacy_op_configure_filter,
  2997. .get_stats = b43legacy_op_get_stats,
  2998. .get_tx_stats = b43legacy_op_get_tx_stats,
  2999. .start = b43legacy_op_start,
  3000. .stop = b43legacy_op_stop,
  3001. .set_tim = b43legacy_op_beacon_set_tim,
  3002. };
  3003. /* Hard-reset the chip. Do not call this directly.
  3004. * Use b43legacy_controller_restart()
  3005. */
  3006. static void b43legacy_chip_reset(struct work_struct *work)
  3007. {
  3008. struct b43legacy_wldev *dev =
  3009. container_of(work, struct b43legacy_wldev, restart_work);
  3010. struct b43legacy_wl *wl = dev->wl;
  3011. int err = 0;
  3012. int prev_status;
  3013. mutex_lock(&wl->mutex);
  3014. prev_status = b43legacy_status(dev);
  3015. /* Bring the device down... */
  3016. if (prev_status >= B43legacy_STAT_STARTED)
  3017. b43legacy_wireless_core_stop(dev);
  3018. if (prev_status >= B43legacy_STAT_INITIALIZED)
  3019. b43legacy_wireless_core_exit(dev);
  3020. /* ...and up again. */
  3021. if (prev_status >= B43legacy_STAT_INITIALIZED) {
  3022. err = b43legacy_wireless_core_init(dev);
  3023. if (err)
  3024. goto out;
  3025. }
  3026. if (prev_status >= B43legacy_STAT_STARTED) {
  3027. err = b43legacy_wireless_core_start(dev);
  3028. if (err) {
  3029. b43legacy_wireless_core_exit(dev);
  3030. goto out;
  3031. }
  3032. }
  3033. out:
  3034. if (err)
  3035. wl->current_dev = NULL; /* Failed to init the dev. */
  3036. mutex_unlock(&wl->mutex);
  3037. if (err)
  3038. b43legacyerr(wl, "Controller restart FAILED\n");
  3039. else
  3040. b43legacyinfo(wl, "Controller restarted\n");
  3041. }
  3042. static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
  3043. int have_bphy,
  3044. int have_gphy)
  3045. {
  3046. struct ieee80211_hw *hw = dev->wl->hw;
  3047. struct b43legacy_phy *phy = &dev->phy;
  3048. phy->possible_phymodes = 0;
  3049. if (have_bphy) {
  3050. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3051. &b43legacy_band_2GHz_BPHY;
  3052. phy->possible_phymodes |= B43legacy_PHYMODE_B;
  3053. }
  3054. if (have_gphy) {
  3055. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3056. &b43legacy_band_2GHz_GPHY;
  3057. phy->possible_phymodes |= B43legacy_PHYMODE_G;
  3058. }
  3059. return 0;
  3060. }
  3061. static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
  3062. {
  3063. /* We release firmware that late to not be required to re-request
  3064. * is all the time when we reinit the core. */
  3065. b43legacy_release_firmware(dev);
  3066. }
  3067. static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
  3068. {
  3069. struct b43legacy_wl *wl = dev->wl;
  3070. struct ssb_bus *bus = dev->dev->bus;
  3071. struct pci_dev *pdev = bus->host_pci;
  3072. int err;
  3073. int have_bphy = 0;
  3074. int have_gphy = 0;
  3075. u32 tmp;
  3076. /* Do NOT do any device initialization here.
  3077. * Do it in wireless_core_init() instead.
  3078. * This function is for gathering basic information about the HW, only.
  3079. * Also some structs may be set up here. But most likely you want to
  3080. * have that in core_init(), too.
  3081. */
  3082. err = ssb_bus_powerup(bus, 0);
  3083. if (err) {
  3084. b43legacyerr(wl, "Bus powerup failed\n");
  3085. goto out;
  3086. }
  3087. /* Get the PHY type. */
  3088. if (dev->dev->id.revision >= 5) {
  3089. u32 tmshigh;
  3090. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3091. have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
  3092. if (!have_gphy)
  3093. have_bphy = 1;
  3094. } else if (dev->dev->id.revision == 4)
  3095. have_gphy = 1;
  3096. else
  3097. have_bphy = 1;
  3098. dev->phy.gmode = (have_gphy || have_bphy);
  3099. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3100. b43legacy_wireless_core_reset(dev, tmp);
  3101. err = b43legacy_phy_versioning(dev);
  3102. if (err)
  3103. goto err_powerdown;
  3104. /* Check if this device supports multiband. */
  3105. if (!pdev ||
  3106. (pdev->device != 0x4312 &&
  3107. pdev->device != 0x4319 &&
  3108. pdev->device != 0x4324)) {
  3109. /* No multiband support. */
  3110. have_bphy = 0;
  3111. have_gphy = 0;
  3112. switch (dev->phy.type) {
  3113. case B43legacy_PHYTYPE_B:
  3114. have_bphy = 1;
  3115. break;
  3116. case B43legacy_PHYTYPE_G:
  3117. have_gphy = 1;
  3118. break;
  3119. default:
  3120. B43legacy_BUG_ON(1);
  3121. }
  3122. }
  3123. dev->phy.gmode = (have_gphy || have_bphy);
  3124. tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
  3125. b43legacy_wireless_core_reset(dev, tmp);
  3126. err = b43legacy_validate_chipaccess(dev);
  3127. if (err)
  3128. goto err_powerdown;
  3129. err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
  3130. if (err)
  3131. goto err_powerdown;
  3132. /* Now set some default "current_dev" */
  3133. if (!wl->current_dev)
  3134. wl->current_dev = dev;
  3135. INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
  3136. b43legacy_radio_turn_off(dev, 1);
  3137. b43legacy_switch_analog(dev, 0);
  3138. ssb_device_disable(dev->dev, 0);
  3139. ssb_bus_may_powerdown(bus);
  3140. out:
  3141. return err;
  3142. err_powerdown:
  3143. ssb_bus_may_powerdown(bus);
  3144. return err;
  3145. }
  3146. static void b43legacy_one_core_detach(struct ssb_device *dev)
  3147. {
  3148. struct b43legacy_wldev *wldev;
  3149. struct b43legacy_wl *wl;
  3150. /* Do not cancel ieee80211-workqueue based work here.
  3151. * See comment in b43legacy_remove(). */
  3152. wldev = ssb_get_drvdata(dev);
  3153. wl = wldev->wl;
  3154. b43legacy_debugfs_remove_device(wldev);
  3155. b43legacy_wireless_core_detach(wldev);
  3156. list_del(&wldev->list);
  3157. wl->nr_devs--;
  3158. ssb_set_drvdata(dev, NULL);
  3159. kfree(wldev);
  3160. }
  3161. static int b43legacy_one_core_attach(struct ssb_device *dev,
  3162. struct b43legacy_wl *wl)
  3163. {
  3164. struct b43legacy_wldev *wldev;
  3165. struct pci_dev *pdev;
  3166. int err = -ENOMEM;
  3167. if (!list_empty(&wl->devlist)) {
  3168. /* We are not the first core on this chip. */
  3169. pdev = dev->bus->host_pci;
  3170. /* Only special chips support more than one wireless
  3171. * core, although some of the other chips have more than
  3172. * one wireless core as well. Check for this and
  3173. * bail out early.
  3174. */
  3175. if (!pdev ||
  3176. ((pdev->device != 0x4321) &&
  3177. (pdev->device != 0x4313) &&
  3178. (pdev->device != 0x431A))) {
  3179. b43legacydbg(wl, "Ignoring unconnected 802.11 core\n");
  3180. return -ENODEV;
  3181. }
  3182. }
  3183. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3184. if (!wldev)
  3185. goto out;
  3186. wldev->dev = dev;
  3187. wldev->wl = wl;
  3188. b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
  3189. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3190. tasklet_init(&wldev->isr_tasklet,
  3191. (void (*)(unsigned long))b43legacy_interrupt_tasklet,
  3192. (unsigned long)wldev);
  3193. if (modparam_pio)
  3194. wldev->__using_pio = 1;
  3195. INIT_LIST_HEAD(&wldev->list);
  3196. err = b43legacy_wireless_core_attach(wldev);
  3197. if (err)
  3198. goto err_kfree_wldev;
  3199. list_add(&wldev->list, &wl->devlist);
  3200. wl->nr_devs++;
  3201. ssb_set_drvdata(dev, wldev);
  3202. b43legacy_debugfs_add_device(wldev);
  3203. out:
  3204. return err;
  3205. err_kfree_wldev:
  3206. kfree(wldev);
  3207. return err;
  3208. }
  3209. static void b43legacy_sprom_fixup(struct ssb_bus *bus)
  3210. {
  3211. /* boardflags workarounds */
  3212. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3213. bus->boardinfo.type == 0x4E &&
  3214. bus->boardinfo.rev > 0x40)
  3215. bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
  3216. }
  3217. static void b43legacy_wireless_exit(struct ssb_device *dev,
  3218. struct b43legacy_wl *wl)
  3219. {
  3220. struct ieee80211_hw *hw = wl->hw;
  3221. ssb_set_devtypedata(dev, NULL);
  3222. ieee80211_free_hw(hw);
  3223. }
  3224. static int b43legacy_wireless_init(struct ssb_device *dev)
  3225. {
  3226. struct ssb_sprom *sprom = &dev->bus->sprom;
  3227. struct ieee80211_hw *hw;
  3228. struct b43legacy_wl *wl;
  3229. int err = -ENOMEM;
  3230. b43legacy_sprom_fixup(dev->bus);
  3231. hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
  3232. if (!hw) {
  3233. b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
  3234. goto out;
  3235. }
  3236. /* fill hw info */
  3237. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  3238. IEEE80211_HW_SIGNAL_DBM |
  3239. IEEE80211_HW_NOISE_DBM;
  3240. hw->wiphy->interface_modes =
  3241. BIT(NL80211_IFTYPE_AP) |
  3242. BIT(NL80211_IFTYPE_STATION) |
  3243. BIT(NL80211_IFTYPE_WDS) |
  3244. BIT(NL80211_IFTYPE_ADHOC);
  3245. hw->queues = 1; /* FIXME: hardware has more queues */
  3246. hw->max_rates = 2;
  3247. SET_IEEE80211_DEV(hw, dev->dev);
  3248. if (is_valid_ether_addr(sprom->et1mac))
  3249. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  3250. else
  3251. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  3252. /* Get and initialize struct b43legacy_wl */
  3253. wl = hw_to_b43legacy_wl(hw);
  3254. memset(wl, 0, sizeof(*wl));
  3255. wl->hw = hw;
  3256. spin_lock_init(&wl->irq_lock);
  3257. spin_lock_init(&wl->leds_lock);
  3258. mutex_init(&wl->mutex);
  3259. INIT_LIST_HEAD(&wl->devlist);
  3260. ssb_set_devtypedata(dev, wl);
  3261. b43legacyinfo(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  3262. err = 0;
  3263. out:
  3264. return err;
  3265. }
  3266. static int b43legacy_probe(struct ssb_device *dev,
  3267. const struct ssb_device_id *id)
  3268. {
  3269. struct b43legacy_wl *wl;
  3270. int err;
  3271. int first = 0;
  3272. wl = ssb_get_devtypedata(dev);
  3273. if (!wl) {
  3274. /* Probing the first core - setup common struct b43legacy_wl */
  3275. first = 1;
  3276. err = b43legacy_wireless_init(dev);
  3277. if (err)
  3278. goto out;
  3279. wl = ssb_get_devtypedata(dev);
  3280. B43legacy_WARN_ON(!wl);
  3281. }
  3282. err = b43legacy_one_core_attach(dev, wl);
  3283. if (err)
  3284. goto err_wireless_exit;
  3285. if (first) {
  3286. err = ieee80211_register_hw(wl->hw);
  3287. if (err)
  3288. goto err_one_core_detach;
  3289. }
  3290. out:
  3291. return err;
  3292. err_one_core_detach:
  3293. b43legacy_one_core_detach(dev);
  3294. err_wireless_exit:
  3295. if (first)
  3296. b43legacy_wireless_exit(dev, wl);
  3297. return err;
  3298. }
  3299. static void b43legacy_remove(struct ssb_device *dev)
  3300. {
  3301. struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
  3302. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3303. /* We must cancel any work here before unregistering from ieee80211,
  3304. * as the ieee80211 unreg will destroy the workqueue. */
  3305. cancel_work_sync(&wldev->restart_work);
  3306. B43legacy_WARN_ON(!wl);
  3307. if (wl->current_dev == wldev)
  3308. ieee80211_unregister_hw(wl->hw);
  3309. b43legacy_one_core_detach(dev);
  3310. if (list_empty(&wl->devlist))
  3311. /* Last core on the chip unregistered.
  3312. * We can destroy common struct b43legacy_wl.
  3313. */
  3314. b43legacy_wireless_exit(dev, wl);
  3315. }
  3316. /* Perform a hardware reset. This can be called from any context. */
  3317. void b43legacy_controller_restart(struct b43legacy_wldev *dev,
  3318. const char *reason)
  3319. {
  3320. /* Must avoid requeueing, if we are in shutdown. */
  3321. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
  3322. return;
  3323. b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
  3324. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  3325. }
  3326. #ifdef CONFIG_PM
  3327. static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
  3328. {
  3329. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3330. struct b43legacy_wl *wl = wldev->wl;
  3331. b43legacydbg(wl, "Suspending...\n");
  3332. mutex_lock(&wl->mutex);
  3333. wldev->suspend_init_status = b43legacy_status(wldev);
  3334. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
  3335. b43legacy_wireless_core_stop(wldev);
  3336. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
  3337. b43legacy_wireless_core_exit(wldev);
  3338. mutex_unlock(&wl->mutex);
  3339. b43legacydbg(wl, "Device suspended.\n");
  3340. return 0;
  3341. }
  3342. static int b43legacy_resume(struct ssb_device *dev)
  3343. {
  3344. struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
  3345. struct b43legacy_wl *wl = wldev->wl;
  3346. int err = 0;
  3347. b43legacydbg(wl, "Resuming...\n");
  3348. mutex_lock(&wl->mutex);
  3349. if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
  3350. err = b43legacy_wireless_core_init(wldev);
  3351. if (err) {
  3352. b43legacyerr(wl, "Resume failed at core init\n");
  3353. goto out;
  3354. }
  3355. }
  3356. if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
  3357. err = b43legacy_wireless_core_start(wldev);
  3358. if (err) {
  3359. b43legacy_wireless_core_exit(wldev);
  3360. b43legacyerr(wl, "Resume failed at core start\n");
  3361. goto out;
  3362. }
  3363. }
  3364. b43legacydbg(wl, "Device resumed.\n");
  3365. out:
  3366. mutex_unlock(&wl->mutex);
  3367. return err;
  3368. }
  3369. #else /* CONFIG_PM */
  3370. # define b43legacy_suspend NULL
  3371. # define b43legacy_resume NULL
  3372. #endif /* CONFIG_PM */
  3373. static struct ssb_driver b43legacy_ssb_driver = {
  3374. .name = KBUILD_MODNAME,
  3375. .id_table = b43legacy_ssb_tbl,
  3376. .probe = b43legacy_probe,
  3377. .remove = b43legacy_remove,
  3378. .suspend = b43legacy_suspend,
  3379. .resume = b43legacy_resume,
  3380. };
  3381. static void b43legacy_print_driverinfo(void)
  3382. {
  3383. const char *feat_pci = "", *feat_leds = "", *feat_rfkill = "",
  3384. *feat_pio = "", *feat_dma = "";
  3385. #ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
  3386. feat_pci = "P";
  3387. #endif
  3388. #ifdef CONFIG_B43LEGACY_LEDS
  3389. feat_leds = "L";
  3390. #endif
  3391. #ifdef CONFIG_B43LEGACY_RFKILL
  3392. feat_rfkill = "R";
  3393. #endif
  3394. #ifdef CONFIG_B43LEGACY_PIO
  3395. feat_pio = "I";
  3396. #endif
  3397. #ifdef CONFIG_B43LEGACY_DMA
  3398. feat_dma = "D";
  3399. #endif
  3400. printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
  3401. "[ Features: %s%s%s%s%s, Firmware-ID: "
  3402. B43legacy_SUPPORTED_FIRMWARE_ID " ]\n",
  3403. feat_pci, feat_leds, feat_rfkill, feat_pio, feat_dma);
  3404. }
  3405. static int __init b43legacy_init(void)
  3406. {
  3407. int err;
  3408. b43legacy_debugfs_init();
  3409. err = ssb_driver_register(&b43legacy_ssb_driver);
  3410. if (err)
  3411. goto err_dfs_exit;
  3412. b43legacy_print_driverinfo();
  3413. return err;
  3414. err_dfs_exit:
  3415. b43legacy_debugfs_exit();
  3416. return err;
  3417. }
  3418. static void __exit b43legacy_exit(void)
  3419. {
  3420. ssb_driver_unregister(&b43legacy_ssb_driver);
  3421. b43legacy_debugfs_exit();
  3422. }
  3423. module_init(b43legacy_init)
  3424. module_exit(b43legacy_exit)