main.c 104 KB

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