asus-wmi.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Asus PC WMI hotkey driver
  4. *
  5. * Copyright(C) 2010 Intel Corporation.
  6. * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
  7. *
  8. * Portions based on wistron_btns.c:
  9. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  10. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  11. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/acpi.h>
  15. #include <linux/backlight.h>
  16. #include <linux/bits.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/delay.h>
  19. #include <linux/dmi.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/init.h>
  23. #include <linux/input.h>
  24. #include <linux/input/sparse-keymap.h>
  25. #include <linux/kernel.h>
  26. #include <linux/leds.h>
  27. #include <linux/minmax.h>
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_hotplug.h>
  31. #include <linux/platform_data/x86/asus-wmi.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/platform_profile.h>
  34. #include <linux/power_supply.h>
  35. #include <linux/rfkill.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/slab.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/types.h>
  40. #include <linux/units.h>
  41. #include <acpi/battery.h>
  42. #include <acpi/video.h>
  43. #include "asus-wmi.h"
  44. MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
  45. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  46. MODULE_DESCRIPTION("Asus Generic WMI Driver");
  47. MODULE_LICENSE("GPL");
  48. static bool fnlock_default = true;
  49. module_param(fnlock_default, bool, 0444);
  50. #define to_asus_wmi_driver(pdrv) \
  51. (container_of((pdrv), struct asus_wmi_driver, platform_driver))
  52. #define NOTIFY_BRNUP_MIN 0x11
  53. #define NOTIFY_BRNUP_MAX 0x1f
  54. #define NOTIFY_BRNDOWN_MIN 0x20
  55. #define NOTIFY_BRNDOWN_MAX 0x2e
  56. #define NOTIFY_FNLOCK_TOGGLE 0x4e
  57. #define NOTIFY_KBD_DOCK_CHANGE 0x75
  58. #define NOTIFY_KBD_BRTUP 0xc4
  59. #define NOTIFY_KBD_BRTDWN 0xc5
  60. #define NOTIFY_KBD_BRTTOGGLE 0xc7
  61. #define NOTIFY_KBD_FBM 0x99
  62. #define NOTIFY_KBD_TTP 0xae
  63. #define NOTIFY_LID_FLIP 0xfa
  64. #define NOTIFY_LID_FLIP_ROG 0xbd
  65. #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
  66. #define ASUS_MID_FAN_DESC "mid_fan"
  67. #define ASUS_GPU_FAN_DESC "gpu_fan"
  68. #define ASUS_FAN_DESC "cpu_fan"
  69. #define ASUS_FAN_MFUN 0x13
  70. #define ASUS_FAN_SFUN_READ 0x06
  71. #define ASUS_FAN_SFUN_WRITE 0x07
  72. /* Based on standard hwmon pwmX_enable values */
  73. #define ASUS_FAN_CTRL_FULLSPEED 0
  74. #define ASUS_FAN_CTRL_MANUAL 1
  75. #define ASUS_FAN_CTRL_AUTO 2
  76. #define ASUS_FAN_BOOST_MODE_NORMAL 0
  77. #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
  78. #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
  79. #define ASUS_FAN_BOOST_MODE_SILENT 2
  80. #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
  81. #define ASUS_FAN_BOOST_MODES_MASK 0x03
  82. #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
  83. #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
  84. #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
  85. #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT_VIVO 0
  86. #define ASUS_THROTTLE_THERMAL_POLICY_SILENT_VIVO 1
  87. #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST_VIVO 2
  88. #define PLATFORM_PROFILE_MAX 2
  89. #define USB_INTEL_XUSB2PR 0xD0
  90. #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
  91. #define WMI_EVENT_MASK 0xFFFF
  92. #define FAN_CURVE_POINTS 8
  93. #define FAN_CURVE_BUF_LEN 32
  94. #define FAN_CURVE_DEV_CPU 0x00
  95. #define FAN_CURVE_DEV_GPU 0x01
  96. #define FAN_CURVE_DEV_MID 0x02
  97. /* Mask to determine if setting temperature or percentage */
  98. #define FAN_CURVE_PWM_MASK 0x04
  99. /* Limits for tunables available on ASUS ROG laptops */
  100. #define PPT_TOTAL_MIN 5
  101. #define PPT_TOTAL_MAX 250
  102. #define PPT_CPU_MIN 5
  103. #define PPT_CPU_MAX 130
  104. #define NVIDIA_BOOST_MIN 5
  105. #define NVIDIA_BOOST_MAX 25
  106. #define NVIDIA_TEMP_MIN 75
  107. #define NVIDIA_TEMP_MAX 87
  108. #define ASUS_SCREENPAD_BRIGHT_MIN 20
  109. #define ASUS_SCREENPAD_BRIGHT_MAX 255
  110. #define ASUS_SCREENPAD_BRIGHT_DEFAULT 60
  111. #define ASUS_MINI_LED_MODE_MASK 0x03
  112. /* Standard modes for devices with only on/off */
  113. #define ASUS_MINI_LED_OFF 0x00
  114. #define ASUS_MINI_LED_ON 0x01
  115. /* New mode on some devices, define here to clarify remapping later */
  116. #define ASUS_MINI_LED_STRONG_MODE 0x02
  117. /* New modes for devices with 3 mini-led mode types */
  118. #define ASUS_MINI_LED_2024_WEAK 0x00
  119. #define ASUS_MINI_LED_2024_STRONG 0x01
  120. #define ASUS_MINI_LED_2024_OFF 0x02
  121. #define ASUS_USB0_PWR_EC0_CSEE "\\_SB.PCI0.SBRG.EC0.CSEE"
  122. /*
  123. * The period required to wait after screen off/on/s2idle.check in MS.
  124. * Time here greatly impacts the wake behaviour. Used in suspend/wake.
  125. */
  126. #define ASUS_USB0_PWR_EC0_CSEE_WAIT 600
  127. #define ASUS_USB0_PWR_EC0_CSEE_OFF 0xB7
  128. #define ASUS_USB0_PWR_EC0_CSEE_ON 0xB8
  129. static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
  130. static int throttle_thermal_policy_write(struct asus_wmi *);
  131. static const struct dmi_system_id asus_rog_ally_device[] = {
  132. {
  133. .matches = {
  134. DMI_MATCH(DMI_BOARD_NAME, "RC71L"),
  135. },
  136. },
  137. {
  138. .matches = {
  139. DMI_MATCH(DMI_BOARD_NAME, "RC72L"),
  140. },
  141. },
  142. { },
  143. };
  144. static bool ashs_present(void)
  145. {
  146. int i = 0;
  147. while (ashs_ids[i]) {
  148. if (acpi_dev_found(ashs_ids[i++]))
  149. return true;
  150. }
  151. return false;
  152. }
  153. struct bios_args {
  154. u32 arg0;
  155. u32 arg1;
  156. u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
  157. u32 arg3;
  158. u32 arg4; /* Some ROG laptops require a full 5 input args */
  159. u32 arg5;
  160. } __packed;
  161. /*
  162. * Struct that's used for all methods called via AGFN. Naming is
  163. * identically to the AML code.
  164. */
  165. struct agfn_args {
  166. u16 mfun; /* probably "Multi-function" to be called */
  167. u16 sfun; /* probably "Sub-function" to be called */
  168. u16 len; /* size of the hole struct, including subfunction fields */
  169. u8 stas; /* not used by now */
  170. u8 err; /* zero on success */
  171. } __packed;
  172. /* struct used for calling fan read and write methods */
  173. struct agfn_fan_args {
  174. struct agfn_args agfn; /* common fields */
  175. u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
  176. u32 speed; /* read: RPM/100 - write: 0-255 */
  177. } __packed;
  178. /*
  179. * <platform>/ - debugfs root directory
  180. * dev_id - current dev_id
  181. * ctrl_param - current ctrl_param
  182. * method_id - current method_id
  183. * devs - call DEVS(dev_id, ctrl_param) and print result
  184. * dsts - call DSTS(dev_id) and print result
  185. * call - call method_id(dev_id, ctrl_param) and print result
  186. */
  187. struct asus_wmi_debug {
  188. struct dentry *root;
  189. u32 method_id;
  190. u32 dev_id;
  191. u32 ctrl_param;
  192. };
  193. struct asus_rfkill {
  194. struct asus_wmi *asus;
  195. struct rfkill *rfkill;
  196. u32 dev_id;
  197. };
  198. enum fan_type {
  199. FAN_TYPE_NONE = 0,
  200. FAN_TYPE_AGFN, /* deprecated on newer platforms */
  201. FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
  202. };
  203. struct fan_curve_data {
  204. bool enabled;
  205. u32 device_id;
  206. u8 temps[FAN_CURVE_POINTS];
  207. u8 percents[FAN_CURVE_POINTS];
  208. };
  209. struct asus_wmi {
  210. int dsts_id;
  211. int spec;
  212. int sfun;
  213. struct input_dev *inputdev;
  214. struct backlight_device *backlight_device;
  215. struct backlight_device *screenpad_backlight_device;
  216. struct platform_device *platform_device;
  217. struct led_classdev wlan_led;
  218. int wlan_led_wk;
  219. struct led_classdev tpd_led;
  220. int tpd_led_wk;
  221. struct led_classdev kbd_led;
  222. int kbd_led_wk;
  223. bool kbd_led_notify;
  224. bool kbd_led_avail;
  225. bool kbd_led_registered;
  226. struct led_classdev lightbar_led;
  227. int lightbar_led_wk;
  228. struct led_classdev micmute_led;
  229. struct led_classdev camera_led;
  230. struct workqueue_struct *led_workqueue;
  231. struct work_struct tpd_led_work;
  232. struct work_struct wlan_led_work;
  233. struct work_struct lightbar_led_work;
  234. struct work_struct kbd_led_work;
  235. struct asus_rfkill wlan;
  236. struct asus_rfkill bluetooth;
  237. struct asus_rfkill wimax;
  238. struct asus_rfkill wwan3g;
  239. struct asus_rfkill gps;
  240. struct asus_rfkill uwb;
  241. int tablet_switch_event_code;
  242. u32 tablet_switch_dev_id;
  243. bool tablet_switch_inverted;
  244. enum fan_type fan_type;
  245. enum fan_type gpu_fan_type;
  246. enum fan_type mid_fan_type;
  247. int fan_pwm_mode;
  248. int gpu_fan_pwm_mode;
  249. int mid_fan_pwm_mode;
  250. int agfn_pwm;
  251. bool fan_boost_mode_available;
  252. u8 fan_boost_mode_mask;
  253. u8 fan_boost_mode;
  254. bool egpu_enable_available;
  255. bool dgpu_disable_available;
  256. u32 gpu_mux_dev;
  257. /* Tunables provided by ASUS for gaming laptops */
  258. u32 ppt_pl2_sppt;
  259. u32 ppt_pl1_spl;
  260. u32 ppt_apu_sppt;
  261. u32 ppt_platform_sppt;
  262. u32 ppt_fppt;
  263. u32 nv_dynamic_boost;
  264. u32 nv_temp_target;
  265. u32 kbd_rgb_dev;
  266. bool kbd_rgb_state_available;
  267. bool oobe_state_available;
  268. u8 throttle_thermal_policy_mode;
  269. u32 throttle_thermal_policy_dev;
  270. bool cpu_fan_curve_available;
  271. bool gpu_fan_curve_available;
  272. bool mid_fan_curve_available;
  273. struct fan_curve_data custom_fan_curves[3];
  274. struct device *ppdev;
  275. bool platform_profile_support;
  276. // The RSOC controls the maximum charging percentage.
  277. bool battery_rsoc_available;
  278. bool panel_overdrive_available;
  279. u32 mini_led_dev_id;
  280. struct hotplug_slot hotplug_slot;
  281. struct mutex hotplug_lock;
  282. struct mutex wmi_lock;
  283. struct workqueue_struct *hotplug_workqueue;
  284. struct work_struct hotplug_work;
  285. bool fnlock_locked;
  286. struct asus_wmi_debug debug;
  287. struct asus_wmi_driver *driver;
  288. };
  289. /* Global to allow setting externally without requiring driver data */
  290. static enum asus_ally_mcu_hack use_ally_mcu_hack = ASUS_WMI_ALLY_MCU_HACK_INIT;
  291. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  292. static void asus_wmi_show_deprecated(void)
  293. {
  294. pr_notice_once("Accessing attributes through /sys/bus/platform/asus_wmi is deprecated and will be removed in a future release. Please switch over to /sys/class/firmware_attributes.\n");
  295. }
  296. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  297. /* WMI ************************************************************************/
  298. static int asus_wmi_evaluate_method3(u32 method_id,
  299. u32 arg0, u32 arg1, u32 arg2, u32 *retval)
  300. {
  301. struct bios_args args = {
  302. .arg0 = arg0,
  303. .arg1 = arg1,
  304. .arg2 = arg2,
  305. };
  306. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  307. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  308. acpi_status status;
  309. union acpi_object *obj;
  310. u32 tmp = 0;
  311. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
  312. &input, &output);
  313. pr_debug("%s called (0x%08x) with args: 0x%08x, 0x%08x, 0x%08x\n",
  314. __func__, method_id, arg0, arg1, arg2);
  315. if (ACPI_FAILURE(status)) {
  316. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  317. __func__, method_id, arg0, -EIO);
  318. return -EIO;
  319. }
  320. obj = (union acpi_object *)output.pointer;
  321. if (obj && obj->type == ACPI_TYPE_INTEGER)
  322. tmp = (u32) obj->integer.value;
  323. pr_debug("Result: 0x%08x\n", tmp);
  324. if (retval)
  325. *retval = tmp;
  326. kfree(obj);
  327. if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) {
  328. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  329. __func__, method_id, arg0, -ENODEV);
  330. return -ENODEV;
  331. }
  332. return 0;
  333. }
  334. int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
  335. {
  336. return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
  337. }
  338. EXPORT_SYMBOL_NS_GPL(asus_wmi_evaluate_method, "ASUS_WMI");
  339. static int asus_wmi_evaluate_method5(u32 method_id,
  340. u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
  341. {
  342. struct bios_args args = {
  343. .arg0 = arg0,
  344. .arg1 = arg1,
  345. .arg2 = arg2,
  346. .arg3 = arg3,
  347. .arg4 = arg4,
  348. };
  349. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  350. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  351. acpi_status status;
  352. union acpi_object *obj;
  353. u32 tmp = 0;
  354. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
  355. &input, &output);
  356. pr_debug("%s called (0x%08x) with args: 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  357. __func__, method_id, arg0, arg1, arg2, arg3, arg4);
  358. if (ACPI_FAILURE(status)) {
  359. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  360. __func__, method_id, arg0, -EIO);
  361. return -EIO;
  362. }
  363. obj = (union acpi_object *)output.pointer;
  364. if (obj && obj->type == ACPI_TYPE_INTEGER)
  365. tmp = (u32) obj->integer.value;
  366. pr_debug("Result: %x\n", tmp);
  367. if (retval)
  368. *retval = tmp;
  369. kfree(obj);
  370. if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) {
  371. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  372. __func__, method_id, arg0, -ENODEV);
  373. return -ENODEV;
  374. }
  375. return 0;
  376. }
  377. /*
  378. * Returns as an error if the method output is not a buffer. Typically this
  379. * means that the method called is unsupported.
  380. */
  381. static int asus_wmi_evaluate_method_buf(u32 method_id,
  382. u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
  383. {
  384. struct bios_args args = {
  385. .arg0 = arg0,
  386. .arg1 = arg1,
  387. .arg2 = 0,
  388. };
  389. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  390. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  391. acpi_status status;
  392. union acpi_object *obj;
  393. int err = 0;
  394. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
  395. &input, &output);
  396. pr_debug("%s called (0x%08x) with args: 0x%08x, 0x%08x\n",
  397. __func__, method_id, arg0, arg1);
  398. if (ACPI_FAILURE(status)) {
  399. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  400. __func__, method_id, arg0, -EIO);
  401. return -EIO;
  402. }
  403. obj = (union acpi_object *)output.pointer;
  404. switch (obj->type) {
  405. case ACPI_TYPE_BUFFER:
  406. if (obj->buffer.length > size) {
  407. err = -ENOSPC;
  408. break;
  409. }
  410. if (obj->buffer.length == 0) {
  411. err = -ENODATA;
  412. break;
  413. }
  414. memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
  415. break;
  416. case ACPI_TYPE_INTEGER:
  417. err = (u32)obj->integer.value;
  418. if (err == ASUS_WMI_UNSUPPORTED_METHOD)
  419. err = -ENODEV;
  420. /*
  421. * At least one method returns a 0 with no buffer if no arg
  422. * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
  423. */
  424. if (err == 0)
  425. err = -ENODATA;
  426. break;
  427. default:
  428. err = -ENODATA;
  429. break;
  430. }
  431. kfree(obj);
  432. if (err) {
  433. pr_debug("%s, (0x%08x), arg 0x%08x failed: %d\n",
  434. __func__, method_id, arg0, err);
  435. return err;
  436. }
  437. return 0;
  438. }
  439. static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
  440. {
  441. struct acpi_buffer input;
  442. u64 phys_addr;
  443. u32 retval;
  444. u32 status;
  445. /*
  446. * Copy to dma capable address otherwise memory corruption occurs as
  447. * bios has to be able to access it.
  448. */
  449. input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
  450. input.length = args.length;
  451. if (!input.pointer)
  452. return -ENOMEM;
  453. phys_addr = virt_to_phys(input.pointer);
  454. status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
  455. phys_addr, 0, &retval);
  456. if (!status)
  457. memcpy(args.pointer, input.pointer, args.length);
  458. kfree(input.pointer);
  459. if (status)
  460. return -ENXIO;
  461. return retval;
  462. }
  463. static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
  464. {
  465. int err;
  466. err = asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
  467. if (err)
  468. return err;
  469. if (*retval == ~0)
  470. return -ENODEV;
  471. return 0;
  472. }
  473. /**
  474. * asus_wmi_get_devstate_dsts() - Get the WMI function state.
  475. * @dev_id: The WMI method ID to call.
  476. * @retval: A pointer to where to store the value returned from WMI.
  477. *
  478. * Returns:
  479. * * %-ENODEV - method ID is unsupported.
  480. * * %0 - successful and retval is filled.
  481. * * %other - error from WMI call.
  482. */
  483. int asus_wmi_get_devstate_dsts(u32 dev_id, u32 *retval)
  484. {
  485. int err;
  486. err = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, dev_id, 0, retval);
  487. if (err)
  488. return err;
  489. if ((*retval & ASUS_WMI_DSTS_PRESENCE_BIT) == 0x00)
  490. return -ENODEV;
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_NS_GPL(asus_wmi_get_devstate_dsts, "ASUS_WMI");
  494. /**
  495. * asus_wmi_set_devstate() - Set the WMI function state.
  496. *
  497. * Note: an asus_wmi_set_devstate() call must be paired with a
  498. * asus_wmi_get_devstate_dsts() to check if the WMI function is supported.
  499. *
  500. * @dev_id: The WMI function to call.
  501. * @ctrl_param: The argument to be used for this WMI function.
  502. * @retval: A pointer to where to store the value returned from WMI.
  503. *
  504. * Returns:
  505. * * %-ENODEV - method ID is unsupported.
  506. * * %0 - successful and retval is filled.
  507. * * %other - error from WMI call.
  508. */
  509. int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param, u32 *retval)
  510. {
  511. return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
  512. ctrl_param, retval);
  513. }
  514. EXPORT_SYMBOL_NS_GPL(asus_wmi_set_devstate, "ASUS_WMI");
  515. /* Helper for special devices with magic return codes */
  516. static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
  517. u32 dev_id, u32 mask)
  518. {
  519. u32 retval = 0;
  520. int err;
  521. err = asus_wmi_get_devstate(asus, dev_id, &retval);
  522. if (err < 0)
  523. return err;
  524. if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
  525. return -ENODEV;
  526. if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
  527. if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
  528. return -ENODEV;
  529. }
  530. return retval & mask;
  531. }
  532. static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
  533. {
  534. return asus_wmi_get_devstate_bits(asus, dev_id,
  535. ASUS_WMI_DSTS_STATUS_BIT);
  536. }
  537. static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
  538. {
  539. u32 retval;
  540. int status = asus_wmi_get_devstate(asus, dev_id, &retval);
  541. pr_debug("%s called (0x%08x), retval: 0x%08x\n", __func__, dev_id, retval);
  542. return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
  543. }
  544. /* Input **********************************************************************/
  545. static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
  546. {
  547. input_report_switch(asus->inputdev, SW_TABLET_MODE,
  548. asus->tablet_switch_inverted ? !value : value);
  549. input_sync(asus->inputdev);
  550. }
  551. static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
  552. {
  553. struct device *dev = &asus->platform_device->dev;
  554. int result;
  555. result = asus_wmi_get_devstate_simple(asus, dev_id);
  556. if (result >= 0) {
  557. input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
  558. asus_wmi_tablet_sw_report(asus, result);
  559. asus->tablet_switch_dev_id = dev_id;
  560. asus->tablet_switch_event_code = event_code;
  561. } else if (result == -ENODEV) {
  562. dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
  563. } else {
  564. dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
  565. }
  566. }
  567. static int asus_wmi_input_init(struct asus_wmi *asus)
  568. {
  569. struct device *dev = &asus->platform_device->dev;
  570. int err;
  571. asus->inputdev = input_allocate_device();
  572. if (!asus->inputdev)
  573. return -ENOMEM;
  574. asus->inputdev->name = asus->driver->input_name;
  575. asus->inputdev->phys = asus->driver->input_phys;
  576. asus->inputdev->id.bustype = BUS_HOST;
  577. asus->inputdev->dev.parent = dev;
  578. set_bit(EV_REP, asus->inputdev->evbit);
  579. err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
  580. if (err)
  581. goto err_free_dev;
  582. switch (asus->driver->quirks->tablet_switch_mode) {
  583. case asus_wmi_no_tablet_switch:
  584. break;
  585. case asus_wmi_kbd_dock_devid:
  586. asus->tablet_switch_inverted = true;
  587. asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
  588. break;
  589. case asus_wmi_lid_flip_devid:
  590. asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
  591. break;
  592. case asus_wmi_lid_flip_rog_devid:
  593. asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
  594. break;
  595. }
  596. err = input_register_device(asus->inputdev);
  597. if (err)
  598. goto err_free_dev;
  599. return 0;
  600. err_free_dev:
  601. input_free_device(asus->inputdev);
  602. return err;
  603. }
  604. static void asus_wmi_input_exit(struct asus_wmi *asus)
  605. {
  606. if (asus->inputdev)
  607. input_unregister_device(asus->inputdev);
  608. asus->inputdev = NULL;
  609. }
  610. /* Tablet mode ****************************************************************/
  611. static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
  612. {
  613. int result;
  614. if (!asus->tablet_switch_dev_id)
  615. return;
  616. result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
  617. if (result >= 0)
  618. asus_wmi_tablet_sw_report(asus, result);
  619. }
  620. /* Charging mode, 1=Barrel, 2=USB ******************************************/
  621. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  622. static ssize_t charge_mode_show(struct device *dev,
  623. struct device_attribute *attr, char *buf)
  624. {
  625. struct asus_wmi *asus = dev_get_drvdata(dev);
  626. int result, value;
  627. result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
  628. if (result < 0)
  629. return result;
  630. asus_wmi_show_deprecated();
  631. return sysfs_emit(buf, "%d\n", value & 0xff);
  632. }
  633. static DEVICE_ATTR_RO(charge_mode);
  634. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  635. /* dGPU ********************************************************************/
  636. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  637. static ssize_t dgpu_disable_show(struct device *dev,
  638. struct device_attribute *attr, char *buf)
  639. {
  640. struct asus_wmi *asus = dev_get_drvdata(dev);
  641. int result;
  642. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
  643. if (result < 0)
  644. return result;
  645. asus_wmi_show_deprecated();
  646. return sysfs_emit(buf, "%d\n", result);
  647. }
  648. /*
  649. * A user may be required to store the value twice, typcial store first, then
  650. * rescan PCI bus to activate power, then store a second time to save correctly.
  651. * The reason for this is that an extra code path in the ACPI is enabled when
  652. * the device and bus are powered.
  653. */
  654. static ssize_t dgpu_disable_store(struct device *dev,
  655. struct device_attribute *attr,
  656. const char *buf, size_t count)
  657. {
  658. int result, err;
  659. u32 disable;
  660. struct asus_wmi *asus = dev_get_drvdata(dev);
  661. result = kstrtou32(buf, 10, &disable);
  662. if (result)
  663. return result;
  664. if (disable > 1)
  665. return -EINVAL;
  666. if (asus->gpu_mux_dev) {
  667. result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
  668. if (result < 0)
  669. /* An error here may signal greater failure of GPU handling */
  670. return result;
  671. if (!result && disable) {
  672. err = -ENODEV;
  673. pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err);
  674. return err;
  675. }
  676. }
  677. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
  678. if (err) {
  679. pr_warn("Failed to set dgpu disable: %d\n", err);
  680. return err;
  681. }
  682. if (result > 1) {
  683. pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
  684. return -EIO;
  685. }
  686. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
  687. return count;
  688. }
  689. static DEVICE_ATTR_RW(dgpu_disable);
  690. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  691. /* eGPU ********************************************************************/
  692. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  693. static ssize_t egpu_enable_show(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. struct asus_wmi *asus = dev_get_drvdata(dev);
  697. int result;
  698. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
  699. if (result < 0)
  700. return result;
  701. asus_wmi_show_deprecated();
  702. return sysfs_emit(buf, "%d\n", result);
  703. }
  704. /* The ACPI call to enable the eGPU also disables the internal dGPU */
  705. static ssize_t egpu_enable_store(struct device *dev,
  706. struct device_attribute *attr,
  707. const char *buf, size_t count)
  708. {
  709. int result, err;
  710. u32 enable;
  711. struct asus_wmi *asus = dev_get_drvdata(dev);
  712. err = kstrtou32(buf, 10, &enable);
  713. if (err)
  714. return err;
  715. if (enable > 1)
  716. return -EINVAL;
  717. err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
  718. if (err < 0) {
  719. pr_warn("Failed to get egpu connection status: %d\n", err);
  720. return err;
  721. }
  722. if (asus->gpu_mux_dev) {
  723. result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
  724. if (result < 0) {
  725. /* An error here may signal greater failure of GPU handling */
  726. pr_warn("Failed to get gpu mux status: %d\n", result);
  727. return result;
  728. }
  729. if (!result && enable) {
  730. err = -ENODEV;
  731. pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err);
  732. return err;
  733. }
  734. }
  735. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
  736. if (err) {
  737. pr_warn("Failed to set egpu state: %d\n", err);
  738. return err;
  739. }
  740. if (result > 1) {
  741. pr_warn("Failed to set egpu state (retval): 0x%x\n", result);
  742. return -EIO;
  743. }
  744. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
  745. return count;
  746. }
  747. static DEVICE_ATTR_RW(egpu_enable);
  748. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  749. /* Is eGPU connected? *********************************************************/
  750. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  751. static ssize_t egpu_connected_show(struct device *dev,
  752. struct device_attribute *attr, char *buf)
  753. {
  754. struct asus_wmi *asus = dev_get_drvdata(dev);
  755. int result;
  756. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
  757. if (result < 0)
  758. return result;
  759. asus_wmi_show_deprecated();
  760. return sysfs_emit(buf, "%d\n", result);
  761. }
  762. static DEVICE_ATTR_RO(egpu_connected);
  763. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  764. /* gpu mux switch *************************************************************/
  765. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  766. static ssize_t gpu_mux_mode_show(struct device *dev,
  767. struct device_attribute *attr, char *buf)
  768. {
  769. struct asus_wmi *asus = dev_get_drvdata(dev);
  770. int result;
  771. result = asus_wmi_get_devstate_simple(asus, asus->gpu_mux_dev);
  772. if (result < 0)
  773. return result;
  774. asus_wmi_show_deprecated();
  775. return sysfs_emit(buf, "%d\n", result);
  776. }
  777. static ssize_t gpu_mux_mode_store(struct device *dev,
  778. struct device_attribute *attr,
  779. const char *buf, size_t count)
  780. {
  781. struct asus_wmi *asus = dev_get_drvdata(dev);
  782. int result, err;
  783. u32 optimus;
  784. err = kstrtou32(buf, 10, &optimus);
  785. if (err)
  786. return err;
  787. if (optimus > 1)
  788. return -EINVAL;
  789. if (asus->dgpu_disable_available) {
  790. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
  791. if (result < 0)
  792. /* An error here may signal greater failure of GPU handling */
  793. return result;
  794. if (result && !optimus) {
  795. err = -ENODEV;
  796. pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err);
  797. return err;
  798. }
  799. }
  800. if (asus->egpu_enable_available) {
  801. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
  802. if (result < 0)
  803. /* An error here may signal greater failure of GPU handling */
  804. return result;
  805. if (result && !optimus) {
  806. err = -ENODEV;
  807. pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err);
  808. return err;
  809. }
  810. }
  811. err = asus_wmi_set_devstate(asus->gpu_mux_dev, optimus, &result);
  812. if (err) {
  813. dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
  814. return err;
  815. }
  816. /* !1 is considered a fail by ASUS */
  817. if (result != 1) {
  818. dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
  819. return -EIO;
  820. }
  821. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
  822. return count;
  823. }
  824. static DEVICE_ATTR_RW(gpu_mux_mode);
  825. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  826. /* TUF Laptop Keyboard RGB Modes **********************************************/
  827. static ssize_t kbd_rgb_mode_store(struct device *dev,
  828. struct device_attribute *attr,
  829. const char *buf, size_t count)
  830. {
  831. u32 cmd, mode, r, g, b, speed;
  832. struct led_classdev *led;
  833. struct asus_wmi *asus;
  834. int err;
  835. led = dev_get_drvdata(dev);
  836. asus = container_of(led, struct asus_wmi, kbd_led);
  837. if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
  838. return -EINVAL;
  839. /* B3 is set and B4 is save to BIOS */
  840. switch (cmd) {
  841. case 0:
  842. cmd = 0xb3;
  843. break;
  844. case 1:
  845. cmd = 0xb4;
  846. break;
  847. default:
  848. return -EINVAL;
  849. }
  850. /* These are the known usable modes across all TUF/ROG */
  851. if (mode >= 12 || mode == 9)
  852. mode = 10;
  853. switch (speed) {
  854. case 0:
  855. speed = 0xe1;
  856. break;
  857. case 1:
  858. speed = 0xeb;
  859. break;
  860. case 2:
  861. speed = 0xf5;
  862. break;
  863. default:
  864. speed = 0xeb;
  865. }
  866. err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, asus->kbd_rgb_dev,
  867. cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
  868. if (err)
  869. return err;
  870. return count;
  871. }
  872. static DEVICE_ATTR_WO(kbd_rgb_mode);
  873. static DEVICE_STRING_ATTR_RO(kbd_rgb_mode_index, 0444,
  874. "cmd mode red green blue speed");
  875. static struct attribute *kbd_rgb_mode_attrs[] = {
  876. &dev_attr_kbd_rgb_mode.attr,
  877. &dev_attr_kbd_rgb_mode_index.attr.attr,
  878. NULL,
  879. };
  880. static const struct attribute_group kbd_rgb_mode_group = {
  881. .attrs = kbd_rgb_mode_attrs,
  882. };
  883. /* TUF Laptop Keyboard RGB State **********************************************/
  884. static ssize_t kbd_rgb_state_store(struct device *dev,
  885. struct device_attribute *attr,
  886. const char *buf, size_t count)
  887. {
  888. u32 flags, cmd, boot, awake, sleep, keyboard;
  889. int err;
  890. if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
  891. return -EINVAL;
  892. if (cmd)
  893. cmd = BIT(2);
  894. flags = 0;
  895. if (boot)
  896. flags |= BIT(1);
  897. if (awake)
  898. flags |= BIT(3);
  899. if (sleep)
  900. flags |= BIT(5);
  901. if (keyboard)
  902. flags |= BIT(7);
  903. /* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
  904. err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
  905. ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
  906. if (err)
  907. return err;
  908. return count;
  909. }
  910. static DEVICE_ATTR_WO(kbd_rgb_state);
  911. static DEVICE_STRING_ATTR_RO(kbd_rgb_state_index, 0444,
  912. "cmd boot awake sleep keyboard");
  913. static struct attribute *kbd_rgb_state_attrs[] = {
  914. &dev_attr_kbd_rgb_state.attr,
  915. &dev_attr_kbd_rgb_state_index.attr.attr,
  916. NULL,
  917. };
  918. static const struct attribute_group kbd_rgb_state_group = {
  919. .attrs = kbd_rgb_state_attrs,
  920. };
  921. static const struct attribute_group *kbd_rgb_mode_groups[] = {
  922. NULL,
  923. NULL,
  924. NULL,
  925. };
  926. /* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/
  927. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  928. static ssize_t ppt_pl2_sppt_store(struct device *dev,
  929. struct device_attribute *attr,
  930. const char *buf, size_t count)
  931. {
  932. struct asus_wmi *asus = dev_get_drvdata(dev);
  933. int result, err;
  934. u32 value;
  935. result = kstrtou32(buf, 10, &value);
  936. if (result)
  937. return result;
  938. if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
  939. return -EINVAL;
  940. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result);
  941. if (err) {
  942. pr_warn("Failed to set ppt_pl2_sppt: %d\n", err);
  943. return err;
  944. }
  945. if (result > 1) {
  946. pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result);
  947. return -EIO;
  948. }
  949. asus->ppt_pl2_sppt = value;
  950. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt");
  951. return count;
  952. }
  953. static ssize_t ppt_pl2_sppt_show(struct device *dev,
  954. struct device_attribute *attr,
  955. char *buf)
  956. {
  957. struct asus_wmi *asus = dev_get_drvdata(dev);
  958. asus_wmi_show_deprecated();
  959. return sysfs_emit(buf, "%u\n", asus->ppt_pl2_sppt);
  960. }
  961. static DEVICE_ATTR_RW(ppt_pl2_sppt);
  962. /* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/
  963. static ssize_t ppt_pl1_spl_store(struct device *dev,
  964. struct device_attribute *attr,
  965. const char *buf, size_t count)
  966. {
  967. struct asus_wmi *asus = dev_get_drvdata(dev);
  968. int result, err;
  969. u32 value;
  970. result = kstrtou32(buf, 10, &value);
  971. if (result)
  972. return result;
  973. if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
  974. return -EINVAL;
  975. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result);
  976. if (err) {
  977. pr_warn("Failed to set ppt_pl1_spl: %d\n", err);
  978. return err;
  979. }
  980. if (result > 1) {
  981. pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result);
  982. return -EIO;
  983. }
  984. asus->ppt_pl1_spl = value;
  985. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl");
  986. return count;
  987. }
  988. static ssize_t ppt_pl1_spl_show(struct device *dev,
  989. struct device_attribute *attr,
  990. char *buf)
  991. {
  992. struct asus_wmi *asus = dev_get_drvdata(dev);
  993. asus_wmi_show_deprecated();
  994. return sysfs_emit(buf, "%u\n", asus->ppt_pl1_spl);
  995. }
  996. static DEVICE_ATTR_RW(ppt_pl1_spl);
  997. /* Tunable: PPT APU FPPT ******************************************************/
  998. static ssize_t ppt_fppt_store(struct device *dev,
  999. struct device_attribute *attr,
  1000. const char *buf, size_t count)
  1001. {
  1002. struct asus_wmi *asus = dev_get_drvdata(dev);
  1003. int result, err;
  1004. u32 value;
  1005. result = kstrtou32(buf, 10, &value);
  1006. if (result)
  1007. return result;
  1008. if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
  1009. return -EINVAL;
  1010. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL3_FPPT, value, &result);
  1011. if (err) {
  1012. pr_warn("Failed to set ppt_fppt: %d\n", err);
  1013. return err;
  1014. }
  1015. if (result > 1) {
  1016. pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result);
  1017. return -EIO;
  1018. }
  1019. asus->ppt_fppt = value;
  1020. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt");
  1021. return count;
  1022. }
  1023. static ssize_t ppt_fppt_show(struct device *dev,
  1024. struct device_attribute *attr,
  1025. char *buf)
  1026. {
  1027. struct asus_wmi *asus = dev_get_drvdata(dev);
  1028. asus_wmi_show_deprecated();
  1029. return sysfs_emit(buf, "%u\n", asus->ppt_fppt);
  1030. }
  1031. static DEVICE_ATTR_RW(ppt_fppt);
  1032. /* Tunable: PPT APU SPPT *****************************************************/
  1033. static ssize_t ppt_apu_sppt_store(struct device *dev,
  1034. struct device_attribute *attr,
  1035. const char *buf, size_t count)
  1036. {
  1037. struct asus_wmi *asus = dev_get_drvdata(dev);
  1038. int result, err;
  1039. u32 value;
  1040. result = kstrtou32(buf, 10, &value);
  1041. if (result)
  1042. return result;
  1043. if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
  1044. return -EINVAL;
  1045. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result);
  1046. if (err) {
  1047. pr_warn("Failed to set ppt_apu_sppt: %d\n", err);
  1048. return err;
  1049. }
  1050. if (result > 1) {
  1051. pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result);
  1052. return -EIO;
  1053. }
  1054. asus->ppt_apu_sppt = value;
  1055. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt");
  1056. return count;
  1057. }
  1058. static ssize_t ppt_apu_sppt_show(struct device *dev,
  1059. struct device_attribute *attr,
  1060. char *buf)
  1061. {
  1062. struct asus_wmi *asus = dev_get_drvdata(dev);
  1063. asus_wmi_show_deprecated();
  1064. return sysfs_emit(buf, "%u\n", asus->ppt_apu_sppt);
  1065. }
  1066. static DEVICE_ATTR_RW(ppt_apu_sppt);
  1067. /* Tunable: PPT platform SPPT ************************************************/
  1068. static ssize_t ppt_platform_sppt_store(struct device *dev,
  1069. struct device_attribute *attr,
  1070. const char *buf, size_t count)
  1071. {
  1072. struct asus_wmi *asus = dev_get_drvdata(dev);
  1073. int result, err;
  1074. u32 value;
  1075. result = kstrtou32(buf, 10, &value);
  1076. if (result)
  1077. return result;
  1078. if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
  1079. return -EINVAL;
  1080. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result);
  1081. if (err) {
  1082. pr_warn("Failed to set ppt_platform_sppt: %d\n", err);
  1083. return err;
  1084. }
  1085. if (result > 1) {
  1086. pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result);
  1087. return -EIO;
  1088. }
  1089. asus->ppt_platform_sppt = value;
  1090. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt");
  1091. return count;
  1092. }
  1093. static ssize_t ppt_platform_sppt_show(struct device *dev,
  1094. struct device_attribute *attr,
  1095. char *buf)
  1096. {
  1097. struct asus_wmi *asus = dev_get_drvdata(dev);
  1098. asus_wmi_show_deprecated();
  1099. return sysfs_emit(buf, "%u\n", asus->ppt_platform_sppt);
  1100. }
  1101. static DEVICE_ATTR_RW(ppt_platform_sppt);
  1102. /* Tunable: NVIDIA dynamic boost *********************************************/
  1103. static ssize_t nv_dynamic_boost_store(struct device *dev,
  1104. struct device_attribute *attr,
  1105. const char *buf, size_t count)
  1106. {
  1107. struct asus_wmi *asus = dev_get_drvdata(dev);
  1108. int result, err;
  1109. u32 value;
  1110. result = kstrtou32(buf, 10, &value);
  1111. if (result)
  1112. return result;
  1113. if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX)
  1114. return -EINVAL;
  1115. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result);
  1116. if (err) {
  1117. pr_warn("Failed to set nv_dynamic_boost: %d\n", err);
  1118. return err;
  1119. }
  1120. if (result > 1) {
  1121. pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result);
  1122. return -EIO;
  1123. }
  1124. asus->nv_dynamic_boost = value;
  1125. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost");
  1126. return count;
  1127. }
  1128. static ssize_t nv_dynamic_boost_show(struct device *dev,
  1129. struct device_attribute *attr,
  1130. char *buf)
  1131. {
  1132. struct asus_wmi *asus = dev_get_drvdata(dev);
  1133. asus_wmi_show_deprecated();
  1134. return sysfs_emit(buf, "%u\n", asus->nv_dynamic_boost);
  1135. }
  1136. static DEVICE_ATTR_RW(nv_dynamic_boost);
  1137. /* Tunable: NVIDIA temperature target ****************************************/
  1138. static ssize_t nv_temp_target_store(struct device *dev,
  1139. struct device_attribute *attr,
  1140. const char *buf, size_t count)
  1141. {
  1142. struct asus_wmi *asus = dev_get_drvdata(dev);
  1143. int result, err;
  1144. u32 value;
  1145. result = kstrtou32(buf, 10, &value);
  1146. if (result)
  1147. return result;
  1148. if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX)
  1149. return -EINVAL;
  1150. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result);
  1151. if (err) {
  1152. pr_warn("Failed to set nv_temp_target: %d\n", err);
  1153. return err;
  1154. }
  1155. if (result > 1) {
  1156. pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result);
  1157. return -EIO;
  1158. }
  1159. asus->nv_temp_target = value;
  1160. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target");
  1161. return count;
  1162. }
  1163. static ssize_t nv_temp_target_show(struct device *dev,
  1164. struct device_attribute *attr,
  1165. char *buf)
  1166. {
  1167. struct asus_wmi *asus = dev_get_drvdata(dev);
  1168. asus_wmi_show_deprecated();
  1169. return sysfs_emit(buf, "%u\n", asus->nv_temp_target);
  1170. }
  1171. static DEVICE_ATTR_RW(nv_temp_target);
  1172. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  1173. /* Ally MCU Powersave ********************************************************/
  1174. /*
  1175. * The HID driver needs to check MCU version and set this to false if the MCU FW
  1176. * version is >= the minimum requirements. New FW do not need the hacks.
  1177. */
  1178. void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
  1179. {
  1180. use_ally_mcu_hack = status;
  1181. pr_debug("%s Ally MCU suspend quirk\n",
  1182. status == ASUS_WMI_ALLY_MCU_HACK_ENABLED ? "Enabled" : "Disabled");
  1183. }
  1184. EXPORT_SYMBOL_NS_GPL(set_ally_mcu_hack, "ASUS_WMI");
  1185. /*
  1186. * mcu_powersave should be enabled always, as it is fixed in MCU FW versions:
  1187. * - v313 for Ally X
  1188. * - v319 for Ally 1
  1189. * The HID driver checks MCU versions and so should set this if requirements match
  1190. */
  1191. void set_ally_mcu_powersave(bool enabled)
  1192. {
  1193. int result, err;
  1194. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enabled, &result);
  1195. if (err) {
  1196. pr_warn("Failed to set MCU powersave: %d\n", err);
  1197. return;
  1198. }
  1199. if (result > 1) {
  1200. pr_warn("Failed to set MCU powersave (result): 0x%x\n", result);
  1201. return;
  1202. }
  1203. pr_debug("%s MCU Powersave\n",
  1204. enabled ? "Enabled" : "Disabled");
  1205. }
  1206. EXPORT_SYMBOL_NS_GPL(set_ally_mcu_powersave, "ASUS_WMI");
  1207. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  1208. static ssize_t mcu_powersave_show(struct device *dev,
  1209. struct device_attribute *attr, char *buf)
  1210. {
  1211. struct asus_wmi *asus = dev_get_drvdata(dev);
  1212. int result;
  1213. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE);
  1214. if (result < 0)
  1215. return result;
  1216. asus_wmi_show_deprecated();
  1217. return sysfs_emit(buf, "%d\n", result);
  1218. }
  1219. static ssize_t mcu_powersave_store(struct device *dev,
  1220. struct device_attribute *attr,
  1221. const char *buf, size_t count)
  1222. {
  1223. int result, err;
  1224. u32 enable;
  1225. struct asus_wmi *asus = dev_get_drvdata(dev);
  1226. result = kstrtou32(buf, 10, &enable);
  1227. if (result)
  1228. return result;
  1229. if (enable > 1)
  1230. return -EINVAL;
  1231. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enable, &result);
  1232. if (err) {
  1233. pr_warn("Failed to set MCU powersave: %d\n", err);
  1234. return err;
  1235. }
  1236. if (result > 1) {
  1237. pr_warn("Failed to set MCU powersave (result): 0x%x\n", result);
  1238. return -EIO;
  1239. }
  1240. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mcu_powersave");
  1241. return count;
  1242. }
  1243. static DEVICE_ATTR_RW(mcu_powersave);
  1244. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  1245. /* Battery ********************************************************************/
  1246. /* The battery maximum charging percentage */
  1247. static int charge_end_threshold;
  1248. static ssize_t charge_control_end_threshold_store(struct device *dev,
  1249. struct device_attribute *attr,
  1250. const char *buf, size_t count)
  1251. {
  1252. int value, ret, rv;
  1253. ret = kstrtouint(buf, 10, &value);
  1254. if (ret)
  1255. return ret;
  1256. if (value < 0 || value > 100)
  1257. return -EINVAL;
  1258. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
  1259. if (ret)
  1260. return ret;
  1261. if (rv != 1)
  1262. return -EIO;
  1263. /* There isn't any method in the DSDT to read the threshold, so we
  1264. * save the threshold.
  1265. */
  1266. charge_end_threshold = value;
  1267. return count;
  1268. }
  1269. static ssize_t charge_control_end_threshold_show(struct device *device,
  1270. struct device_attribute *attr,
  1271. char *buf)
  1272. {
  1273. return sysfs_emit(buf, "%d\n", charge_end_threshold);
  1274. }
  1275. static DEVICE_ATTR_RW(charge_control_end_threshold);
  1276. static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
  1277. {
  1278. /* The WMI method does not provide a way to specific a battery, so we
  1279. * just assume it is the first battery.
  1280. * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
  1281. * battery is named BATT.
  1282. */
  1283. if (strcmp(battery->desc->name, "BAT0") != 0 &&
  1284. strcmp(battery->desc->name, "BAT1") != 0 &&
  1285. strcmp(battery->desc->name, "BATC") != 0 &&
  1286. strcmp(battery->desc->name, "BATT") != 0)
  1287. return -ENODEV;
  1288. if (device_create_file(&battery->dev,
  1289. &dev_attr_charge_control_end_threshold))
  1290. return -ENODEV;
  1291. /* The charge threshold is only reset when the system is power cycled,
  1292. * and we can't get the current threshold so let set it to 100% when
  1293. * a battery is added.
  1294. */
  1295. asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
  1296. charge_end_threshold = 100;
  1297. return 0;
  1298. }
  1299. static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
  1300. {
  1301. device_remove_file(&battery->dev,
  1302. &dev_attr_charge_control_end_threshold);
  1303. return 0;
  1304. }
  1305. static struct acpi_battery_hook battery_hook = {
  1306. .add_battery = asus_wmi_battery_add,
  1307. .remove_battery = asus_wmi_battery_remove,
  1308. .name = "ASUS Battery Extension",
  1309. };
  1310. static void asus_wmi_battery_init(struct asus_wmi *asus)
  1311. {
  1312. asus->battery_rsoc_available = false;
  1313. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
  1314. asus->battery_rsoc_available = true;
  1315. battery_hook_register(&battery_hook);
  1316. }
  1317. }
  1318. static void asus_wmi_battery_exit(struct asus_wmi *asus)
  1319. {
  1320. if (asus->battery_rsoc_available)
  1321. battery_hook_unregister(&battery_hook);
  1322. }
  1323. /* LEDs ***********************************************************************/
  1324. struct asus_hid_ref {
  1325. struct list_head listeners;
  1326. struct asus_wmi *asus;
  1327. /* Protects concurrent access from hid-asus and asus-wmi to leds */
  1328. spinlock_t lock;
  1329. };
  1330. static struct asus_hid_ref asus_ref = {
  1331. .listeners = LIST_HEAD_INIT(asus_ref.listeners),
  1332. .asus = NULL,
  1333. /*
  1334. * Protects .asus, .asus.kbd_led_{wk,notify}, and .listener refs. Other
  1335. * asus variables are read-only after .asus is set.
  1336. *
  1337. * The led cdev device is not protected because it calls backlight_get
  1338. * during initialization, which would result in a nested lock attempt.
  1339. *
  1340. * The led cdev is safe to access without a lock because if
  1341. * kbd_led_avail is true it is initialized before .asus is set and never
  1342. * changed until .asus is dropped. If kbd_led_avail is false, the led
  1343. * cdev is registered by the workqueue, which is single-threaded and
  1344. * cancelled before asus-wmi would access the led cdev to unregister it.
  1345. *
  1346. * A spinlock is used, because the protected variables can be accessed
  1347. * from an IRQ context from asus-hid.
  1348. */
  1349. .lock = __SPIN_LOCK_UNLOCKED(asus_ref.lock),
  1350. };
  1351. /*
  1352. * Allows registering hid-asus listeners that want to be notified of
  1353. * keyboard backlight changes.
  1354. */
  1355. int asus_hid_register_listener(struct asus_hid_listener *bdev)
  1356. {
  1357. struct asus_wmi *asus;
  1358. guard(spinlock_irqsave)(&asus_ref.lock);
  1359. list_add_tail(&bdev->list, &asus_ref.listeners);
  1360. asus = asus_ref.asus;
  1361. if (asus)
  1362. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL_GPL(asus_hid_register_listener);
  1366. /*
  1367. * Allows unregistering hid-asus listeners that were added with
  1368. * asus_hid_register_listener().
  1369. */
  1370. void asus_hid_unregister_listener(struct asus_hid_listener *bdev)
  1371. {
  1372. guard(spinlock_irqsave)(&asus_ref.lock);
  1373. list_del(&bdev->list);
  1374. }
  1375. EXPORT_SYMBOL_GPL(asus_hid_unregister_listener);
  1376. static void do_kbd_led_set(struct led_classdev *led_cdev, int value);
  1377. static void kbd_led_update_all(struct work_struct *work)
  1378. {
  1379. struct asus_wmi *asus;
  1380. bool registered, notify;
  1381. int ret, value;
  1382. asus = container_of(work, struct asus_wmi, kbd_led_work);
  1383. scoped_guard(spinlock_irqsave, &asus_ref.lock) {
  1384. registered = asus->kbd_led_registered;
  1385. value = asus->kbd_led_wk;
  1386. notify = asus->kbd_led_notify;
  1387. }
  1388. if (!registered) {
  1389. /*
  1390. * This workqueue runs under asus-wmi, which means probe has
  1391. * completed and asus-wmi will keep running until it finishes.
  1392. * Therefore, we can safely register the LED without holding
  1393. * a spinlock.
  1394. */
  1395. ret = devm_led_classdev_register(&asus->platform_device->dev,
  1396. &asus->kbd_led);
  1397. if (!ret) {
  1398. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1399. asus->kbd_led_registered = true;
  1400. } else {
  1401. pr_warn("Failed to register keyboard backlight LED: %d\n", ret);
  1402. return;
  1403. }
  1404. }
  1405. if (value >= 0)
  1406. do_kbd_led_set(&asus->kbd_led, value);
  1407. if (notify) {
  1408. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1409. asus->kbd_led_notify = false;
  1410. led_classdev_notify_brightness_hw_changed(&asus->kbd_led, value);
  1411. }
  1412. }
  1413. /*
  1414. * This function is called from hid-asus to inform asus-wmi of brightness
  1415. * changes initiated by the keyboard backlight keys.
  1416. */
  1417. int asus_hid_event(enum asus_hid_event event)
  1418. {
  1419. struct asus_wmi *asus;
  1420. int brightness;
  1421. guard(spinlock_irqsave)(&asus_ref.lock);
  1422. asus = asus_ref.asus;
  1423. if (!asus || !asus->kbd_led_registered)
  1424. return -EBUSY;
  1425. brightness = asus->kbd_led_wk;
  1426. switch (event) {
  1427. case ASUS_EV_BRTUP:
  1428. brightness += 1;
  1429. break;
  1430. case ASUS_EV_BRTDOWN:
  1431. brightness -= 1;
  1432. break;
  1433. case ASUS_EV_BRTTOGGLE:
  1434. if (brightness >= ASUS_EV_MAX_BRIGHTNESS)
  1435. brightness = 0;
  1436. else
  1437. brightness += 1;
  1438. break;
  1439. }
  1440. asus->kbd_led_wk = clamp_val(brightness, 0, ASUS_EV_MAX_BRIGHTNESS);
  1441. asus->kbd_led_notify = true;
  1442. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1443. return 0;
  1444. }
  1445. EXPORT_SYMBOL_GPL(asus_hid_event);
  1446. /*
  1447. * These functions actually update the LED's, and are called from a
  1448. * workqueue. By doing this as separate work rather than when the LED
  1449. * subsystem asks, we avoid messing with the Asus ACPI stuff during a
  1450. * potentially bad time, such as a timer interrupt.
  1451. */
  1452. static void tpd_led_update(struct work_struct *work)
  1453. {
  1454. int ctrl_param;
  1455. struct asus_wmi *asus;
  1456. asus = container_of(work, struct asus_wmi, tpd_led_work);
  1457. ctrl_param = asus->tpd_led_wk;
  1458. asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
  1459. }
  1460. static void tpd_led_set(struct led_classdev *led_cdev,
  1461. enum led_brightness value)
  1462. {
  1463. struct asus_wmi *asus;
  1464. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  1465. asus->tpd_led_wk = !!value;
  1466. queue_work(asus->led_workqueue, &asus->tpd_led_work);
  1467. }
  1468. static int read_tpd_led_state(struct asus_wmi *asus)
  1469. {
  1470. return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
  1471. }
  1472. static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
  1473. {
  1474. struct asus_wmi *asus;
  1475. asus = container_of(led_cdev, struct asus_wmi, tpd_led);
  1476. return read_tpd_led_state(asus);
  1477. }
  1478. static void kbd_led_update(struct asus_wmi *asus)
  1479. {
  1480. int ctrl_param = 0;
  1481. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1482. ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
  1483. asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
  1484. }
  1485. static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
  1486. {
  1487. int retval;
  1488. /*
  1489. * bits 0-2: level
  1490. * bit 7: light on/off
  1491. * bit 8-10: environment (0: dark, 1: normal, 2: light)
  1492. * bit 17: status unknown
  1493. */
  1494. retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
  1495. 0xFFFF);
  1496. /* Unknown status is considered as off */
  1497. if (retval == 0x8000)
  1498. retval = 0;
  1499. if (retval < 0)
  1500. return retval;
  1501. if (level)
  1502. *level = retval & 0x7F;
  1503. if (env)
  1504. *env = (retval >> 8) & 0x7F;
  1505. return 0;
  1506. }
  1507. static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
  1508. {
  1509. struct asus_hid_listener *listener;
  1510. struct asus_wmi *asus;
  1511. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  1512. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1513. asus->kbd_led_wk = clamp_val(value, 0, ASUS_EV_MAX_BRIGHTNESS);
  1514. if (asus->kbd_led_avail)
  1515. kbd_led_update(asus);
  1516. scoped_guard(spinlock_irqsave, &asus_ref.lock) {
  1517. list_for_each_entry(listener, &asus_ref.listeners, list)
  1518. listener->brightness_set(listener, asus->kbd_led_wk);
  1519. }
  1520. }
  1521. static int kbd_led_set(struct led_classdev *led_cdev, enum led_brightness value)
  1522. {
  1523. /* Prevent disabling keyboard backlight on module unregister */
  1524. if (led_cdev->flags & LED_UNREGISTERING)
  1525. return 0;
  1526. do_kbd_led_set(led_cdev, value);
  1527. return 0;
  1528. }
  1529. static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
  1530. {
  1531. scoped_guard(spinlock_irqsave, &asus_ref.lock) {
  1532. asus->kbd_led_wk = value;
  1533. asus->kbd_led_notify = true;
  1534. }
  1535. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1536. }
  1537. static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
  1538. {
  1539. struct asus_wmi *asus;
  1540. int retval, value;
  1541. asus = container_of(led_cdev, struct asus_wmi, kbd_led);
  1542. scoped_guard(spinlock_irqsave, &asus_ref.lock) {
  1543. if (!asus->kbd_led_avail)
  1544. return asus->kbd_led_wk;
  1545. }
  1546. retval = kbd_led_read(asus, &value, NULL);
  1547. if (retval < 0)
  1548. return retval;
  1549. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1550. asus->kbd_led_wk = value;
  1551. return value;
  1552. }
  1553. static int wlan_led_unknown_state(struct asus_wmi *asus)
  1554. {
  1555. u32 result;
  1556. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  1557. return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
  1558. }
  1559. static void wlan_led_update(struct work_struct *work)
  1560. {
  1561. int ctrl_param;
  1562. struct asus_wmi *asus;
  1563. asus = container_of(work, struct asus_wmi, wlan_led_work);
  1564. ctrl_param = asus->wlan_led_wk;
  1565. asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
  1566. }
  1567. static void wlan_led_set(struct led_classdev *led_cdev,
  1568. enum led_brightness value)
  1569. {
  1570. struct asus_wmi *asus;
  1571. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  1572. asus->wlan_led_wk = !!value;
  1573. queue_work(asus->led_workqueue, &asus->wlan_led_work);
  1574. }
  1575. static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
  1576. {
  1577. struct asus_wmi *asus;
  1578. u32 result;
  1579. asus = container_of(led_cdev, struct asus_wmi, wlan_led);
  1580. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
  1581. return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  1582. }
  1583. static void lightbar_led_update(struct work_struct *work)
  1584. {
  1585. struct asus_wmi *asus;
  1586. int ctrl_param;
  1587. asus = container_of(work, struct asus_wmi, lightbar_led_work);
  1588. ctrl_param = asus->lightbar_led_wk;
  1589. asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
  1590. }
  1591. static void lightbar_led_set(struct led_classdev *led_cdev,
  1592. enum led_brightness value)
  1593. {
  1594. struct asus_wmi *asus;
  1595. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  1596. asus->lightbar_led_wk = !!value;
  1597. queue_work(asus->led_workqueue, &asus->lightbar_led_work);
  1598. }
  1599. static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
  1600. {
  1601. struct asus_wmi *asus;
  1602. u32 result;
  1603. asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
  1604. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
  1605. return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
  1606. }
  1607. static int micmute_led_set(struct led_classdev *led_cdev,
  1608. enum led_brightness brightness)
  1609. {
  1610. int state = brightness != LED_OFF;
  1611. int err;
  1612. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
  1613. return err < 0 ? err : 0;
  1614. }
  1615. static enum led_brightness camera_led_get(struct led_classdev *led_cdev)
  1616. {
  1617. struct asus_wmi *asus;
  1618. u32 result;
  1619. asus = container_of(led_cdev, struct asus_wmi, camera_led);
  1620. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CAMERA_LED, &result);
  1621. return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  1622. }
  1623. static int camera_led_set(struct led_classdev *led_cdev,
  1624. enum led_brightness brightness)
  1625. {
  1626. int state = brightness != LED_OFF;
  1627. int err;
  1628. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_CAMERA_LED, state, NULL);
  1629. return err < 0 ? err : 0;
  1630. }
  1631. static void asus_wmi_led_exit(struct asus_wmi *asus)
  1632. {
  1633. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  1634. asus_ref.asus = NULL;
  1635. led_classdev_unregister(&asus->tpd_led);
  1636. led_classdev_unregister(&asus->wlan_led);
  1637. led_classdev_unregister(&asus->lightbar_led);
  1638. led_classdev_unregister(&asus->micmute_led);
  1639. led_classdev_unregister(&asus->camera_led);
  1640. if (asus->led_workqueue)
  1641. destroy_workqueue(asus->led_workqueue);
  1642. }
  1643. static int asus_wmi_led_init(struct asus_wmi *asus)
  1644. {
  1645. int rv = 0, num_rgb_groups = 0, led_val;
  1646. if (asus->kbd_rgb_dev)
  1647. kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
  1648. if (asus->kbd_rgb_state_available)
  1649. kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
  1650. asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
  1651. if (!asus->led_workqueue)
  1652. return -ENOMEM;
  1653. if (read_tpd_led_state(asus) >= 0) {
  1654. INIT_WORK(&asus->tpd_led_work, tpd_led_update);
  1655. asus->tpd_led.name = "asus::touchpad";
  1656. asus->tpd_led.brightness_set = tpd_led_set;
  1657. asus->tpd_led.brightness_get = tpd_led_get;
  1658. asus->tpd_led.max_brightness = 1;
  1659. rv = led_classdev_register(&asus->platform_device->dev,
  1660. &asus->tpd_led);
  1661. if (rv)
  1662. goto error;
  1663. }
  1664. asus->kbd_led.name = "asus::kbd_backlight";
  1665. asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
  1666. asus->kbd_led.brightness_set_blocking = kbd_led_set;
  1667. asus->kbd_led.brightness_get = kbd_led_get;
  1668. asus->kbd_led.max_brightness = ASUS_EV_MAX_BRIGHTNESS;
  1669. asus->kbd_led_avail = !kbd_led_read(asus, &led_val, NULL);
  1670. INIT_WORK(&asus->kbd_led_work, kbd_led_update_all);
  1671. if (asus->kbd_led_avail) {
  1672. asus->kbd_led_wk = led_val;
  1673. if (num_rgb_groups != 0)
  1674. asus->kbd_led.groups = kbd_rgb_mode_groups;
  1675. } else {
  1676. asus->kbd_led_wk = -1;
  1677. }
  1678. scoped_guard(spinlock_irqsave, &asus_ref.lock) {
  1679. asus_ref.asus = asus;
  1680. if (asus->kbd_led_avail || !list_empty(&asus_ref.listeners))
  1681. queue_work(asus->led_workqueue, &asus->kbd_led_work);
  1682. }
  1683. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
  1684. && (asus->driver->quirks->wapf > 0)) {
  1685. INIT_WORK(&asus->wlan_led_work, wlan_led_update);
  1686. asus->wlan_led.name = "asus::wlan";
  1687. asus->wlan_led.brightness_set = wlan_led_set;
  1688. if (!wlan_led_unknown_state(asus))
  1689. asus->wlan_led.brightness_get = wlan_led_get;
  1690. asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
  1691. asus->wlan_led.max_brightness = 1;
  1692. asus->wlan_led.default_trigger = "asus-wlan";
  1693. rv = led_classdev_register(&asus->platform_device->dev,
  1694. &asus->wlan_led);
  1695. if (rv)
  1696. goto error;
  1697. }
  1698. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
  1699. INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
  1700. asus->lightbar_led.name = "asus::lightbar";
  1701. asus->lightbar_led.brightness_set = lightbar_led_set;
  1702. asus->lightbar_led.brightness_get = lightbar_led_get;
  1703. asus->lightbar_led.max_brightness = 1;
  1704. rv = led_classdev_register(&asus->platform_device->dev,
  1705. &asus->lightbar_led);
  1706. }
  1707. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
  1708. asus->micmute_led.name = "platform::micmute";
  1709. asus->micmute_led.max_brightness = 1;
  1710. asus->micmute_led.brightness_set_blocking = micmute_led_set;
  1711. asus->micmute_led.default_trigger = "audio-micmute";
  1712. rv = led_classdev_register(&asus->platform_device->dev,
  1713. &asus->micmute_led);
  1714. if (rv)
  1715. goto error;
  1716. }
  1717. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED)) {
  1718. asus->camera_led.name = "asus::camera";
  1719. asus->camera_led.max_brightness = 1;
  1720. asus->camera_led.brightness_get = camera_led_get;
  1721. asus->camera_led.brightness_set_blocking = camera_led_set;
  1722. rv = led_classdev_register(&asus->platform_device->dev,
  1723. &asus->camera_led);
  1724. if (rv)
  1725. goto error;
  1726. }
  1727. if (asus->oobe_state_available) {
  1728. /*
  1729. * Disable OOBE state, so that e.g. the keyboard backlight
  1730. * works.
  1731. */
  1732. rv = asus_wmi_set_devstate(ASUS_WMI_DEVID_OOBE, 1, NULL);
  1733. if (rv)
  1734. goto error;
  1735. }
  1736. error:
  1737. if (rv)
  1738. asus_wmi_led_exit(asus);
  1739. return rv;
  1740. }
  1741. /* RF *************************************************************************/
  1742. /*
  1743. * PCI hotplug (for wlan rfkill)
  1744. */
  1745. static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
  1746. {
  1747. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  1748. if (result < 0)
  1749. return false;
  1750. return !result;
  1751. }
  1752. static void asus_rfkill_hotplug(struct asus_wmi *asus)
  1753. {
  1754. struct pci_dev *dev;
  1755. struct pci_bus *bus;
  1756. bool blocked;
  1757. bool absent;
  1758. u32 l;
  1759. mutex_lock(&asus->wmi_lock);
  1760. blocked = asus_wlan_rfkill_blocked(asus);
  1761. mutex_unlock(&asus->wmi_lock);
  1762. mutex_lock(&asus->hotplug_lock);
  1763. pci_lock_rescan_remove();
  1764. if (asus->wlan.rfkill)
  1765. rfkill_set_sw_state(asus->wlan.rfkill, blocked);
  1766. if (asus->hotplug_slot.ops) {
  1767. bus = pci_find_bus(0, 1);
  1768. if (!bus) {
  1769. pr_warn("Unable to find PCI bus 1?\n");
  1770. goto out_unlock;
  1771. }
  1772. if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
  1773. pr_err("Unable to read PCI config space?\n");
  1774. goto out_unlock;
  1775. }
  1776. absent = (l == 0xffffffff);
  1777. if (blocked != absent) {
  1778. pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
  1779. blocked ? "blocked" : "unblocked",
  1780. absent ? "absent" : "present");
  1781. pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
  1782. goto out_unlock;
  1783. }
  1784. if (!blocked) {
  1785. dev = pci_get_slot(bus, 0);
  1786. if (dev) {
  1787. /* Device already present */
  1788. pci_dev_put(dev);
  1789. goto out_unlock;
  1790. }
  1791. dev = pci_scan_single_device(bus, 0);
  1792. if (dev) {
  1793. pci_bus_assign_resources(bus);
  1794. pci_bus_add_device(dev);
  1795. }
  1796. } else {
  1797. dev = pci_get_slot(bus, 0);
  1798. if (dev) {
  1799. pci_stop_and_remove_bus_device(dev);
  1800. pci_dev_put(dev);
  1801. }
  1802. }
  1803. }
  1804. out_unlock:
  1805. pci_unlock_rescan_remove();
  1806. mutex_unlock(&asus->hotplug_lock);
  1807. }
  1808. static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
  1809. {
  1810. struct asus_wmi *asus = data;
  1811. if (event != ACPI_NOTIFY_BUS_CHECK)
  1812. return;
  1813. /*
  1814. * We can't call directly asus_rfkill_hotplug because most
  1815. * of the time WMBC is still being executed and not reetrant.
  1816. * There is currently no way to tell ACPICA that we want this
  1817. * method to be serialized, we schedule a asus_rfkill_hotplug
  1818. * call later, in a safer context.
  1819. */
  1820. queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
  1821. }
  1822. static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
  1823. {
  1824. acpi_status status;
  1825. acpi_handle handle;
  1826. status = acpi_get_handle(NULL, node, &handle);
  1827. if (ACPI_FAILURE(status))
  1828. return -ENODEV;
  1829. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1830. asus_rfkill_notify, asus);
  1831. if (ACPI_FAILURE(status))
  1832. pr_warn("Failed to register notify on %s\n", node);
  1833. return 0;
  1834. }
  1835. static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
  1836. {
  1837. acpi_status status = AE_OK;
  1838. acpi_handle handle;
  1839. status = acpi_get_handle(NULL, node, &handle);
  1840. if (ACPI_FAILURE(status))
  1841. return;
  1842. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1843. asus_rfkill_notify);
  1844. if (ACPI_FAILURE(status))
  1845. pr_err("Error removing rfkill notify handler %s\n", node);
  1846. }
  1847. static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
  1848. u8 *value)
  1849. {
  1850. struct asus_wmi *asus = container_of(hotplug_slot,
  1851. struct asus_wmi, hotplug_slot);
  1852. int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  1853. if (result < 0)
  1854. return result;
  1855. *value = !!result;
  1856. return 0;
  1857. }
  1858. static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
  1859. .get_adapter_status = asus_get_adapter_status,
  1860. .get_power_status = asus_get_adapter_status,
  1861. };
  1862. static void asus_hotplug_work(struct work_struct *work)
  1863. {
  1864. struct asus_wmi *asus;
  1865. asus = container_of(work, struct asus_wmi, hotplug_work);
  1866. asus_rfkill_hotplug(asus);
  1867. }
  1868. static int asus_setup_pci_hotplug(struct asus_wmi *asus)
  1869. {
  1870. int ret = -ENOMEM;
  1871. struct pci_bus *bus = pci_find_bus(0, 1);
  1872. if (!bus) {
  1873. pr_err("Unable to find wifi PCI bus\n");
  1874. return -ENODEV;
  1875. }
  1876. asus->hotplug_workqueue =
  1877. create_singlethread_workqueue("hotplug_workqueue");
  1878. if (!asus->hotplug_workqueue)
  1879. goto error_workqueue;
  1880. INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
  1881. asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
  1882. ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
  1883. if (ret) {
  1884. pr_err("Unable to register hotplug slot - %d\n", ret);
  1885. goto error_register;
  1886. }
  1887. return 0;
  1888. error_register:
  1889. asus->hotplug_slot.ops = NULL;
  1890. destroy_workqueue(asus->hotplug_workqueue);
  1891. error_workqueue:
  1892. return ret;
  1893. }
  1894. /*
  1895. * Rfkill devices
  1896. */
  1897. static int asus_rfkill_set(void *data, bool blocked)
  1898. {
  1899. struct asus_rfkill *priv = data;
  1900. u32 ctrl_param = !blocked;
  1901. u32 dev_id = priv->dev_id;
  1902. /*
  1903. * If the user bit is set, BIOS can't set and record the wlan status,
  1904. * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
  1905. * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
  1906. * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
  1907. * while setting the wlan status through WMI.
  1908. * This is also the behavior that windows app will do.
  1909. */
  1910. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  1911. priv->asus->driver->wlan_ctrl_by_user)
  1912. dev_id = ASUS_WMI_DEVID_WLAN_LED;
  1913. return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
  1914. }
  1915. static void asus_rfkill_query(struct rfkill *rfkill, void *data)
  1916. {
  1917. struct asus_rfkill *priv = data;
  1918. int result;
  1919. result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
  1920. if (result < 0)
  1921. return;
  1922. rfkill_set_sw_state(priv->rfkill, !result);
  1923. }
  1924. static int asus_rfkill_wlan_set(void *data, bool blocked)
  1925. {
  1926. struct asus_rfkill *priv = data;
  1927. struct asus_wmi *asus = priv->asus;
  1928. int ret;
  1929. /*
  1930. * This handler is enabled only if hotplug is enabled.
  1931. * In this case, the asus_wmi_set_devstate() will
  1932. * trigger a wmi notification and we need to wait
  1933. * this call to finish before being able to call
  1934. * any wmi method
  1935. */
  1936. mutex_lock(&asus->wmi_lock);
  1937. ret = asus_rfkill_set(data, blocked);
  1938. mutex_unlock(&asus->wmi_lock);
  1939. return ret;
  1940. }
  1941. static const struct rfkill_ops asus_rfkill_wlan_ops = {
  1942. .set_block = asus_rfkill_wlan_set,
  1943. .query = asus_rfkill_query,
  1944. };
  1945. static const struct rfkill_ops asus_rfkill_ops = {
  1946. .set_block = asus_rfkill_set,
  1947. .query = asus_rfkill_query,
  1948. };
  1949. static int asus_new_rfkill(struct asus_wmi *asus,
  1950. struct asus_rfkill *arfkill,
  1951. const char *name, enum rfkill_type type, int dev_id)
  1952. {
  1953. int result = asus_wmi_get_devstate_simple(asus, dev_id);
  1954. struct rfkill **rfkill = &arfkill->rfkill;
  1955. if (result < 0)
  1956. return result;
  1957. arfkill->dev_id = dev_id;
  1958. arfkill->asus = asus;
  1959. if (dev_id == ASUS_WMI_DEVID_WLAN &&
  1960. asus->driver->quirks->hotplug_wireless)
  1961. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  1962. &asus_rfkill_wlan_ops, arfkill);
  1963. else
  1964. *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
  1965. &asus_rfkill_ops, arfkill);
  1966. if (!*rfkill)
  1967. return -EINVAL;
  1968. if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
  1969. (asus->driver->quirks->wapf > 0))
  1970. rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
  1971. rfkill_init_sw_state(*rfkill, !result);
  1972. result = rfkill_register(*rfkill);
  1973. if (result) {
  1974. rfkill_destroy(*rfkill);
  1975. *rfkill = NULL;
  1976. return result;
  1977. }
  1978. return 0;
  1979. }
  1980. static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
  1981. {
  1982. if (asus->driver->wlan_ctrl_by_user && ashs_present())
  1983. return;
  1984. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  1985. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  1986. asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  1987. if (asus->wlan.rfkill) {
  1988. rfkill_unregister(asus->wlan.rfkill);
  1989. rfkill_destroy(asus->wlan.rfkill);
  1990. asus->wlan.rfkill = NULL;
  1991. }
  1992. /*
  1993. * Refresh pci hotplug in case the rfkill state was changed after
  1994. * asus_unregister_rfkill_notifier()
  1995. */
  1996. asus_rfkill_hotplug(asus);
  1997. if (asus->hotplug_slot.ops)
  1998. pci_hp_deregister(&asus->hotplug_slot);
  1999. if (asus->hotplug_workqueue)
  2000. destroy_workqueue(asus->hotplug_workqueue);
  2001. if (asus->bluetooth.rfkill) {
  2002. rfkill_unregister(asus->bluetooth.rfkill);
  2003. rfkill_destroy(asus->bluetooth.rfkill);
  2004. asus->bluetooth.rfkill = NULL;
  2005. }
  2006. if (asus->wimax.rfkill) {
  2007. rfkill_unregister(asus->wimax.rfkill);
  2008. rfkill_destroy(asus->wimax.rfkill);
  2009. asus->wimax.rfkill = NULL;
  2010. }
  2011. if (asus->wwan3g.rfkill) {
  2012. rfkill_unregister(asus->wwan3g.rfkill);
  2013. rfkill_destroy(asus->wwan3g.rfkill);
  2014. asus->wwan3g.rfkill = NULL;
  2015. }
  2016. if (asus->gps.rfkill) {
  2017. rfkill_unregister(asus->gps.rfkill);
  2018. rfkill_destroy(asus->gps.rfkill);
  2019. asus->gps.rfkill = NULL;
  2020. }
  2021. if (asus->uwb.rfkill) {
  2022. rfkill_unregister(asus->uwb.rfkill);
  2023. rfkill_destroy(asus->uwb.rfkill);
  2024. asus->uwb.rfkill = NULL;
  2025. }
  2026. }
  2027. static int asus_wmi_rfkill_init(struct asus_wmi *asus)
  2028. {
  2029. int result = 0;
  2030. mutex_init(&asus->hotplug_lock);
  2031. mutex_init(&asus->wmi_lock);
  2032. result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
  2033. RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
  2034. if (result && result != -ENODEV)
  2035. goto exit;
  2036. result = asus_new_rfkill(asus, &asus->bluetooth,
  2037. "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
  2038. ASUS_WMI_DEVID_BLUETOOTH);
  2039. if (result && result != -ENODEV)
  2040. goto exit;
  2041. result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
  2042. RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
  2043. if (result && result != -ENODEV)
  2044. goto exit;
  2045. result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
  2046. RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
  2047. if (result && result != -ENODEV)
  2048. goto exit;
  2049. result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
  2050. RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
  2051. if (result && result != -ENODEV)
  2052. goto exit;
  2053. result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
  2054. RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
  2055. if (result && result != -ENODEV)
  2056. goto exit;
  2057. if (!asus->driver->quirks->hotplug_wireless)
  2058. goto exit;
  2059. result = asus_setup_pci_hotplug(asus);
  2060. /*
  2061. * If we get -EBUSY then something else is handling the PCI hotplug -
  2062. * don't fail in this case
  2063. */
  2064. if (result == -EBUSY)
  2065. result = 0;
  2066. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
  2067. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
  2068. asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
  2069. /*
  2070. * Refresh pci hotplug in case the rfkill state was changed during
  2071. * setup.
  2072. */
  2073. asus_rfkill_hotplug(asus);
  2074. exit:
  2075. if (result && result != -ENODEV)
  2076. asus_wmi_rfkill_exit(asus);
  2077. if (result == -ENODEV)
  2078. result = 0;
  2079. return result;
  2080. }
  2081. /* Panel Overdrive ************************************************************/
  2082. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  2083. static ssize_t panel_od_show(struct device *dev,
  2084. struct device_attribute *attr, char *buf)
  2085. {
  2086. struct asus_wmi *asus = dev_get_drvdata(dev);
  2087. int result;
  2088. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
  2089. if (result < 0)
  2090. return result;
  2091. asus_wmi_show_deprecated();
  2092. return sysfs_emit(buf, "%d\n", result);
  2093. }
  2094. static ssize_t panel_od_store(struct device *dev,
  2095. struct device_attribute *attr,
  2096. const char *buf, size_t count)
  2097. {
  2098. int result, err;
  2099. u32 overdrive;
  2100. struct asus_wmi *asus = dev_get_drvdata(dev);
  2101. result = kstrtou32(buf, 10, &overdrive);
  2102. if (result)
  2103. return result;
  2104. if (overdrive > 1)
  2105. return -EINVAL;
  2106. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
  2107. if (err) {
  2108. pr_warn("Failed to set panel overdrive: %d\n", err);
  2109. return err;
  2110. }
  2111. if (result > 1) {
  2112. pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
  2113. return -EIO;
  2114. }
  2115. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
  2116. return count;
  2117. }
  2118. static DEVICE_ATTR_RW(panel_od);
  2119. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  2120. /* Bootup sound ***************************************************************/
  2121. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  2122. static ssize_t boot_sound_show(struct device *dev,
  2123. struct device_attribute *attr, char *buf)
  2124. {
  2125. struct asus_wmi *asus = dev_get_drvdata(dev);
  2126. int result;
  2127. result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BOOT_SOUND);
  2128. if (result < 0)
  2129. return result;
  2130. asus_wmi_show_deprecated();
  2131. return sysfs_emit(buf, "%d\n", result);
  2132. }
  2133. static ssize_t boot_sound_store(struct device *dev,
  2134. struct device_attribute *attr,
  2135. const char *buf, size_t count)
  2136. {
  2137. int result, err;
  2138. u32 snd;
  2139. struct asus_wmi *asus = dev_get_drvdata(dev);
  2140. result = kstrtou32(buf, 10, &snd);
  2141. if (result)
  2142. return result;
  2143. if (snd > 1)
  2144. return -EINVAL;
  2145. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BOOT_SOUND, snd, &result);
  2146. if (err) {
  2147. pr_warn("Failed to set boot sound: %d\n", err);
  2148. return err;
  2149. }
  2150. if (result > 1) {
  2151. pr_warn("Failed to set panel boot sound (result): 0x%x\n", result);
  2152. return -EIO;
  2153. }
  2154. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "boot_sound");
  2155. return count;
  2156. }
  2157. static DEVICE_ATTR_RW(boot_sound);
  2158. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  2159. /* Mini-LED mode **************************************************************/
  2160. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  2161. static ssize_t mini_led_mode_show(struct device *dev,
  2162. struct device_attribute *attr, char *buf)
  2163. {
  2164. struct asus_wmi *asus = dev_get_drvdata(dev);
  2165. u32 value;
  2166. int err;
  2167. err = asus_wmi_get_devstate(asus, asus->mini_led_dev_id, &value);
  2168. if (err < 0)
  2169. return err;
  2170. value = value & ASUS_MINI_LED_MODE_MASK;
  2171. /*
  2172. * Remap the mode values to match previous generation mini-led. The last gen
  2173. * WMI 0 == off, while on this version WMI 2 ==off (flipped).
  2174. */
  2175. if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) {
  2176. switch (value) {
  2177. case ASUS_MINI_LED_2024_WEAK:
  2178. value = ASUS_MINI_LED_ON;
  2179. break;
  2180. case ASUS_MINI_LED_2024_STRONG:
  2181. value = ASUS_MINI_LED_STRONG_MODE;
  2182. break;
  2183. case ASUS_MINI_LED_2024_OFF:
  2184. value = ASUS_MINI_LED_OFF;
  2185. break;
  2186. }
  2187. }
  2188. asus_wmi_show_deprecated();
  2189. return sysfs_emit(buf, "%d\n", value);
  2190. }
  2191. static ssize_t mini_led_mode_store(struct device *dev,
  2192. struct device_attribute *attr,
  2193. const char *buf, size_t count)
  2194. {
  2195. int result, err;
  2196. u32 mode;
  2197. struct asus_wmi *asus = dev_get_drvdata(dev);
  2198. result = kstrtou32(buf, 10, &mode);
  2199. if (result)
  2200. return result;
  2201. if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE &&
  2202. mode > ASUS_MINI_LED_ON)
  2203. return -EINVAL;
  2204. if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2 &&
  2205. mode > ASUS_MINI_LED_STRONG_MODE)
  2206. return -EINVAL;
  2207. /*
  2208. * Remap the mode values so expected behaviour is the same as the last
  2209. * generation of mini-LED with 0 == off, 1 == on.
  2210. */
  2211. if (asus->mini_led_dev_id == ASUS_WMI_DEVID_MINI_LED_MODE2) {
  2212. switch (mode) {
  2213. case ASUS_MINI_LED_OFF:
  2214. mode = ASUS_MINI_LED_2024_OFF;
  2215. break;
  2216. case ASUS_MINI_LED_ON:
  2217. mode = ASUS_MINI_LED_2024_WEAK;
  2218. break;
  2219. case ASUS_MINI_LED_STRONG_MODE:
  2220. mode = ASUS_MINI_LED_2024_STRONG;
  2221. break;
  2222. }
  2223. }
  2224. err = asus_wmi_set_devstate(asus->mini_led_dev_id, mode, &result);
  2225. if (err) {
  2226. pr_warn("Failed to set mini-LED: %d\n", err);
  2227. return err;
  2228. }
  2229. if (result > 1) {
  2230. pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result);
  2231. return -EIO;
  2232. }
  2233. sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode");
  2234. return count;
  2235. }
  2236. static DEVICE_ATTR_RW(mini_led_mode);
  2237. static ssize_t available_mini_led_mode_show(struct device *dev,
  2238. struct device_attribute *attr, char *buf)
  2239. {
  2240. struct asus_wmi *asus = dev_get_drvdata(dev);
  2241. switch (asus->mini_led_dev_id) {
  2242. case ASUS_WMI_DEVID_MINI_LED_MODE:
  2243. return sysfs_emit(buf, "0 1\n");
  2244. case ASUS_WMI_DEVID_MINI_LED_MODE2:
  2245. return sysfs_emit(buf, "0 1 2\n");
  2246. }
  2247. asus_wmi_show_deprecated();
  2248. return sysfs_emit(buf, "0\n");
  2249. }
  2250. static DEVICE_ATTR_RO(available_mini_led_mode);
  2251. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  2252. /* Quirks *********************************************************************/
  2253. static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
  2254. {
  2255. struct pci_dev *xhci_pdev;
  2256. u32 orig_ports_available;
  2257. u32 ports_available = asus->driver->quirks->xusb2pr;
  2258. xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2259. PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
  2260. NULL);
  2261. if (!xhci_pdev)
  2262. return;
  2263. pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  2264. &orig_ports_available);
  2265. pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  2266. cpu_to_le32(ports_available));
  2267. pci_dev_put(xhci_pdev);
  2268. pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
  2269. orig_ports_available, ports_available);
  2270. }
  2271. /*
  2272. * Some devices dont support or have borcken get_als method
  2273. * but still support set method.
  2274. */
  2275. static void asus_wmi_set_als(void)
  2276. {
  2277. asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
  2278. }
  2279. /* Hwmon device ***************************************************************/
  2280. static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
  2281. int *speed)
  2282. {
  2283. struct agfn_fan_args args = {
  2284. .agfn.len = sizeof(args),
  2285. .agfn.mfun = ASUS_FAN_MFUN,
  2286. .agfn.sfun = ASUS_FAN_SFUN_READ,
  2287. .fan = fan,
  2288. .speed = 0,
  2289. };
  2290. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  2291. int status;
  2292. if (fan != 1)
  2293. return -EINVAL;
  2294. status = asus_wmi_evaluate_method_agfn(input);
  2295. if (status || args.agfn.err)
  2296. return -ENXIO;
  2297. if (speed)
  2298. *speed = args.speed;
  2299. return 0;
  2300. }
  2301. static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
  2302. int *speed)
  2303. {
  2304. struct agfn_fan_args args = {
  2305. .agfn.len = sizeof(args),
  2306. .agfn.mfun = ASUS_FAN_MFUN,
  2307. .agfn.sfun = ASUS_FAN_SFUN_WRITE,
  2308. .fan = fan,
  2309. .speed = speed ? *speed : 0,
  2310. };
  2311. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  2312. int status;
  2313. /* 1: for setting 1st fan's speed 0: setting auto mode */
  2314. if (fan != 1 && fan != 0)
  2315. return -EINVAL;
  2316. status = asus_wmi_evaluate_method_agfn(input);
  2317. if (status || args.agfn.err)
  2318. return -ENXIO;
  2319. if (speed && fan == 1)
  2320. asus->agfn_pwm = *speed;
  2321. return 0;
  2322. }
  2323. /*
  2324. * Check if we can read the speed of one fan. If true we assume we can also
  2325. * control it.
  2326. */
  2327. static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
  2328. {
  2329. int status;
  2330. int speed;
  2331. u32 value;
  2332. status = asus_agfn_fan_speed_read(asus, 1, &speed);
  2333. if (status != 0)
  2334. return false;
  2335. status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
  2336. if (status != 0)
  2337. return false;
  2338. /*
  2339. * We need to find a better way, probably using sfun,
  2340. * bits or spec ...
  2341. * Currently we disable it if:
  2342. * - ASUS_WMI_UNSUPPORTED_METHOD is returned
  2343. * - reverved bits are non-zero
  2344. * - sfun and presence bit are not set
  2345. */
  2346. return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
  2347. || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
  2348. }
  2349. static int asus_fan_set_auto(struct asus_wmi *asus)
  2350. {
  2351. int status;
  2352. u32 retval;
  2353. switch (asus->fan_type) {
  2354. case FAN_TYPE_SPEC83:
  2355. status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
  2356. 0, &retval);
  2357. if (status)
  2358. return status;
  2359. if (retval != 1)
  2360. return -EIO;
  2361. break;
  2362. case FAN_TYPE_AGFN:
  2363. status = asus_agfn_fan_speed_write(asus, 0, NULL);
  2364. if (status)
  2365. return -ENXIO;
  2366. break;
  2367. default:
  2368. return -ENXIO;
  2369. }
  2370. /*
  2371. * Modern models like the G713 also have GPU fan control (this is not AGFN)
  2372. */
  2373. if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
  2374. status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
  2375. 0, &retval);
  2376. if (status)
  2377. return status;
  2378. if (retval != 1)
  2379. return -EIO;
  2380. }
  2381. return 0;
  2382. }
  2383. static ssize_t pwm1_show(struct device *dev,
  2384. struct device_attribute *attr,
  2385. char *buf)
  2386. {
  2387. struct asus_wmi *asus = dev_get_drvdata(dev);
  2388. int err;
  2389. int value;
  2390. /* If we already set a value then just return it */
  2391. if (asus->agfn_pwm >= 0)
  2392. return sysfs_emit(buf, "%d\n", asus->agfn_pwm);
  2393. /*
  2394. * If we haven't set already set a value through the AGFN interface,
  2395. * we read a current value through the (now-deprecated) FAN_CTRL device.
  2396. */
  2397. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
  2398. if (err < 0)
  2399. return err;
  2400. value &= 0xFF;
  2401. if (value == 1) /* Low Speed */
  2402. value = 85;
  2403. else if (value == 2)
  2404. value = 170;
  2405. else if (value == 3)
  2406. value = 255;
  2407. else if (value) {
  2408. pr_err("Unknown fan speed %#x\n", value);
  2409. value = -1;
  2410. }
  2411. return sysfs_emit(buf, "%d\n", value);
  2412. }
  2413. static ssize_t pwm1_store(struct device *dev,
  2414. struct device_attribute *attr,
  2415. const char *buf, size_t count) {
  2416. struct asus_wmi *asus = dev_get_drvdata(dev);
  2417. int value;
  2418. int state;
  2419. int ret;
  2420. ret = kstrtouint(buf, 10, &value);
  2421. if (ret)
  2422. return ret;
  2423. value = clamp(value, 0, 255);
  2424. state = asus_agfn_fan_speed_write(asus, 1, &value);
  2425. if (state)
  2426. pr_warn("Setting fan speed failed: %d\n", state);
  2427. else
  2428. asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
  2429. return count;
  2430. }
  2431. static ssize_t fan1_input_show(struct device *dev,
  2432. struct device_attribute *attr,
  2433. char *buf)
  2434. {
  2435. struct asus_wmi *asus = dev_get_drvdata(dev);
  2436. int value;
  2437. int ret;
  2438. switch (asus->fan_type) {
  2439. case FAN_TYPE_SPEC83:
  2440. ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
  2441. &value);
  2442. if (ret < 0)
  2443. return ret;
  2444. value &= 0xffff;
  2445. break;
  2446. case FAN_TYPE_AGFN:
  2447. /* no speed readable on manual mode */
  2448. if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
  2449. return -ENXIO;
  2450. ret = asus_agfn_fan_speed_read(asus, 1, &value);
  2451. if (ret) {
  2452. pr_warn("reading fan speed failed: %d\n", ret);
  2453. return -ENXIO;
  2454. }
  2455. break;
  2456. default:
  2457. return -ENXIO;
  2458. }
  2459. return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
  2460. }
  2461. static ssize_t pwm1_enable_show(struct device *dev,
  2462. struct device_attribute *attr,
  2463. char *buf)
  2464. {
  2465. struct asus_wmi *asus = dev_get_drvdata(dev);
  2466. /*
  2467. * Just read back the cached pwm mode.
  2468. *
  2469. * For the CPU_FAN device, the spec indicates that we should be
  2470. * able to read the device status and consult bit 19 to see if we
  2471. * are in Full On or Automatic mode. However, this does not work
  2472. * in practice on X532FL at least (the bit is always 0) and there's
  2473. * also nothing in the DSDT to indicate that this behaviour exists.
  2474. */
  2475. return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
  2476. }
  2477. static ssize_t pwm1_enable_store(struct device *dev,
  2478. struct device_attribute *attr,
  2479. const char *buf, size_t count)
  2480. {
  2481. struct asus_wmi *asus = dev_get_drvdata(dev);
  2482. int status = 0;
  2483. int state;
  2484. int value;
  2485. int ret;
  2486. u32 retval;
  2487. ret = kstrtouint(buf, 10, &state);
  2488. if (ret)
  2489. return ret;
  2490. if (asus->fan_type == FAN_TYPE_SPEC83) {
  2491. switch (state) { /* standard documented hwmon values */
  2492. case ASUS_FAN_CTRL_FULLSPEED:
  2493. value = 1;
  2494. break;
  2495. case ASUS_FAN_CTRL_AUTO:
  2496. value = 0;
  2497. break;
  2498. default:
  2499. return -EINVAL;
  2500. }
  2501. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
  2502. value, &retval);
  2503. if (ret)
  2504. return ret;
  2505. if (retval != 1)
  2506. return -EIO;
  2507. } else if (asus->fan_type == FAN_TYPE_AGFN) {
  2508. switch (state) {
  2509. case ASUS_FAN_CTRL_MANUAL:
  2510. break;
  2511. case ASUS_FAN_CTRL_AUTO:
  2512. status = asus_fan_set_auto(asus);
  2513. if (status)
  2514. return status;
  2515. break;
  2516. default:
  2517. return -EINVAL;
  2518. }
  2519. }
  2520. asus->fan_pwm_mode = state;
  2521. /* Must set to disabled if mode is toggled */
  2522. if (asus->cpu_fan_curve_available)
  2523. asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
  2524. if (asus->gpu_fan_curve_available)
  2525. asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
  2526. if (asus->mid_fan_curve_available)
  2527. asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
  2528. return count;
  2529. }
  2530. static ssize_t asus_hwmon_temp1(struct device *dev,
  2531. struct device_attribute *attr,
  2532. char *buf)
  2533. {
  2534. struct asus_wmi *asus = dev_get_drvdata(dev);
  2535. u32 value;
  2536. int err;
  2537. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
  2538. if (err < 0)
  2539. return err;
  2540. return sysfs_emit(buf, "%ld\n",
  2541. deci_kelvin_to_millicelsius(value & 0xFFFF));
  2542. }
  2543. /* GPU fan on modern ROG laptops */
  2544. static ssize_t fan2_input_show(struct device *dev,
  2545. struct device_attribute *attr,
  2546. char *buf)
  2547. {
  2548. struct asus_wmi *asus = dev_get_drvdata(dev);
  2549. int value;
  2550. int ret;
  2551. ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
  2552. if (ret < 0)
  2553. return ret;
  2554. value &= 0xffff;
  2555. return sysfs_emit(buf, "%d\n", value * 100);
  2556. }
  2557. /* Middle/Center fan on modern ROG laptops */
  2558. static ssize_t fan3_input_show(struct device *dev,
  2559. struct device_attribute *attr,
  2560. char *buf)
  2561. {
  2562. struct asus_wmi *asus = dev_get_drvdata(dev);
  2563. int value;
  2564. int ret;
  2565. ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value);
  2566. if (ret < 0)
  2567. return ret;
  2568. value &= 0xffff;
  2569. return sysfs_emit(buf, "%d\n", value * 100);
  2570. }
  2571. static ssize_t pwm2_enable_show(struct device *dev,
  2572. struct device_attribute *attr,
  2573. char *buf)
  2574. {
  2575. struct asus_wmi *asus = dev_get_drvdata(dev);
  2576. return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
  2577. }
  2578. static ssize_t pwm2_enable_store(struct device *dev,
  2579. struct device_attribute *attr,
  2580. const char *buf, size_t count)
  2581. {
  2582. struct asus_wmi *asus = dev_get_drvdata(dev);
  2583. int state;
  2584. int value;
  2585. int ret;
  2586. u32 retval;
  2587. ret = kstrtouint(buf, 10, &state);
  2588. if (ret)
  2589. return ret;
  2590. switch (state) { /* standard documented hwmon values */
  2591. case ASUS_FAN_CTRL_FULLSPEED:
  2592. value = 1;
  2593. break;
  2594. case ASUS_FAN_CTRL_AUTO:
  2595. value = 0;
  2596. break;
  2597. default:
  2598. return -EINVAL;
  2599. }
  2600. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
  2601. value, &retval);
  2602. if (ret)
  2603. return ret;
  2604. if (retval != 1)
  2605. return -EIO;
  2606. asus->gpu_fan_pwm_mode = state;
  2607. return count;
  2608. }
  2609. static ssize_t pwm3_enable_show(struct device *dev,
  2610. struct device_attribute *attr,
  2611. char *buf)
  2612. {
  2613. struct asus_wmi *asus = dev_get_drvdata(dev);
  2614. return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode);
  2615. }
  2616. static ssize_t pwm3_enable_store(struct device *dev,
  2617. struct device_attribute *attr,
  2618. const char *buf, size_t count)
  2619. {
  2620. struct asus_wmi *asus = dev_get_drvdata(dev);
  2621. int state;
  2622. int value;
  2623. int ret;
  2624. u32 retval;
  2625. ret = kstrtouint(buf, 10, &state);
  2626. if (ret)
  2627. return ret;
  2628. switch (state) { /* standard documented hwmon values */
  2629. case ASUS_FAN_CTRL_FULLSPEED:
  2630. value = 1;
  2631. break;
  2632. case ASUS_FAN_CTRL_AUTO:
  2633. value = 0;
  2634. break;
  2635. default:
  2636. return -EINVAL;
  2637. }
  2638. ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL,
  2639. value, &retval);
  2640. if (ret)
  2641. return ret;
  2642. if (retval != 1)
  2643. return -EIO;
  2644. asus->mid_fan_pwm_mode = state;
  2645. return count;
  2646. }
  2647. /* Fan1 */
  2648. static DEVICE_ATTR_RW(pwm1);
  2649. static DEVICE_ATTR_RW(pwm1_enable);
  2650. static DEVICE_ATTR_RO(fan1_input);
  2651. static DEVICE_STRING_ATTR_RO(fan1_label, 0444, ASUS_FAN_DESC);
  2652. /* Fan2 - GPU fan */
  2653. static DEVICE_ATTR_RW(pwm2_enable);
  2654. static DEVICE_ATTR_RO(fan2_input);
  2655. static DEVICE_STRING_ATTR_RO(fan2_label, 0444, ASUS_GPU_FAN_DESC);
  2656. /* Fan3 - Middle/center fan */
  2657. static DEVICE_ATTR_RW(pwm3_enable);
  2658. static DEVICE_ATTR_RO(fan3_input);
  2659. static DEVICE_STRING_ATTR_RO(fan3_label, 0444, ASUS_MID_FAN_DESC);
  2660. /* Temperature */
  2661. static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
  2662. static struct attribute *hwmon_attributes[] = {
  2663. &dev_attr_pwm1.attr,
  2664. &dev_attr_pwm1_enable.attr,
  2665. &dev_attr_pwm2_enable.attr,
  2666. &dev_attr_pwm3_enable.attr,
  2667. &dev_attr_fan1_input.attr,
  2668. &dev_attr_fan1_label.attr.attr,
  2669. &dev_attr_fan2_input.attr,
  2670. &dev_attr_fan2_label.attr.attr,
  2671. &dev_attr_fan3_input.attr,
  2672. &dev_attr_fan3_label.attr.attr,
  2673. &dev_attr_temp1_input.attr,
  2674. NULL
  2675. };
  2676. static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
  2677. struct attribute *attr, int idx)
  2678. {
  2679. struct device *dev = kobj_to_dev(kobj);
  2680. struct asus_wmi *asus = dev_get_drvdata(dev->parent);
  2681. u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
  2682. if (attr == &dev_attr_pwm1.attr) {
  2683. if (asus->fan_type != FAN_TYPE_AGFN)
  2684. return 0;
  2685. } else if (attr == &dev_attr_fan1_input.attr
  2686. || attr == &dev_attr_fan1_label.attr.attr
  2687. || attr == &dev_attr_pwm1_enable.attr) {
  2688. if (asus->fan_type == FAN_TYPE_NONE)
  2689. return 0;
  2690. } else if (attr == &dev_attr_fan2_input.attr
  2691. || attr == &dev_attr_fan2_label.attr.attr
  2692. || attr == &dev_attr_pwm2_enable.attr) {
  2693. if (asus->gpu_fan_type == FAN_TYPE_NONE)
  2694. return 0;
  2695. } else if (attr == &dev_attr_fan3_input.attr
  2696. || attr == &dev_attr_fan3_label.attr.attr
  2697. || attr == &dev_attr_pwm3_enable.attr) {
  2698. if (asus->mid_fan_type == FAN_TYPE_NONE)
  2699. return 0;
  2700. } else if (attr == &dev_attr_temp1_input.attr) {
  2701. int err = asus_wmi_get_devstate(asus,
  2702. ASUS_WMI_DEVID_THERMAL_CTRL,
  2703. &value);
  2704. if (err < 0)
  2705. return 0; /* can't return negative here */
  2706. /*
  2707. * If the temperature value in deci-Kelvin is near the absolute
  2708. * zero temperature, something is clearly wrong
  2709. */
  2710. if (value == 0 || value == 1)
  2711. return 0;
  2712. }
  2713. return attr->mode;
  2714. }
  2715. static const struct attribute_group hwmon_attribute_group = {
  2716. .is_visible = asus_hwmon_sysfs_is_visible,
  2717. .attrs = hwmon_attributes
  2718. };
  2719. __ATTRIBUTE_GROUPS(hwmon_attribute);
  2720. static int asus_wmi_hwmon_init(struct asus_wmi *asus)
  2721. {
  2722. struct device *dev = &asus->platform_device->dev;
  2723. struct device *hwmon;
  2724. hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
  2725. hwmon_attribute_groups);
  2726. if (IS_ERR(hwmon)) {
  2727. pr_err("Could not register asus hwmon device\n");
  2728. return PTR_ERR(hwmon);
  2729. }
  2730. return 0;
  2731. }
  2732. static int asus_wmi_fan_init(struct asus_wmi *asus)
  2733. {
  2734. asus->gpu_fan_type = FAN_TYPE_NONE;
  2735. asus->mid_fan_type = FAN_TYPE_NONE;
  2736. asus->fan_type = FAN_TYPE_NONE;
  2737. asus->agfn_pwm = -1;
  2738. if (asus->driver->quirks->wmi_ignore_fan)
  2739. asus->fan_type = FAN_TYPE_NONE;
  2740. else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
  2741. asus->fan_type = FAN_TYPE_SPEC83;
  2742. else if (asus_wmi_has_agfn_fan(asus))
  2743. asus->fan_type = FAN_TYPE_AGFN;
  2744. /* Modern models like G713 also have GPU fan control */
  2745. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
  2746. asus->gpu_fan_type = FAN_TYPE_SPEC83;
  2747. /* Some models also have a center/middle fan */
  2748. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL))
  2749. asus->mid_fan_type = FAN_TYPE_SPEC83;
  2750. if (asus->fan_type == FAN_TYPE_NONE)
  2751. return -ENODEV;
  2752. asus_fan_set_auto(asus);
  2753. asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
  2754. return 0;
  2755. }
  2756. /* Fan mode *******************************************************************/
  2757. static int fan_boost_mode_check_present(struct asus_wmi *asus)
  2758. {
  2759. u32 result;
  2760. int err;
  2761. asus->fan_boost_mode_available = false;
  2762. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
  2763. &result);
  2764. if (err) {
  2765. if (err == -ENODEV)
  2766. return 0;
  2767. else
  2768. return err;
  2769. }
  2770. if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
  2771. (result & ASUS_FAN_BOOST_MODES_MASK)) {
  2772. asus->fan_boost_mode_available = true;
  2773. asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
  2774. }
  2775. return 0;
  2776. }
  2777. static int fan_boost_mode_write(struct asus_wmi *asus)
  2778. {
  2779. u32 retval;
  2780. u8 value;
  2781. int err;
  2782. value = asus->fan_boost_mode;
  2783. pr_info("Set fan boost mode: %u\n", value);
  2784. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
  2785. &retval);
  2786. sysfs_notify(&asus->platform_device->dev.kobj, NULL,
  2787. "fan_boost_mode");
  2788. if (err) {
  2789. pr_warn("Failed to set fan boost mode: %d\n", err);
  2790. return err;
  2791. }
  2792. if (retval != 1) {
  2793. pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
  2794. retval);
  2795. return -EIO;
  2796. }
  2797. return 0;
  2798. }
  2799. static int fan_boost_mode_switch_next(struct asus_wmi *asus)
  2800. {
  2801. u8 mask = asus->fan_boost_mode_mask;
  2802. if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
  2803. if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
  2804. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
  2805. else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
  2806. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
  2807. } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
  2808. if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
  2809. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
  2810. else
  2811. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
  2812. } else {
  2813. asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
  2814. }
  2815. return fan_boost_mode_write(asus);
  2816. }
  2817. static ssize_t fan_boost_mode_show(struct device *dev,
  2818. struct device_attribute *attr, char *buf)
  2819. {
  2820. struct asus_wmi *asus = dev_get_drvdata(dev);
  2821. return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
  2822. }
  2823. static ssize_t fan_boost_mode_store(struct device *dev,
  2824. struct device_attribute *attr,
  2825. const char *buf, size_t count)
  2826. {
  2827. struct asus_wmi *asus = dev_get_drvdata(dev);
  2828. u8 mask = asus->fan_boost_mode_mask;
  2829. u8 new_mode;
  2830. int result;
  2831. result = kstrtou8(buf, 10, &new_mode);
  2832. if (result < 0) {
  2833. pr_warn("Trying to store invalid value\n");
  2834. return result;
  2835. }
  2836. if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
  2837. if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
  2838. return -EINVAL;
  2839. } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
  2840. if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
  2841. return -EINVAL;
  2842. } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
  2843. return -EINVAL;
  2844. }
  2845. asus->fan_boost_mode = new_mode;
  2846. fan_boost_mode_write(asus);
  2847. return count;
  2848. }
  2849. // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
  2850. static DEVICE_ATTR_RW(fan_boost_mode);
  2851. /* Custom fan curves **********************************************************/
  2852. static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
  2853. {
  2854. int i;
  2855. for (i = 0; i < FAN_CURVE_POINTS; i++) {
  2856. data->temps[i] = buf[i];
  2857. }
  2858. for (i = 0; i < FAN_CURVE_POINTS; i++) {
  2859. data->percents[i] =
  2860. 255 * buf[i + FAN_CURVE_POINTS] / 100;
  2861. }
  2862. }
  2863. static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
  2864. {
  2865. struct fan_curve_data *curves;
  2866. u8 buf[FAN_CURVE_BUF_LEN];
  2867. int err, fan_idx;
  2868. u8 mode = 0;
  2869. if (asus->throttle_thermal_policy_dev)
  2870. mode = asus->throttle_thermal_policy_mode;
  2871. /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
  2872. if (mode == 2)
  2873. mode = 1;
  2874. else if (mode == 1)
  2875. mode = 2;
  2876. err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
  2877. FAN_CURVE_BUF_LEN);
  2878. if (err) {
  2879. pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
  2880. return err;
  2881. }
  2882. fan_idx = FAN_CURVE_DEV_CPU;
  2883. if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
  2884. fan_idx = FAN_CURVE_DEV_GPU;
  2885. if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE)
  2886. fan_idx = FAN_CURVE_DEV_MID;
  2887. curves = &asus->custom_fan_curves[fan_idx];
  2888. curves->device_id = fan_dev;
  2889. fan_curve_copy_from_buf(curves, buf);
  2890. return 0;
  2891. }
  2892. /* Check if capability exists, and populate defaults */
  2893. static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
  2894. u32 fan_dev)
  2895. {
  2896. int err;
  2897. *available = false;
  2898. if (asus->fan_type == FAN_TYPE_NONE)
  2899. return 0;
  2900. err = fan_curve_get_factory_default(asus, fan_dev);
  2901. if (err) {
  2902. return 0;
  2903. }
  2904. *available = true;
  2905. return 0;
  2906. }
  2907. /* Determine which fan the attribute is for if SENSOR_ATTR */
  2908. static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
  2909. struct device_attribute *attr)
  2910. {
  2911. int index = to_sensor_dev_attr(attr)->index;
  2912. return &asus->custom_fan_curves[index];
  2913. }
  2914. /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
  2915. static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
  2916. struct device_attribute *attr)
  2917. {
  2918. int nr = to_sensor_dev_attr_2(attr)->nr;
  2919. return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK];
  2920. }
  2921. static ssize_t fan_curve_show(struct device *dev,
  2922. struct device_attribute *attr, char *buf)
  2923. {
  2924. struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
  2925. struct asus_wmi *asus = dev_get_drvdata(dev);
  2926. struct fan_curve_data *data;
  2927. int value, pwm, index;
  2928. data = fan_curve_attr_2_select(asus, attr);
  2929. pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
  2930. index = dev_attr->index;
  2931. if (pwm)
  2932. value = data->percents[index];
  2933. else
  2934. value = data->temps[index];
  2935. return sysfs_emit(buf, "%d\n", value);
  2936. }
  2937. /*
  2938. * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
  2939. */
  2940. static int fan_curve_write(struct asus_wmi *asus,
  2941. struct fan_curve_data *data)
  2942. {
  2943. u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
  2944. u8 *percents = data->percents;
  2945. u8 *temps = data->temps;
  2946. int ret, i, shift = 0;
  2947. if (!data->enabled)
  2948. return 0;
  2949. for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
  2950. arg1 += (temps[i]) << shift;
  2951. arg2 += (temps[i + 4]) << shift;
  2952. /* Scale to percentage for device */
  2953. arg3 += (100 * percents[i] / 255) << shift;
  2954. arg4 += (100 * percents[i + 4] / 255) << shift;
  2955. shift += 8;
  2956. }
  2957. return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
  2958. data->device_id,
  2959. arg1, arg2, arg3, arg4, &ret);
  2960. }
  2961. static ssize_t fan_curve_store(struct device *dev,
  2962. struct device_attribute *attr, const char *buf,
  2963. size_t count)
  2964. {
  2965. struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
  2966. struct asus_wmi *asus = dev_get_drvdata(dev);
  2967. struct fan_curve_data *data;
  2968. int err, pwm, index;
  2969. u8 value;
  2970. data = fan_curve_attr_2_select(asus, attr);
  2971. pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
  2972. index = dev_attr->index;
  2973. err = kstrtou8(buf, 10, &value);
  2974. if (err < 0)
  2975. return err;
  2976. if (pwm)
  2977. data->percents[index] = value;
  2978. else
  2979. data->temps[index] = value;
  2980. /*
  2981. * Mark as disabled so the user has to explicitly enable to apply a
  2982. * changed fan curve. This prevents potential lockups from writing out
  2983. * many changes as one-write-per-change.
  2984. */
  2985. data->enabled = false;
  2986. return count;
  2987. }
  2988. static ssize_t fan_curve_enable_show(struct device *dev,
  2989. struct device_attribute *attr, char *buf)
  2990. {
  2991. struct asus_wmi *asus = dev_get_drvdata(dev);
  2992. struct fan_curve_data *data;
  2993. int out = 2;
  2994. data = fan_curve_attr_select(asus, attr);
  2995. if (data->enabled)
  2996. out = 1;
  2997. return sysfs_emit(buf, "%d\n", out);
  2998. }
  2999. static ssize_t fan_curve_enable_store(struct device *dev,
  3000. struct device_attribute *attr,
  3001. const char *buf, size_t count)
  3002. {
  3003. struct asus_wmi *asus = dev_get_drvdata(dev);
  3004. struct fan_curve_data *data;
  3005. int value, err;
  3006. data = fan_curve_attr_select(asus, attr);
  3007. err = kstrtoint(buf, 10, &value);
  3008. if (err < 0)
  3009. return err;
  3010. switch (value) {
  3011. case 1:
  3012. data->enabled = true;
  3013. break;
  3014. case 2:
  3015. data->enabled = false;
  3016. break;
  3017. /*
  3018. * Auto + reset the fan curve data to defaults. Make it an explicit
  3019. * option so that users don't accidentally overwrite a set fan curve.
  3020. */
  3021. case 3:
  3022. err = fan_curve_get_factory_default(asus, data->device_id);
  3023. if (err)
  3024. return err;
  3025. data->enabled = false;
  3026. break;
  3027. default:
  3028. return -EINVAL;
  3029. }
  3030. if (data->enabled) {
  3031. err = fan_curve_write(asus, data);
  3032. if (err)
  3033. return err;
  3034. } else {
  3035. /*
  3036. * For machines with throttle this is the only way to reset fans
  3037. * to default mode of operation (does not erase curve data).
  3038. */
  3039. if (asus->throttle_thermal_policy_dev) {
  3040. err = throttle_thermal_policy_write(asus);
  3041. if (err)
  3042. return err;
  3043. /* Similar is true for laptops with this fan */
  3044. } else if (asus->fan_type == FAN_TYPE_SPEC83) {
  3045. err = asus_fan_set_auto(asus);
  3046. if (err)
  3047. return err;
  3048. } else {
  3049. /* Safeguard against fautly ACPI tables */
  3050. err = fan_curve_get_factory_default(asus, data->device_id);
  3051. if (err)
  3052. return err;
  3053. err = fan_curve_write(asus, data);
  3054. if (err)
  3055. return err;
  3056. }
  3057. }
  3058. return count;
  3059. }
  3060. /* CPU */
  3061. static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
  3062. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
  3063. FAN_CURVE_DEV_CPU, 0);
  3064. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
  3065. FAN_CURVE_DEV_CPU, 1);
  3066. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
  3067. FAN_CURVE_DEV_CPU, 2);
  3068. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
  3069. FAN_CURVE_DEV_CPU, 3);
  3070. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
  3071. FAN_CURVE_DEV_CPU, 4);
  3072. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
  3073. FAN_CURVE_DEV_CPU, 5);
  3074. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
  3075. FAN_CURVE_DEV_CPU, 6);
  3076. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
  3077. FAN_CURVE_DEV_CPU, 7);
  3078. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
  3079. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
  3080. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
  3081. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
  3082. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
  3083. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
  3084. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
  3085. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
  3086. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
  3087. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
  3088. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
  3089. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
  3090. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
  3091. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
  3092. static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
  3093. FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
  3094. /* GPU */
  3095. static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
  3096. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
  3097. FAN_CURVE_DEV_GPU, 0);
  3098. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
  3099. FAN_CURVE_DEV_GPU, 1);
  3100. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
  3101. FAN_CURVE_DEV_GPU, 2);
  3102. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
  3103. FAN_CURVE_DEV_GPU, 3);
  3104. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
  3105. FAN_CURVE_DEV_GPU, 4);
  3106. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
  3107. FAN_CURVE_DEV_GPU, 5);
  3108. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
  3109. FAN_CURVE_DEV_GPU, 6);
  3110. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
  3111. FAN_CURVE_DEV_GPU, 7);
  3112. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
  3113. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
  3114. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
  3115. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
  3116. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
  3117. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
  3118. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
  3119. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
  3120. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
  3121. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
  3122. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
  3123. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
  3124. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
  3125. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
  3126. static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
  3127. FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
  3128. /* MID */
  3129. static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID);
  3130. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve,
  3131. FAN_CURVE_DEV_MID, 0);
  3132. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve,
  3133. FAN_CURVE_DEV_MID, 1);
  3134. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve,
  3135. FAN_CURVE_DEV_MID, 2);
  3136. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve,
  3137. FAN_CURVE_DEV_MID, 3);
  3138. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve,
  3139. FAN_CURVE_DEV_MID, 4);
  3140. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve,
  3141. FAN_CURVE_DEV_MID, 5);
  3142. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve,
  3143. FAN_CURVE_DEV_MID, 6);
  3144. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve,
  3145. FAN_CURVE_DEV_MID, 7);
  3146. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve,
  3147. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0);
  3148. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve,
  3149. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1);
  3150. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve,
  3151. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2);
  3152. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve,
  3153. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3);
  3154. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve,
  3155. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4);
  3156. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve,
  3157. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5);
  3158. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve,
  3159. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6);
  3160. static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve,
  3161. FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7);
  3162. static struct attribute *asus_fan_curve_attr[] = {
  3163. /* CPU */
  3164. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  3165. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  3166. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  3167. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  3168. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  3169. &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
  3170. &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
  3171. &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
  3172. &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
  3173. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  3174. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  3175. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  3176. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  3177. &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
  3178. &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
  3179. &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
  3180. &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
  3181. /* GPU */
  3182. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  3183. &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
  3184. &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
  3185. &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
  3186. &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
  3187. &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
  3188. &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
  3189. &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
  3190. &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
  3191. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  3192. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  3193. &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
  3194. &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
  3195. &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
  3196. &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
  3197. &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
  3198. &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
  3199. /* MID */
  3200. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  3201. &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
  3202. &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
  3203. &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
  3204. &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
  3205. &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
  3206. &sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr,
  3207. &sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr,
  3208. &sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr,
  3209. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  3210. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  3211. &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
  3212. &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
  3213. &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
  3214. &sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr,
  3215. &sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr,
  3216. &sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr,
  3217. NULL
  3218. };
  3219. static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
  3220. struct attribute *attr, int idx)
  3221. {
  3222. struct device *dev = kobj_to_dev(kobj);
  3223. struct asus_wmi *asus = dev_get_drvdata(dev->parent);
  3224. /*
  3225. * Check the char instead of casting attr as there are two attr types
  3226. * involved here (attr1 and attr2)
  3227. */
  3228. if (asus->cpu_fan_curve_available && attr->name[3] == '1')
  3229. return 0644;
  3230. if (asus->gpu_fan_curve_available && attr->name[3] == '2')
  3231. return 0644;
  3232. if (asus->mid_fan_curve_available && attr->name[3] == '3')
  3233. return 0644;
  3234. return 0;
  3235. }
  3236. static const struct attribute_group asus_fan_curve_attr_group = {
  3237. .is_visible = asus_fan_curve_is_visible,
  3238. .attrs = asus_fan_curve_attr,
  3239. };
  3240. __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
  3241. /*
  3242. * Must be initialised after throttle_thermal_policy_dev is set as
  3243. * we check the status of throttle_thermal_policy_dev during init.
  3244. */
  3245. static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
  3246. {
  3247. struct device *dev = &asus->platform_device->dev;
  3248. struct device *hwmon;
  3249. int err;
  3250. err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
  3251. ASUS_WMI_DEVID_CPU_FAN_CURVE);
  3252. if (err) {
  3253. pr_debug("%s, checked 0x%08x, failed: %d\n",
  3254. __func__, ASUS_WMI_DEVID_CPU_FAN_CURVE, err);
  3255. return err;
  3256. }
  3257. err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
  3258. ASUS_WMI_DEVID_GPU_FAN_CURVE);
  3259. if (err) {
  3260. pr_debug("%s, checked 0x%08x, failed: %d\n",
  3261. __func__, ASUS_WMI_DEVID_GPU_FAN_CURVE, err);
  3262. return err;
  3263. }
  3264. err = fan_curve_check_present(asus, &asus->mid_fan_curve_available,
  3265. ASUS_WMI_DEVID_MID_FAN_CURVE);
  3266. if (err) {
  3267. pr_debug("%s, checked 0x%08x, failed: %d\n",
  3268. __func__, ASUS_WMI_DEVID_MID_FAN_CURVE, err);
  3269. return err;
  3270. }
  3271. if (!asus->cpu_fan_curve_available
  3272. && !asus->gpu_fan_curve_available
  3273. && !asus->mid_fan_curve_available)
  3274. return 0;
  3275. hwmon = devm_hwmon_device_register_with_groups(
  3276. dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
  3277. if (IS_ERR(hwmon)) {
  3278. dev_err(dev,
  3279. "Could not register asus_custom_fan_curve device\n");
  3280. return PTR_ERR(hwmon);
  3281. }
  3282. return 0;
  3283. }
  3284. /* Throttle thermal policy ****************************************************/
  3285. static int throttle_thermal_policy_write(struct asus_wmi *asus)
  3286. {
  3287. u8 value;
  3288. int err;
  3289. if (asus->throttle_thermal_policy_dev == ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY_VIVO) {
  3290. switch (asus->throttle_thermal_policy_mode) {
  3291. case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
  3292. value = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT_VIVO;
  3293. break;
  3294. case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
  3295. value = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST_VIVO;
  3296. break;
  3297. case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
  3298. value = ASUS_THROTTLE_THERMAL_POLICY_SILENT_VIVO;
  3299. break;
  3300. default:
  3301. return -EINVAL;
  3302. }
  3303. } else {
  3304. value = asus->throttle_thermal_policy_mode;
  3305. }
  3306. /* Some machines do not return an error code as a result, so we ignore it */
  3307. err = asus_wmi_set_devstate(asus->throttle_thermal_policy_dev, value, NULL);
  3308. sysfs_notify(&asus->platform_device->dev.kobj, NULL,
  3309. "throttle_thermal_policy");
  3310. if (err) {
  3311. pr_warn("Failed to set throttle thermal policy: %d\n", err);
  3312. return err;
  3313. }
  3314. /* Must set to disabled if mode is toggled */
  3315. if (asus->cpu_fan_curve_available)
  3316. asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
  3317. if (asus->gpu_fan_curve_available)
  3318. asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
  3319. if (asus->mid_fan_curve_available)
  3320. asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
  3321. return 0;
  3322. }
  3323. static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
  3324. {
  3325. if (!asus->throttle_thermal_policy_dev)
  3326. return 0;
  3327. asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
  3328. return throttle_thermal_policy_write(asus);
  3329. }
  3330. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  3331. static ssize_t throttle_thermal_policy_show(struct device *dev,
  3332. struct device_attribute *attr, char *buf)
  3333. {
  3334. struct asus_wmi *asus = dev_get_drvdata(dev);
  3335. u8 mode = asus->throttle_thermal_policy_mode;
  3336. return sysfs_emit(buf, "%d\n", mode);
  3337. }
  3338. static ssize_t throttle_thermal_policy_store(struct device *dev,
  3339. struct device_attribute *attr,
  3340. const char *buf, size_t count)
  3341. {
  3342. struct asus_wmi *asus = dev_get_drvdata(dev);
  3343. u8 new_mode;
  3344. int result;
  3345. int err;
  3346. result = kstrtou8(buf, 10, &new_mode);
  3347. if (result < 0)
  3348. return result;
  3349. if (new_mode > PLATFORM_PROFILE_MAX)
  3350. return -EINVAL;
  3351. asus->throttle_thermal_policy_mode = new_mode;
  3352. err = throttle_thermal_policy_write(asus);
  3353. if (err)
  3354. return err;
  3355. /*
  3356. * Ensure that platform_profile updates userspace with the change to ensure
  3357. * that platform_profile and throttle_thermal_policy_mode are in sync.
  3358. */
  3359. platform_profile_notify(asus->ppdev);
  3360. return count;
  3361. }
  3362. /*
  3363. * Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
  3364. */
  3365. static DEVICE_ATTR_RW(throttle_thermal_policy);
  3366. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  3367. /* Platform profile ***********************************************************/
  3368. static int asus_wmi_platform_profile_get(struct device *dev,
  3369. enum platform_profile_option *profile)
  3370. {
  3371. struct asus_wmi *asus;
  3372. int tp;
  3373. asus = dev_get_drvdata(dev);
  3374. tp = asus->throttle_thermal_policy_mode;
  3375. switch (tp) {
  3376. case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
  3377. *profile = PLATFORM_PROFILE_BALANCED;
  3378. break;
  3379. case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
  3380. *profile = PLATFORM_PROFILE_PERFORMANCE;
  3381. break;
  3382. case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
  3383. *profile = PLATFORM_PROFILE_QUIET;
  3384. break;
  3385. default:
  3386. return -EINVAL;
  3387. }
  3388. return 0;
  3389. }
  3390. static int asus_wmi_platform_profile_set(struct device *dev,
  3391. enum platform_profile_option profile)
  3392. {
  3393. struct asus_wmi *asus;
  3394. int tp;
  3395. asus = dev_get_drvdata(dev);
  3396. switch (profile) {
  3397. case PLATFORM_PROFILE_PERFORMANCE:
  3398. tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
  3399. break;
  3400. case PLATFORM_PROFILE_BALANCED:
  3401. tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
  3402. break;
  3403. case PLATFORM_PROFILE_QUIET:
  3404. tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
  3405. break;
  3406. default:
  3407. return -EOPNOTSUPP;
  3408. }
  3409. asus->throttle_thermal_policy_mode = tp;
  3410. return throttle_thermal_policy_write(asus);
  3411. }
  3412. static int asus_wmi_platform_profile_probe(void *drvdata, unsigned long *choices)
  3413. {
  3414. set_bit(PLATFORM_PROFILE_QUIET, choices);
  3415. set_bit(PLATFORM_PROFILE_BALANCED, choices);
  3416. set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
  3417. return 0;
  3418. }
  3419. static const struct platform_profile_ops asus_wmi_platform_profile_ops = {
  3420. .probe = asus_wmi_platform_profile_probe,
  3421. .profile_get = asus_wmi_platform_profile_get,
  3422. .profile_set = asus_wmi_platform_profile_set,
  3423. };
  3424. static int platform_profile_setup(struct asus_wmi *asus)
  3425. {
  3426. struct device *dev = &asus->platform_device->dev;
  3427. int err;
  3428. /*
  3429. * Not an error if a component platform_profile relies on is unavailable
  3430. * so early return, skipping the setup of platform_profile.
  3431. */
  3432. if (!asus->throttle_thermal_policy_dev)
  3433. return 0;
  3434. /*
  3435. * We need to set the default thermal profile during probe or otherwise
  3436. * the system will often remain in silent mode, causing low performance.
  3437. */
  3438. err = throttle_thermal_policy_set_default(asus);
  3439. if (err < 0) {
  3440. pr_warn("Failed to set default thermal profile\n");
  3441. return err;
  3442. }
  3443. dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
  3444. asus->ppdev = devm_platform_profile_register(dev, "asus-wmi", asus,
  3445. &asus_wmi_platform_profile_ops);
  3446. if (IS_ERR(asus->ppdev)) {
  3447. dev_err(dev, "Failed to register a platform_profile class device\n");
  3448. return PTR_ERR(asus->ppdev);
  3449. }
  3450. asus->platform_profile_support = true;
  3451. return 0;
  3452. }
  3453. /* Backlight ******************************************************************/
  3454. static int read_backlight_power(struct asus_wmi *asus)
  3455. {
  3456. int ret;
  3457. if (asus->driver->quirks->store_backlight_power)
  3458. ret = !asus->driver->panel_power;
  3459. else
  3460. ret = asus_wmi_get_devstate_simple(asus,
  3461. ASUS_WMI_DEVID_BACKLIGHT);
  3462. if (ret < 0)
  3463. return ret;
  3464. return ret ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
  3465. }
  3466. static int read_brightness_max(struct asus_wmi *asus)
  3467. {
  3468. u32 retval;
  3469. int err;
  3470. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  3471. if (err < 0)
  3472. return err;
  3473. retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
  3474. retval >>= 8;
  3475. if (!retval)
  3476. return -ENODEV;
  3477. return retval;
  3478. }
  3479. static int read_brightness(struct backlight_device *bd)
  3480. {
  3481. struct asus_wmi *asus = bl_get_data(bd);
  3482. u32 retval;
  3483. int err;
  3484. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
  3485. if (err < 0)
  3486. return err;
  3487. return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
  3488. }
  3489. static u32 get_scalar_command(struct backlight_device *bd)
  3490. {
  3491. struct asus_wmi *asus = bl_get_data(bd);
  3492. u32 ctrl_param = 0;
  3493. if ((asus->driver->brightness < bd->props.brightness) ||
  3494. bd->props.brightness == bd->props.max_brightness)
  3495. ctrl_param = 0x00008001;
  3496. else if ((asus->driver->brightness > bd->props.brightness) ||
  3497. bd->props.brightness == 0)
  3498. ctrl_param = 0x00008000;
  3499. asus->driver->brightness = bd->props.brightness;
  3500. return ctrl_param;
  3501. }
  3502. static int update_bl_status(struct backlight_device *bd)
  3503. {
  3504. struct asus_wmi *asus = bl_get_data(bd);
  3505. u32 ctrl_param;
  3506. int power, err = 0;
  3507. power = read_backlight_power(asus);
  3508. if (power != -ENODEV && bd->props.power != power) {
  3509. ctrl_param = !!(bd->props.power == BACKLIGHT_POWER_ON);
  3510. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
  3511. ctrl_param, NULL);
  3512. if (asus->driver->quirks->store_backlight_power)
  3513. asus->driver->panel_power = bd->props.power;
  3514. /* When using scalar brightness, updating the brightness
  3515. * will mess with the backlight power */
  3516. if (asus->driver->quirks->scalar_panel_brightness)
  3517. return err;
  3518. }
  3519. if (asus->driver->quirks->scalar_panel_brightness)
  3520. ctrl_param = get_scalar_command(bd);
  3521. else
  3522. ctrl_param = bd->props.brightness;
  3523. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
  3524. ctrl_param, NULL);
  3525. return err;
  3526. }
  3527. static const struct backlight_ops asus_wmi_bl_ops = {
  3528. .get_brightness = read_brightness,
  3529. .update_status = update_bl_status,
  3530. };
  3531. static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
  3532. {
  3533. struct backlight_device *bd = asus->backlight_device;
  3534. int old = bd->props.brightness;
  3535. int new = old;
  3536. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  3537. new = code - NOTIFY_BRNUP_MIN + 1;
  3538. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  3539. new = code - NOTIFY_BRNDOWN_MIN;
  3540. bd->props.brightness = new;
  3541. backlight_update_status(bd);
  3542. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  3543. return old;
  3544. }
  3545. static int asus_wmi_backlight_init(struct asus_wmi *asus)
  3546. {
  3547. struct backlight_device *bd;
  3548. struct backlight_properties props;
  3549. int max;
  3550. int power;
  3551. max = read_brightness_max(asus);
  3552. if (max < 0)
  3553. return max;
  3554. power = read_backlight_power(asus);
  3555. if (power == -ENODEV)
  3556. power = BACKLIGHT_POWER_ON;
  3557. else if (power < 0)
  3558. return power;
  3559. memset(&props, 0, sizeof(struct backlight_properties));
  3560. props.type = BACKLIGHT_PLATFORM;
  3561. props.max_brightness = max;
  3562. bd = backlight_device_register(asus->driver->name,
  3563. &asus->platform_device->dev, asus,
  3564. &asus_wmi_bl_ops, &props);
  3565. if (IS_ERR(bd)) {
  3566. pr_err("Could not register backlight device\n");
  3567. return PTR_ERR(bd);
  3568. }
  3569. asus->backlight_device = bd;
  3570. if (asus->driver->quirks->store_backlight_power)
  3571. asus->driver->panel_power = power;
  3572. bd->props.brightness = read_brightness(bd);
  3573. bd->props.power = power;
  3574. backlight_update_status(bd);
  3575. asus->driver->brightness = bd->props.brightness;
  3576. return 0;
  3577. }
  3578. static void asus_wmi_backlight_exit(struct asus_wmi *asus)
  3579. {
  3580. backlight_device_unregister(asus->backlight_device);
  3581. asus->backlight_device = NULL;
  3582. }
  3583. static int is_display_toggle(int code)
  3584. {
  3585. /* display toggle keys */
  3586. if ((code >= 0x61 && code <= 0x67) ||
  3587. (code >= 0x8c && code <= 0x93) ||
  3588. (code >= 0xa0 && code <= 0xa7) ||
  3589. (code >= 0xd0 && code <= 0xd5))
  3590. return 1;
  3591. return 0;
  3592. }
  3593. /* Screenpad backlight *******************************************************/
  3594. static int read_screenpad_backlight_power(struct asus_wmi *asus)
  3595. {
  3596. int ret;
  3597. ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_SCREENPAD_POWER);
  3598. if (ret < 0)
  3599. return ret;
  3600. /* 1 == powered */
  3601. return ret ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
  3602. }
  3603. static int read_screenpad_brightness(struct backlight_device *bd)
  3604. {
  3605. struct asus_wmi *asus = bl_get_data(bd);
  3606. u32 retval;
  3607. int err;
  3608. err = read_screenpad_backlight_power(asus);
  3609. if (err < 0)
  3610. return err;
  3611. /* The device brightness can only be read if powered, so return stored */
  3612. if (err == BACKLIGHT_POWER_OFF)
  3613. return asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
  3614. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &retval);
  3615. if (err < 0)
  3616. return err;
  3617. return (retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK) - ASUS_SCREENPAD_BRIGHT_MIN;
  3618. }
  3619. static int update_screenpad_bl_status(struct backlight_device *bd)
  3620. {
  3621. struct asus_wmi *asus = bl_get_data(bd);
  3622. int power, err = 0;
  3623. u32 ctrl_param;
  3624. power = read_screenpad_backlight_power(asus);
  3625. if (power < 0)
  3626. return power;
  3627. if (bd->props.power != power) {
  3628. if (power != BACKLIGHT_POWER_ON) {
  3629. /* Only brightness > 0 can power it back on */
  3630. ctrl_param = asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
  3631. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT,
  3632. ctrl_param, NULL);
  3633. } else {
  3634. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_POWER, 0, NULL);
  3635. }
  3636. } else if (power == BACKLIGHT_POWER_ON) {
  3637. /* Only set brightness if powered on or we get invalid/unsync state */
  3638. ctrl_param = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
  3639. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT, ctrl_param, NULL);
  3640. }
  3641. /* Ensure brightness is stored to turn back on with */
  3642. if (err == 0)
  3643. asus->driver->screenpad_brightness = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
  3644. return err;
  3645. }
  3646. static const struct backlight_ops asus_screenpad_bl_ops = {
  3647. .get_brightness = read_screenpad_brightness,
  3648. .update_status = update_screenpad_bl_status,
  3649. .options = BL_CORE_SUSPENDRESUME,
  3650. };
  3651. static int asus_screenpad_init(struct asus_wmi *asus)
  3652. {
  3653. struct backlight_device *bd;
  3654. struct backlight_properties props;
  3655. int err, power;
  3656. int brightness = 0;
  3657. power = read_screenpad_backlight_power(asus);
  3658. if (power < 0)
  3659. return power;
  3660. if (power != BACKLIGHT_POWER_OFF) {
  3661. err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &brightness);
  3662. if (err < 0)
  3663. return err;
  3664. }
  3665. /* default to an acceptable min brightness on boot if too low */
  3666. if (brightness < ASUS_SCREENPAD_BRIGHT_MIN)
  3667. brightness = ASUS_SCREENPAD_BRIGHT_DEFAULT;
  3668. memset(&props, 0, sizeof(struct backlight_properties));
  3669. props.type = BACKLIGHT_RAW; /* ensure this bd is last to be picked */
  3670. props.max_brightness = ASUS_SCREENPAD_BRIGHT_MAX - ASUS_SCREENPAD_BRIGHT_MIN;
  3671. bd = backlight_device_register("asus_screenpad",
  3672. &asus->platform_device->dev, asus,
  3673. &asus_screenpad_bl_ops, &props);
  3674. if (IS_ERR(bd)) {
  3675. pr_err("Could not register backlight device\n");
  3676. return PTR_ERR(bd);
  3677. }
  3678. asus->screenpad_backlight_device = bd;
  3679. asus->driver->screenpad_brightness = brightness;
  3680. bd->props.brightness = brightness - ASUS_SCREENPAD_BRIGHT_MIN;
  3681. bd->props.power = power;
  3682. backlight_update_status(bd);
  3683. return 0;
  3684. }
  3685. static void asus_screenpad_exit(struct asus_wmi *asus)
  3686. {
  3687. backlight_device_unregister(asus->screenpad_backlight_device);
  3688. asus->screenpad_backlight_device = NULL;
  3689. }
  3690. /* Fn-lock ********************************************************************/
  3691. static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
  3692. {
  3693. u32 result;
  3694. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
  3695. return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
  3696. !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
  3697. }
  3698. static void asus_wmi_fnlock_update(struct asus_wmi *asus)
  3699. {
  3700. int mode = asus->fnlock_locked;
  3701. asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
  3702. }
  3703. /* WMI events *****************************************************************/
  3704. static int asus_wmi_get_event_code(union acpi_object *obj)
  3705. {
  3706. int code;
  3707. if (obj && obj->type == ACPI_TYPE_INTEGER)
  3708. code = (int)(obj->integer.value & WMI_EVENT_MASK);
  3709. else
  3710. code = -EIO;
  3711. return code;
  3712. }
  3713. static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
  3714. {
  3715. enum led_brightness led_value;
  3716. unsigned int key_value = 1;
  3717. bool autorelease = 1;
  3718. if (asus->driver->key_filter) {
  3719. asus->driver->key_filter(asus->driver, &code, &key_value,
  3720. &autorelease);
  3721. if (code == ASUS_WMI_KEY_IGNORE)
  3722. return;
  3723. }
  3724. if (acpi_video_get_backlight_type() == acpi_backlight_vendor &&
  3725. code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNDOWN_MAX) {
  3726. asus_wmi_backlight_notify(asus, code);
  3727. return;
  3728. }
  3729. scoped_guard(spinlock_irqsave, &asus_ref.lock)
  3730. led_value = asus->kbd_led_wk;
  3731. if (code == NOTIFY_KBD_BRTUP) {
  3732. kbd_led_set_by_kbd(asus, led_value + 1);
  3733. return;
  3734. }
  3735. if (code == NOTIFY_KBD_BRTDWN) {
  3736. kbd_led_set_by_kbd(asus, led_value - 1);
  3737. return;
  3738. }
  3739. if (code == NOTIFY_KBD_BRTTOGGLE) {
  3740. if (led_value >= ASUS_EV_MAX_BRIGHTNESS)
  3741. kbd_led_set_by_kbd(asus, 0);
  3742. else
  3743. kbd_led_set_by_kbd(asus, led_value + 1);
  3744. return;
  3745. }
  3746. if (code == NOTIFY_FNLOCK_TOGGLE) {
  3747. asus->fnlock_locked = !asus->fnlock_locked;
  3748. asus_wmi_fnlock_update(asus);
  3749. return;
  3750. }
  3751. if (code == asus->tablet_switch_event_code) {
  3752. asus_wmi_tablet_mode_get_state(asus);
  3753. return;
  3754. }
  3755. if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
  3756. if (asus->fan_boost_mode_available)
  3757. fan_boost_mode_switch_next(asus);
  3758. if (asus->throttle_thermal_policy_dev)
  3759. platform_profile_cycle();
  3760. return;
  3761. }
  3762. if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
  3763. return;
  3764. if (!sparse_keymap_report_event(asus->inputdev, code,
  3765. key_value, autorelease))
  3766. pr_info("Unknown key code 0x%x\n", code);
  3767. }
  3768. static void asus_wmi_notify(union acpi_object *obj, void *context)
  3769. {
  3770. struct asus_wmi *asus = context;
  3771. int code = asus_wmi_get_event_code(obj);
  3772. if (code < 0) {
  3773. pr_warn("Failed to get notify code: %d\n", code);
  3774. return;
  3775. }
  3776. asus_wmi_handle_event_code(code, asus);
  3777. }
  3778. /* Sysfs **********************************************************************/
  3779. static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
  3780. const char *buf, size_t count)
  3781. {
  3782. u32 retval;
  3783. int err, value;
  3784. value = asus_wmi_get_devstate_simple(asus, devid);
  3785. if (value < 0)
  3786. return value;
  3787. err = kstrtoint(buf, 0, &value);
  3788. if (err)
  3789. return err;
  3790. err = asus_wmi_set_devstate(devid, value, &retval);
  3791. if (err < 0)
  3792. return err;
  3793. return count;
  3794. }
  3795. static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
  3796. {
  3797. int value = asus_wmi_get_devstate_simple(asus, devid);
  3798. if (value < 0)
  3799. return value;
  3800. return sysfs_emit(buf, "%d\n", value);
  3801. }
  3802. #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
  3803. static ssize_t show_##_name(struct device *dev, \
  3804. struct device_attribute *attr, \
  3805. char *buf) \
  3806. { \
  3807. struct asus_wmi *asus = dev_get_drvdata(dev); \
  3808. \
  3809. return show_sys_wmi(asus, _cm, buf); \
  3810. } \
  3811. static ssize_t store_##_name(struct device *dev, \
  3812. struct device_attribute *attr, \
  3813. const char *buf, size_t count) \
  3814. { \
  3815. struct asus_wmi *asus = dev_get_drvdata(dev); \
  3816. \
  3817. return store_sys_wmi(asus, _cm, buf, count); \
  3818. } \
  3819. static struct device_attribute dev_attr_##_name = { \
  3820. .attr = { \
  3821. .name = __stringify(_name), \
  3822. .mode = _mode }, \
  3823. .show = show_##_name, \
  3824. .store = store_##_name, \
  3825. }
  3826. ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
  3827. ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
  3828. ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
  3829. ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
  3830. ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
  3831. static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
  3832. const char *buf, size_t count)
  3833. {
  3834. int value, rv;
  3835. rv = kstrtoint(buf, 0, &value);
  3836. if (rv)
  3837. return rv;
  3838. if (value < 0 || value > 2)
  3839. return -EINVAL;
  3840. rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
  3841. if (rv < 0)
  3842. return rv;
  3843. return count;
  3844. }
  3845. static DEVICE_ATTR_WO(cpufv);
  3846. static struct attribute *platform_attributes[] = {
  3847. &dev_attr_cpufv.attr,
  3848. &dev_attr_camera.attr,
  3849. &dev_attr_cardr.attr,
  3850. &dev_attr_touchpad.attr,
  3851. &dev_attr_lid_resume.attr,
  3852. &dev_attr_als_enable.attr,
  3853. &dev_attr_fan_boost_mode.attr,
  3854. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  3855. &dev_attr_charge_mode.attr,
  3856. &dev_attr_egpu_enable.attr,
  3857. &dev_attr_egpu_connected.attr,
  3858. &dev_attr_dgpu_disable.attr,
  3859. &dev_attr_gpu_mux_mode.attr,
  3860. &dev_attr_ppt_pl2_sppt.attr,
  3861. &dev_attr_ppt_pl1_spl.attr,
  3862. &dev_attr_ppt_fppt.attr,
  3863. &dev_attr_ppt_apu_sppt.attr,
  3864. &dev_attr_ppt_platform_sppt.attr,
  3865. &dev_attr_nv_dynamic_boost.attr,
  3866. &dev_attr_nv_temp_target.attr,
  3867. &dev_attr_mcu_powersave.attr,
  3868. &dev_attr_boot_sound.attr,
  3869. &dev_attr_panel_od.attr,
  3870. &dev_attr_mini_led_mode.attr,
  3871. &dev_attr_available_mini_led_mode.attr,
  3872. &dev_attr_throttle_thermal_policy.attr,
  3873. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  3874. NULL
  3875. };
  3876. static umode_t asus_sysfs_is_visible(struct kobject *kobj,
  3877. struct attribute *attr, int idx)
  3878. {
  3879. struct device *dev = kobj_to_dev(kobj);
  3880. struct asus_wmi *asus = dev_get_drvdata(dev);
  3881. bool ok = true;
  3882. int devid = -1;
  3883. if (attr == &dev_attr_camera.attr)
  3884. devid = ASUS_WMI_DEVID_CAMERA;
  3885. else if (attr == &dev_attr_cardr.attr)
  3886. devid = ASUS_WMI_DEVID_CARDREADER;
  3887. else if (attr == &dev_attr_touchpad.attr)
  3888. devid = ASUS_WMI_DEVID_TOUCHPAD;
  3889. else if (attr == &dev_attr_lid_resume.attr)
  3890. devid = ASUS_WMI_DEVID_LID_RESUME;
  3891. else if (attr == &dev_attr_als_enable.attr)
  3892. devid = ASUS_WMI_DEVID_ALS_ENABLE;
  3893. else if (attr == &dev_attr_fan_boost_mode.attr)
  3894. ok = asus->fan_boost_mode_available;
  3895. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  3896. if (attr == &dev_attr_charge_mode.attr)
  3897. devid = ASUS_WMI_DEVID_CHARGE_MODE;
  3898. else if (attr == &dev_attr_egpu_enable.attr)
  3899. ok = asus->egpu_enable_available;
  3900. else if (attr == &dev_attr_egpu_connected.attr)
  3901. devid = ASUS_WMI_DEVID_EGPU_CONNECTED;
  3902. else if (attr == &dev_attr_dgpu_disable.attr)
  3903. ok = asus->dgpu_disable_available;
  3904. else if (attr == &dev_attr_gpu_mux_mode.attr)
  3905. ok = asus->gpu_mux_dev != 0;
  3906. else if (attr == &dev_attr_fan_boost_mode.attr)
  3907. ok = asus->fan_boost_mode_available;
  3908. else if (attr == &dev_attr_throttle_thermal_policy.attr)
  3909. ok = asus->throttle_thermal_policy_dev != 0;
  3910. else if (attr == &dev_attr_ppt_pl2_sppt.attr)
  3911. devid = ASUS_WMI_DEVID_PPT_PL2_SPPT;
  3912. else if (attr == &dev_attr_ppt_pl1_spl.attr)
  3913. devid = ASUS_WMI_DEVID_PPT_PL1_SPL;
  3914. else if (attr == &dev_attr_ppt_fppt.attr)
  3915. devid = ASUS_WMI_DEVID_PPT_PL3_FPPT;
  3916. else if (attr == &dev_attr_ppt_apu_sppt.attr)
  3917. devid = ASUS_WMI_DEVID_PPT_APU_SPPT;
  3918. else if (attr == &dev_attr_ppt_platform_sppt.attr)
  3919. devid = ASUS_WMI_DEVID_PPT_PLAT_SPPT;
  3920. else if (attr == &dev_attr_nv_dynamic_boost.attr)
  3921. devid = ASUS_WMI_DEVID_NV_DYN_BOOST;
  3922. else if (attr == &dev_attr_nv_temp_target.attr)
  3923. devid = ASUS_WMI_DEVID_NV_THERM_TARGET;
  3924. else if (attr == &dev_attr_mcu_powersave.attr)
  3925. devid = ASUS_WMI_DEVID_MCU_POWERSAVE;
  3926. else if (attr == &dev_attr_boot_sound.attr)
  3927. devid = ASUS_WMI_DEVID_BOOT_SOUND;
  3928. else if (attr == &dev_attr_panel_od.attr)
  3929. devid = ASUS_WMI_DEVID_PANEL_OD;
  3930. else if (attr == &dev_attr_mini_led_mode.attr)
  3931. ok = asus->mini_led_dev_id != 0;
  3932. else if (attr == &dev_attr_available_mini_led_mode.attr)
  3933. ok = asus->mini_led_dev_id != 0;
  3934. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  3935. if (devid != -1) {
  3936. ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
  3937. pr_debug("%s called 0x%08x, ok: %x\n", __func__, devid, ok);
  3938. }
  3939. return ok ? attr->mode : 0;
  3940. }
  3941. static const struct attribute_group platform_attribute_group = {
  3942. .is_visible = asus_sysfs_is_visible,
  3943. .attrs = platform_attributes
  3944. };
  3945. static void asus_wmi_sysfs_exit(struct platform_device *device)
  3946. {
  3947. sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
  3948. }
  3949. static int asus_wmi_sysfs_init(struct platform_device *device)
  3950. {
  3951. return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
  3952. }
  3953. /* Platform device ************************************************************/
  3954. static int asus_wmi_platform_init(struct asus_wmi *asus)
  3955. {
  3956. struct device *dev = &asus->platform_device->dev;
  3957. char *wmi_uid;
  3958. int rv;
  3959. /* INIT enable hotkeys on some models */
  3960. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
  3961. pr_info("Initialization: %#x\n", rv);
  3962. /* We don't know yet what to do with this version... */
  3963. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
  3964. pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
  3965. asus->spec = rv;
  3966. }
  3967. /*
  3968. * The SFUN method probably allows the original driver to get the list
  3969. * of features supported by a given model. For now, 0x0100 or 0x0800
  3970. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  3971. * The significance of others is yet to be found.
  3972. */
  3973. if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
  3974. pr_info("SFUN value: %#x\n", rv);
  3975. asus->sfun = rv;
  3976. }
  3977. /*
  3978. * Eee PC and Notebooks seems to have different method_id for DSTS,
  3979. * but it may also be related to the BIOS's SPEC.
  3980. * Note, on most Eeepc, there is no way to check if a method exist
  3981. * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
  3982. * but once again, SPEC may probably be used for that kind of things.
  3983. *
  3984. * Additionally at least TUF Gaming series laptops return nothing for
  3985. * unknown methods, so the detection in this way is not possible.
  3986. *
  3987. * There is strong indication that only ACPI WMI devices that have _UID
  3988. * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
  3989. */
  3990. wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
  3991. if (!wmi_uid)
  3992. return -ENODEV;
  3993. if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
  3994. dev_info(dev, "Detected ASUSWMI, use DCTS\n");
  3995. asus->dsts_id = ASUS_WMI_METHODID_DCTS;
  3996. } else {
  3997. dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
  3998. asus->dsts_id = ASUS_WMI_METHODID_DSTS;
  3999. }
  4000. /* CWAP allow to define the behavior of the Fn+F2 key,
  4001. * this method doesn't seems to be present on Eee PCs */
  4002. if (asus->driver->quirks->wapf >= 0)
  4003. asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
  4004. asus->driver->quirks->wapf, NULL);
  4005. return 0;
  4006. }
  4007. /* debugfs ********************************************************************/
  4008. struct asus_wmi_debugfs_node {
  4009. struct asus_wmi *asus;
  4010. char *name;
  4011. int (*show) (struct seq_file *m, void *data);
  4012. };
  4013. static int show_dsts(struct seq_file *m, void *data)
  4014. {
  4015. struct asus_wmi *asus = m->private;
  4016. int err;
  4017. u32 retval = -1;
  4018. err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
  4019. if (err < 0)
  4020. return err;
  4021. seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
  4022. return 0;
  4023. }
  4024. static int show_devs(struct seq_file *m, void *data)
  4025. {
  4026. struct asus_wmi *asus = m->private;
  4027. int err;
  4028. u32 retval = -1;
  4029. err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
  4030. &retval);
  4031. if (err < 0)
  4032. return err;
  4033. seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
  4034. asus->debug.ctrl_param, retval);
  4035. return 0;
  4036. }
  4037. static int show_call(struct seq_file *m, void *data)
  4038. {
  4039. struct asus_wmi *asus = m->private;
  4040. struct bios_args args = {
  4041. .arg0 = asus->debug.dev_id,
  4042. .arg1 = asus->debug.ctrl_param,
  4043. };
  4044. struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
  4045. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  4046. union acpi_object *obj;
  4047. acpi_status status;
  4048. status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
  4049. 0, asus->debug.method_id,
  4050. &input, &output);
  4051. if (ACPI_FAILURE(status))
  4052. return -EIO;
  4053. obj = (union acpi_object *)output.pointer;
  4054. if (obj && obj->type == ACPI_TYPE_INTEGER)
  4055. seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
  4056. asus->debug.dev_id, asus->debug.ctrl_param,
  4057. (u32) obj->integer.value);
  4058. else
  4059. seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
  4060. asus->debug.dev_id, asus->debug.ctrl_param,
  4061. obj ? obj->type : -1);
  4062. kfree(obj);
  4063. return 0;
  4064. }
  4065. static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
  4066. {NULL, "devs", show_devs},
  4067. {NULL, "dsts", show_dsts},
  4068. {NULL, "call", show_call},
  4069. };
  4070. static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
  4071. {
  4072. struct asus_wmi_debugfs_node *node = inode->i_private;
  4073. return single_open(file, node->show, node->asus);
  4074. }
  4075. static const struct file_operations asus_wmi_debugfs_io_ops = {
  4076. .owner = THIS_MODULE,
  4077. .open = asus_wmi_debugfs_open,
  4078. .read = seq_read,
  4079. .llseek = seq_lseek,
  4080. .release = single_release,
  4081. };
  4082. static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
  4083. {
  4084. debugfs_remove_recursive(asus->debug.root);
  4085. }
  4086. static void asus_wmi_debugfs_init(struct asus_wmi *asus)
  4087. {
  4088. int i;
  4089. asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
  4090. debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
  4091. &asus->debug.method_id);
  4092. debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
  4093. &asus->debug.dev_id);
  4094. debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
  4095. &asus->debug.ctrl_param);
  4096. for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
  4097. struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
  4098. node->asus = asus;
  4099. debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  4100. asus->debug.root, node,
  4101. &asus_wmi_debugfs_io_ops);
  4102. }
  4103. }
  4104. /* Init / exit ****************************************************************/
  4105. static int asus_wmi_add(struct platform_device *pdev)
  4106. {
  4107. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  4108. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  4109. struct asus_wmi *asus;
  4110. acpi_status status;
  4111. int err;
  4112. u32 result;
  4113. asus = kzalloc_obj(struct asus_wmi);
  4114. if (!asus)
  4115. return -ENOMEM;
  4116. asus->driver = wdrv;
  4117. asus->platform_device = pdev;
  4118. wdrv->platform_device = pdev;
  4119. platform_set_drvdata(asus->platform_device, asus);
  4120. if (wdrv->detect_quirks)
  4121. wdrv->detect_quirks(asus->driver);
  4122. err = asus_wmi_platform_init(asus);
  4123. if (err)
  4124. goto fail_platform;
  4125. if (use_ally_mcu_hack == ASUS_WMI_ALLY_MCU_HACK_INIT) {
  4126. if (acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE)
  4127. && dmi_check_system(asus_rog_ally_device))
  4128. use_ally_mcu_hack = ASUS_WMI_ALLY_MCU_HACK_ENABLED;
  4129. if (dmi_match(DMI_BOARD_NAME, "RC71")) {
  4130. /*
  4131. * These steps ensure the device is in a valid good state, this is
  4132. * especially important for the Ally 1 after a reboot.
  4133. */
  4134. acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE,
  4135. ASUS_USB0_PWR_EC0_CSEE_ON);
  4136. msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
  4137. }
  4138. }
  4139. /* ensure defaults for tunables */
  4140. #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
  4141. asus->ppt_pl2_sppt = 5;
  4142. asus->ppt_pl1_spl = 5;
  4143. asus->ppt_apu_sppt = 5;
  4144. asus->ppt_platform_sppt = 5;
  4145. asus->ppt_fppt = 5;
  4146. asus->nv_dynamic_boost = 5;
  4147. asus->nv_temp_target = 75;
  4148. asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
  4149. asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
  4150. asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
  4151. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE))
  4152. asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE;
  4153. else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE2))
  4154. asus->mini_led_dev_id = ASUS_WMI_DEVID_MINI_LED_MODE2;
  4155. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX))
  4156. asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX;
  4157. else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX_VIVO))
  4158. asus->gpu_mux_dev = ASUS_WMI_DEVID_GPU_MUX_VIVO;
  4159. #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
  4160. asus->oobe_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_OOBE);
  4161. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY))
  4162. asus->throttle_thermal_policy_dev = ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY;
  4163. else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY_VIVO))
  4164. asus->throttle_thermal_policy_dev = ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY_VIVO;
  4165. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE))
  4166. asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE;
  4167. else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE2))
  4168. asus->kbd_rgb_dev = ASUS_WMI_DEVID_TUF_RGB_MODE2;
  4169. err = fan_boost_mode_check_present(asus);
  4170. if (err)
  4171. goto fail_fan_boost_mode;
  4172. err = platform_profile_setup(asus);
  4173. if (err)
  4174. goto fail_platform_profile_setup;
  4175. err = asus_wmi_sysfs_init(asus->platform_device);
  4176. if (err)
  4177. goto fail_sysfs;
  4178. err = asus_wmi_input_init(asus);
  4179. if (err)
  4180. goto fail_input;
  4181. err = asus_wmi_fan_init(asus); /* probably no problems on error */
  4182. err = asus_wmi_hwmon_init(asus);
  4183. if (err)
  4184. goto fail_hwmon;
  4185. err = asus_wmi_custom_fan_curve_init(asus);
  4186. if (err)
  4187. goto fail_custom_fan_curve;
  4188. err = asus_wmi_led_init(asus);
  4189. if (err)
  4190. goto fail_leds;
  4191. asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
  4192. if ((result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT)) ==
  4193. (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
  4194. asus->driver->wlan_ctrl_by_user = 1;
  4195. if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
  4196. err = asus_wmi_rfkill_init(asus);
  4197. if (err)
  4198. goto fail_rfkill;
  4199. }
  4200. if (asus->driver->quirks->wmi_force_als_set)
  4201. asus_wmi_set_als();
  4202. if (asus->driver->quirks->xusb2pr)
  4203. asus_wmi_set_xusb2pr(asus);
  4204. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  4205. err = asus_wmi_backlight_init(asus);
  4206. if (err && err != -ENODEV)
  4207. goto fail_backlight;
  4208. } else if (asus->driver->quirks->wmi_backlight_set_devstate)
  4209. err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
  4210. if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT)) {
  4211. err = asus_screenpad_init(asus);
  4212. if (err && err != -ENODEV)
  4213. goto fail_screenpad;
  4214. }
  4215. if (asus_wmi_has_fnlock_key(asus)) {
  4216. asus->fnlock_locked = fnlock_default;
  4217. asus_wmi_fnlock_update(asus);
  4218. }
  4219. status = wmi_install_notify_handler(asus->driver->event_guid,
  4220. asus_wmi_notify, asus);
  4221. if (ACPI_FAILURE(status)) {
  4222. pr_err("Unable to register notify handler - %d\n", status);
  4223. err = -ENODEV;
  4224. goto fail_wmi_handler;
  4225. }
  4226. if (asus->driver->i8042_filter) {
  4227. err = i8042_install_filter(asus->driver->i8042_filter, NULL);
  4228. if (err)
  4229. pr_warn("Unable to install key filter - %d\n", err);
  4230. }
  4231. asus_wmi_battery_init(asus);
  4232. asus_wmi_debugfs_init(asus);
  4233. return 0;
  4234. fail_wmi_handler:
  4235. asus_wmi_backlight_exit(asus);
  4236. fail_backlight:
  4237. asus_wmi_rfkill_exit(asus);
  4238. fail_screenpad:
  4239. asus_screenpad_exit(asus);
  4240. fail_rfkill:
  4241. asus_wmi_led_exit(asus);
  4242. fail_leds:
  4243. fail_hwmon:
  4244. asus_wmi_input_exit(asus);
  4245. fail_input:
  4246. asus_wmi_sysfs_exit(asus->platform_device);
  4247. fail_sysfs:
  4248. fail_custom_fan_curve:
  4249. fail_platform_profile_setup:
  4250. fail_fan_boost_mode:
  4251. fail_platform:
  4252. kfree(asus);
  4253. return err;
  4254. }
  4255. static void asus_wmi_remove(struct platform_device *device)
  4256. {
  4257. struct asus_wmi *asus;
  4258. asus = platform_get_drvdata(device);
  4259. if (asus->driver->i8042_filter)
  4260. i8042_remove_filter(asus->driver->i8042_filter);
  4261. wmi_remove_notify_handler(asus->driver->event_guid);
  4262. asus_wmi_backlight_exit(asus);
  4263. asus_screenpad_exit(asus);
  4264. asus_wmi_input_exit(asus);
  4265. asus_wmi_led_exit(asus);
  4266. asus_wmi_rfkill_exit(asus);
  4267. asus_wmi_debugfs_exit(asus);
  4268. asus_wmi_sysfs_exit(asus->platform_device);
  4269. asus_fan_set_auto(asus);
  4270. throttle_thermal_policy_set_default(asus);
  4271. asus_wmi_battery_exit(asus);
  4272. kfree(asus);
  4273. }
  4274. /* Platform driver - hibernate/resume callbacks *******************************/
  4275. static int asus_hotk_thaw(struct device *device)
  4276. {
  4277. struct asus_wmi *asus = dev_get_drvdata(device);
  4278. if (asus->wlan.rfkill) {
  4279. bool wlan;
  4280. /*
  4281. * Work around bios bug - acpi _PTS turns off the wireless led
  4282. * during suspend. Normally it restores it on resume, but
  4283. * we should kick it ourselves in case hibernation is aborted.
  4284. */
  4285. wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
  4286. asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
  4287. }
  4288. return 0;
  4289. }
  4290. static int asus_hotk_resume(struct device *device)
  4291. {
  4292. struct asus_wmi *asus = dev_get_drvdata(device);
  4293. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  4294. kbd_led_update(asus);
  4295. if (asus_wmi_has_fnlock_key(asus))
  4296. asus_wmi_fnlock_update(asus);
  4297. asus_wmi_tablet_mode_get_state(asus);
  4298. return 0;
  4299. }
  4300. static int asus_hotk_restore(struct device *device)
  4301. {
  4302. struct asus_wmi *asus = dev_get_drvdata(device);
  4303. int bl;
  4304. /* Refresh both wlan rfkill state and pci hotplug */
  4305. if (asus->wlan.rfkill)
  4306. asus_rfkill_hotplug(asus);
  4307. if (asus->bluetooth.rfkill) {
  4308. bl = !asus_wmi_get_devstate_simple(asus,
  4309. ASUS_WMI_DEVID_BLUETOOTH);
  4310. rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
  4311. }
  4312. if (asus->wimax.rfkill) {
  4313. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
  4314. rfkill_set_sw_state(asus->wimax.rfkill, bl);
  4315. }
  4316. if (asus->wwan3g.rfkill) {
  4317. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
  4318. rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
  4319. }
  4320. if (asus->gps.rfkill) {
  4321. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
  4322. rfkill_set_sw_state(asus->gps.rfkill, bl);
  4323. }
  4324. if (asus->uwb.rfkill) {
  4325. bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
  4326. rfkill_set_sw_state(asus->uwb.rfkill, bl);
  4327. }
  4328. if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
  4329. kbd_led_update(asus);
  4330. if (asus->oobe_state_available) {
  4331. /*
  4332. * Disable OOBE state, so that e.g. the keyboard backlight
  4333. * works.
  4334. */
  4335. asus_wmi_set_devstate(ASUS_WMI_DEVID_OOBE, 1, NULL);
  4336. }
  4337. if (asus_wmi_has_fnlock_key(asus))
  4338. asus_wmi_fnlock_update(asus);
  4339. asus_wmi_tablet_mode_get_state(asus);
  4340. return 0;
  4341. }
  4342. static int asus_hotk_prepare(struct device *device)
  4343. {
  4344. if (use_ally_mcu_hack == ASUS_WMI_ALLY_MCU_HACK_ENABLED) {
  4345. acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE,
  4346. ASUS_USB0_PWR_EC0_CSEE_OFF);
  4347. msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
  4348. }
  4349. return 0;
  4350. }
  4351. #if defined(CONFIG_SUSPEND)
  4352. static void asus_ally_s2idle_restore(void)
  4353. {
  4354. if (use_ally_mcu_hack == ASUS_WMI_ALLY_MCU_HACK_ENABLED) {
  4355. acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE,
  4356. ASUS_USB0_PWR_EC0_CSEE_ON);
  4357. msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
  4358. }
  4359. }
  4360. /* Use only for Ally devices due to the wake_on_ac */
  4361. static struct acpi_s2idle_dev_ops asus_ally_s2idle_dev_ops = {
  4362. .restore = asus_ally_s2idle_restore,
  4363. };
  4364. static void asus_s2idle_check_register(void)
  4365. {
  4366. if (acpi_register_lps0_dev(&asus_ally_s2idle_dev_ops))
  4367. pr_warn("failed to register LPS0 sleep handler in asus-wmi\n");
  4368. }
  4369. static void asus_s2idle_check_unregister(void)
  4370. {
  4371. acpi_unregister_lps0_dev(&asus_ally_s2idle_dev_ops);
  4372. }
  4373. #else
  4374. static void asus_s2idle_check_register(void) {}
  4375. static void asus_s2idle_check_unregister(void) {}
  4376. #endif /* CONFIG_SUSPEND */
  4377. static const struct dev_pm_ops asus_pm_ops = {
  4378. .thaw = asus_hotk_thaw,
  4379. .restore = asus_hotk_restore,
  4380. .resume = asus_hotk_resume,
  4381. .prepare = asus_hotk_prepare,
  4382. };
  4383. /* Registration ***************************************************************/
  4384. static int asus_wmi_probe(struct platform_device *pdev)
  4385. {
  4386. struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
  4387. struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
  4388. int ret;
  4389. if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
  4390. pr_warn("ASUS Management GUID not found\n");
  4391. return -ENODEV;
  4392. }
  4393. if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
  4394. pr_warn("ASUS Event GUID not found\n");
  4395. return -ENODEV;
  4396. }
  4397. if (wdrv->probe) {
  4398. ret = wdrv->probe(pdev);
  4399. if (ret)
  4400. return ret;
  4401. }
  4402. asus_s2idle_check_register();
  4403. ret = asus_wmi_add(pdev);
  4404. if (ret)
  4405. asus_s2idle_check_unregister();
  4406. return ret;
  4407. }
  4408. static bool used;
  4409. static DEFINE_MUTEX(register_mutex);
  4410. int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
  4411. {
  4412. struct platform_driver *platform_driver;
  4413. struct platform_device *platform_device;
  4414. guard(mutex)(&register_mutex);
  4415. if (used)
  4416. return -EBUSY;
  4417. platform_driver = &driver->platform_driver;
  4418. platform_driver->remove = asus_wmi_remove;
  4419. platform_driver->driver.owner = driver->owner;
  4420. platform_driver->driver.name = driver->name;
  4421. platform_driver->driver.pm = &asus_pm_ops;
  4422. platform_device = platform_create_bundle(platform_driver,
  4423. asus_wmi_probe,
  4424. NULL, 0, NULL, 0);
  4425. if (IS_ERR(platform_device))
  4426. return PTR_ERR(platform_device);
  4427. used = true;
  4428. return 0;
  4429. }
  4430. EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
  4431. void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
  4432. {
  4433. guard(mutex)(&register_mutex);
  4434. asus_s2idle_check_unregister();
  4435. platform_device_unregister(driver->platform_device);
  4436. platform_driver_unregister(&driver->platform_driver);
  4437. used = false;
  4438. }
  4439. EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
  4440. static int __init asus_wmi_init(void)
  4441. {
  4442. pr_info("ASUS WMI generic driver loaded\n");
  4443. return 0;
  4444. }
  4445. static void __exit asus_wmi_exit(void)
  4446. {
  4447. pr_info("ASUS WMI generic driver unloaded\n");
  4448. }
  4449. module_init(asus_wmi_init);
  4450. module_exit(asus_wmi_exit);