main.c 105 KB

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