main.c 102 KB

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