thinkpad_acpi.c 310 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * thinkpad_acpi.c - ThinkPad ACPI Extras
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #define TPACPI_VERSION "0.26"
  10. #define TPACPI_SYSFS_VERSION 0x030000
  11. /*
  12. * Changelog:
  13. * 2007-10-20 changelog trimmed down
  14. *
  15. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  16. * drivers/misc.
  17. *
  18. * 2006-11-22 0.13 new maintainer
  19. * changelog now lives in git commit history, and will
  20. * not be updated further in-file.
  21. *
  22. * 2005-03-17 0.11 support for 600e, 770x
  23. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  24. *
  25. * 2005-01-16 0.9 use MODULE_VERSION
  26. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  27. * fix parameter passing on module loading
  28. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  29. * thanks to Jim Radford <radford@blackbean.org>
  30. * 2004-11-08 0.8 fix init error case, don't return from a macro
  31. * thanks to Chris Wright <chrisw@osdl.org>
  32. */
  33. #include <linux/acpi.h>
  34. #include <linux/backlight.h>
  35. #include <linux/bitops.h>
  36. #include <linux/delay.h>
  37. #include <linux/dmi.h>
  38. #include <linux/fb.h>
  39. #include <linux/freezer.h>
  40. #include <linux/hwmon.h>
  41. #include <linux/hwmon-sysfs.h>
  42. #include <linux/init.h>
  43. #include <linux/input.h>
  44. #include <linux/input/sparse-keymap.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/kernel.h>
  47. #include <linux/kthread.h>
  48. #include <linux/leds.h>
  49. #include <linux/list.h>
  50. #include <linux/lockdep.h>
  51. #include <linux/module.h>
  52. #include <linux/mutex.h>
  53. #include <linux/nvram.h>
  54. #include <linux/pci.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/platform_profile.h>
  57. #include <linux/power_supply.h>
  58. #include <linux/proc_fs.h>
  59. #include <linux/rfkill.h>
  60. #include <linux/sched.h>
  61. #include <linux/sched/signal.h>
  62. #include <linux/seq_file.h>
  63. #include <linux/slab.h>
  64. #include <linux/string.h>
  65. #include <linux/string_helpers.h>
  66. #include <linux/sysfs.h>
  67. #include <linux/types.h>
  68. #include <linux/uaccess.h>
  69. #include <linux/units.h>
  70. #include <linux/workqueue.h>
  71. #include <acpi/battery.h>
  72. #include <acpi/video.h>
  73. #include <drm/drm_privacy_screen_driver.h>
  74. #include <sound/control.h>
  75. #include <sound/core.h>
  76. #include <sound/initval.h>
  77. #include "dual_accel_detect.h"
  78. /* ThinkPad CMOS commands */
  79. #define TP_CMOS_VOLUME_DOWN 0
  80. #define TP_CMOS_VOLUME_UP 1
  81. #define TP_CMOS_VOLUME_MUTE 2
  82. #define TP_CMOS_BRIGHTNESS_UP 4
  83. #define TP_CMOS_BRIGHTNESS_DOWN 5
  84. #define TP_CMOS_THINKLIGHT_ON 12
  85. #define TP_CMOS_THINKLIGHT_OFF 13
  86. /* NVRAM Addresses */
  87. enum tp_nvram_addr {
  88. TP_NVRAM_ADDR_HK2 = 0x57,
  89. TP_NVRAM_ADDR_THINKLIGHT = 0x58,
  90. TP_NVRAM_ADDR_VIDEO = 0x59,
  91. TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
  92. TP_NVRAM_ADDR_MIXER = 0x60,
  93. };
  94. /* NVRAM bit masks */
  95. enum {
  96. TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
  97. TP_NVRAM_MASK_HKT_ZOOM = 0x20,
  98. TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
  99. TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
  100. TP_NVRAM_MASK_THINKLIGHT = 0x10,
  101. TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
  102. TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
  103. TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
  104. TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
  105. TP_NVRAM_MASK_MUTE = 0x40,
  106. TP_NVRAM_MASK_HKT_VOLUME = 0x80,
  107. TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
  108. TP_NVRAM_POS_LEVEL_VOLUME = 0,
  109. };
  110. /* Misc NVRAM-related */
  111. enum {
  112. TP_NVRAM_LEVEL_VOLUME_MAX = 14,
  113. };
  114. /* ACPI HIDs */
  115. #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
  116. #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
  117. #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
  118. #define TPACPI_ACPI_EC_HID "PNP0C09"
  119. /* Input IDs */
  120. #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
  121. #define TPACPI_HKEY_INPUT_VERSION 0x4101
  122. /* ACPI \WGSV commands */
  123. enum {
  124. TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
  125. TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
  126. TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
  127. TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
  128. };
  129. /* TP_ACPI_WGSV_GET_STATE bits */
  130. enum {
  131. TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
  132. TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
  133. TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
  134. TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
  135. TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
  136. TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
  137. TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
  138. TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
  139. TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
  140. TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
  141. };
  142. /* HKEY events */
  143. enum tpacpi_hkey_event_t {
  144. /* Original hotkeys */
  145. TP_HKEY_EV_ORIG_KEY_START = 0x1001, /* First hotkey (FN+F1) */
  146. TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
  147. TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
  148. TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
  149. TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
  150. TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
  151. TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
  152. TP_HKEY_EV_ORIG_KEY_END = 0x1020, /* Last original hotkey code */
  153. /* Adaptive keyboard (2014 X1 Carbon) */
  154. TP_HKEY_EV_DFR_CHANGE_ROW = 0x1101, /* Change adaptive kbd Fn row mode */
  155. TP_HKEY_EV_DFR_S_QUICKVIEW_ROW = 0x1102, /* Set adap. kbd Fn row to function mode */
  156. TP_HKEY_EV_ADAPTIVE_KEY_START = 0x1103, /* First hotkey code on adaptive kbd */
  157. TP_HKEY_EV_ADAPTIVE_KEY_END = 0x1116, /* Last hotkey code on adaptive kbd */
  158. /* Extended hotkey events in 2017+ models */
  159. TP_HKEY_EV_EXTENDED_KEY_START = 0x1300, /* First extended hotkey code */
  160. TP_HKEY_EV_PRIVACYGUARD_TOGGLE = 0x130f, /* Toggle priv.guard on/off */
  161. TP_HKEY_EV_EXTENDED_KEY_END = 0x1319, /* Last extended hotkey code using
  162. * hkey -> scancode translation for
  163. * compat. Later codes are entered
  164. * directly in the sparse-keymap.
  165. */
  166. TP_HKEY_EV_AMT_TOGGLE = 0x131a, /* Toggle AMT on/off */
  167. TP_HKEY_EV_DOUBLETAP_TOGGLE = 0x131c, /* Toggle trackpoint doubletap on/off */
  168. TP_HKEY_EV_PROFILE_TOGGLE = 0x131f, /* Toggle platform profile in 2024 systems */
  169. TP_HKEY_EV_PROFILE_TOGGLE2 = 0x1401, /* Toggle platform profile in 2025 + systems */
  170. /* Reasons for waking up from S3/S4 */
  171. TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
  172. TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
  173. TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
  174. TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
  175. TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
  176. TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
  177. /* Auto-sleep after eject request */
  178. TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
  179. TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
  180. /* Misc bay events */
  181. TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
  182. TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
  183. or port replicator */
  184. TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
  185. dock or port replicator */
  186. /*
  187. * Thinkpad X1 Tablet series devices emit 0x4012 and 0x4013
  188. * when keyboard cover is attached, detached or folded onto the back
  189. */
  190. TP_HKEY_EV_KBD_COVER_ATTACH = 0x4012, /* keyboard cover attached */
  191. TP_HKEY_EV_KBD_COVER_DETACH = 0x4013, /* keyboard cover detached or folded back */
  192. /* User-interface events */
  193. TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
  194. TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
  195. TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
  196. TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
  197. TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
  198. * enter/leave tablet mode
  199. */
  200. TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
  201. TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
  202. TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
  203. /* Key-related user-interface events */
  204. TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
  205. TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
  206. TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
  207. /* Thermal events */
  208. TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
  209. TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
  210. TP_HKEY_EV_ALARM_BAT_LIM_CHANGE = 0x6013, /* battery charge limit changed*/
  211. TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
  212. TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
  213. TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
  214. TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
  215. * command completed. Related to
  216. * AML DYTC */
  217. TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
  218. * changed. Related to AML GMTS */
  219. /* AC-related events */
  220. TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
  221. /* Further user-interface events */
  222. TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
  223. TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
  224. /* Misc */
  225. TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
  226. /* Misc2 */
  227. TP_HKEY_EV_TRACK_DOUBLETAP = 0x8036, /* trackpoint doubletap */
  228. };
  229. /****************************************************************************
  230. * Main driver
  231. */
  232. #define TPACPI_NAME "thinkpad"
  233. #define TPACPI_DESC "ThinkPad ACPI Extras"
  234. #define TPACPI_FILE TPACPI_NAME "_acpi"
  235. #define TPACPI_URL "http://ibm-acpi.sf.net/"
  236. #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
  237. #define TPACPI_PROC_DIR "ibm"
  238. #define TPACPI_ACPI_EVENT_PREFIX "ibm"
  239. #define TPACPI_DRVR_NAME TPACPI_FILE
  240. #define TPACPI_DRVR_SHORTNAME "tpacpi"
  241. #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
  242. #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
  243. #define TPACPI_WORKQUEUE_NAME "ktpacpid"
  244. #define TPACPI_MAX_ACPI_ARGS 3
  245. /* Debugging printk groups */
  246. #define TPACPI_DBG_ALL 0xffff
  247. #define TPACPI_DBG_DISCLOSETASK 0x8000
  248. #define TPACPI_DBG_INIT 0x0001
  249. #define TPACPI_DBG_EXIT 0x0002
  250. #define TPACPI_DBG_RFKILL 0x0004
  251. #define TPACPI_DBG_HKEY 0x0008
  252. #define TPACPI_DBG_FAN 0x0010
  253. #define TPACPI_DBG_BRGHT 0x0020
  254. #define TPACPI_DBG_MIXER 0x0040
  255. #define FAN_NOT_PRESENT 65535
  256. /****************************************************************************
  257. * Driver-wide structs and misc. variables
  258. */
  259. struct ibm_struct;
  260. struct tp_acpi_drv_struct {
  261. const struct acpi_device_id *hid;
  262. struct acpi_driver *driver;
  263. void (*notify) (struct ibm_struct *, u32);
  264. acpi_handle *handle;
  265. u32 type;
  266. struct acpi_device *device;
  267. };
  268. struct ibm_struct {
  269. char *name;
  270. int (*read) (struct seq_file *);
  271. int (*write) (char *);
  272. void (*exit) (void);
  273. void (*resume) (void);
  274. void (*suspend) (void);
  275. void (*shutdown) (void);
  276. struct list_head all_drivers;
  277. struct tp_acpi_drv_struct *acpi;
  278. struct {
  279. u8 acpi_driver_registered:1;
  280. u8 acpi_notify_installed:1;
  281. u8 proc_created:1;
  282. u8 init_called:1;
  283. u8 experimental:1;
  284. } flags;
  285. };
  286. struct ibm_init_struct {
  287. char param[32];
  288. int (*init) (struct ibm_init_struct *);
  289. umode_t base_procfs_mode;
  290. struct ibm_struct *data;
  291. };
  292. /* DMI Quirks */
  293. struct quirk_entry {
  294. bool btusb_bug;
  295. };
  296. static struct quirk_entry quirk_btusb_bug = {
  297. .btusb_bug = true,
  298. };
  299. static struct {
  300. u32 bluetooth:1;
  301. u32 hotkey:1;
  302. u32 hotkey_mask:1;
  303. u32 hotkey_wlsw:1;
  304. enum {
  305. TP_HOTKEY_TABLET_NONE = 0,
  306. TP_HOTKEY_TABLET_USES_MHKG,
  307. TP_HOTKEY_TABLET_USES_GMMS,
  308. } hotkey_tablet;
  309. u32 kbdlight:1;
  310. u32 light:1;
  311. u32 light_status:1;
  312. u32 bright_acpimode:1;
  313. u32 bright_unkfw:1;
  314. u32 wan:1;
  315. u32 uwb:1;
  316. u32 fan_ctrl_status_undef:1;
  317. u32 second_fan:1;
  318. u32 second_fan_ctl:1;
  319. u32 beep_needs_two_args:1;
  320. u32 mixer_no_level_control:1;
  321. u32 battery_force_primary:1;
  322. u32 input_device_registered:1;
  323. u32 platform_drv_registered:1;
  324. u32 sensors_pdrv_registered:1;
  325. u32 hotkey_poll_active:1;
  326. u32 has_adaptive_kbd:1;
  327. u32 kbd_lang:1;
  328. u32 trackpoint_doubletap:1;
  329. struct quirk_entry *quirks;
  330. } tp_features;
  331. static struct {
  332. u16 hotkey_mask_ff:1;
  333. u16 volume_ctrl_forbidden:1;
  334. } tp_warned;
  335. struct thinkpad_id_data {
  336. unsigned int vendor; /* ThinkPad vendor:
  337. * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
  338. char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
  339. char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
  340. u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
  341. u32 ec_model;
  342. u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
  343. u16 ec_release;
  344. char *model_str; /* ThinkPad T43 */
  345. char *nummodel_str; /* 9384A9C for a 9384-A9C model */
  346. };
  347. static struct thinkpad_id_data thinkpad_id;
  348. static enum {
  349. TPACPI_LIFE_INIT = 0,
  350. TPACPI_LIFE_RUNNING,
  351. TPACPI_LIFE_EXITING,
  352. } tpacpi_lifecycle;
  353. static int experimental;
  354. static u32 dbg_level;
  355. static struct workqueue_struct *tpacpi_wq;
  356. enum led_status_t {
  357. TPACPI_LED_OFF = 0,
  358. TPACPI_LED_ON,
  359. TPACPI_LED_BLINK,
  360. };
  361. /* tpacpi LED class */
  362. struct tpacpi_led_classdev {
  363. struct led_classdev led_classdev;
  364. int led;
  365. };
  366. /* brightness level capabilities */
  367. static unsigned int bright_maxlvl; /* 0 = unknown */
  368. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  369. static int dbg_wlswemul;
  370. static bool tpacpi_wlsw_emulstate;
  371. static int dbg_bluetoothemul;
  372. static bool tpacpi_bluetooth_emulstate;
  373. static int dbg_wwanemul;
  374. static bool tpacpi_wwan_emulstate;
  375. static int dbg_uwbemul;
  376. static bool tpacpi_uwb_emulstate;
  377. #endif
  378. /*************************************************************************
  379. * Debugging helpers
  380. */
  381. #define dbg_printk(a_dbg_level, format, arg...) \
  382. do { \
  383. if (dbg_level & (a_dbg_level)) \
  384. printk(KERN_DEBUG pr_fmt("%s: " format), \
  385. __func__, ##arg); \
  386. } while (0)
  387. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  388. #define vdbg_printk dbg_printk
  389. static const char *str_supported(int is_supported);
  390. #else
  391. static inline const char *str_supported(int is_supported) { return ""; }
  392. #define vdbg_printk(a_dbg_level, format, arg...) \
  393. do { if (0) no_printk(format, ##arg); } while (0)
  394. #endif
  395. static void tpacpi_log_usertask(const char * const what)
  396. {
  397. printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
  398. what, task_tgid_vnr(current));
  399. }
  400. #define tpacpi_disclose_usertask(what, format, arg...) \
  401. do { \
  402. if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
  403. (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
  404. printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
  405. what, task_tgid_vnr(current), ## arg); \
  406. } \
  407. } while (0)
  408. /*
  409. * Quirk handling helpers
  410. *
  411. * ThinkPad IDs and versions seen in the field so far are
  412. * two or three characters from the set [0-9A-Z], i.e. base 36.
  413. *
  414. * We use values well outside that range as specials.
  415. */
  416. #define TPACPI_MATCH_ANY 0xffffffffU
  417. #define TPACPI_MATCH_ANY_VERSION 0xffffU
  418. #define TPACPI_MATCH_UNKNOWN 0U
  419. /* TPID('1', 'Y') == 0x3159 */
  420. #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
  421. #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
  422. #define TPVER TPID
  423. #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
  424. { .vendor = PCI_VENDOR_ID_IBM, \
  425. .bios = TPID(__id1, __id2), \
  426. .ec = TPACPI_MATCH_ANY, \
  427. .quirks = (__quirk) }
  428. #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
  429. { .vendor = PCI_VENDOR_ID_LENOVO, \
  430. .bios = TPID(__id1, __id2), \
  431. .ec = TPACPI_MATCH_ANY, \
  432. .quirks = (__quirk) }
  433. #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
  434. { .vendor = PCI_VENDOR_ID_LENOVO, \
  435. .bios = TPID3(__id1, __id2, __id3), \
  436. .ec = TPACPI_MATCH_ANY, \
  437. .quirks = (__quirk) }
  438. #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
  439. { .vendor = PCI_VENDOR_ID_IBM, \
  440. .bios = TPACPI_MATCH_ANY, \
  441. .ec = TPID(__id1, __id2), \
  442. .quirks = (__quirk) }
  443. #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
  444. { .vendor = PCI_VENDOR_ID_LENOVO, \
  445. .bios = TPACPI_MATCH_ANY, \
  446. .ec = TPID(__id1, __id2), \
  447. .quirks = (__quirk) }
  448. struct tpacpi_quirk {
  449. unsigned int vendor;
  450. u32 bios;
  451. u32 ec;
  452. unsigned long quirks;
  453. };
  454. /**
  455. * tpacpi_check_quirks() - search BIOS/EC version on a list
  456. * @qlist: array of &struct tpacpi_quirk
  457. * @qlist_size: number of elements in @qlist
  458. *
  459. * Iterates over a quirks list until one is found that matches the
  460. * ThinkPad's vendor, BIOS and EC model.
  461. *
  462. * Returns: %0 if nothing matches, otherwise returns the quirks field of
  463. * the matching &struct tpacpi_quirk entry.
  464. *
  465. * The match criteria is: vendor, ec and bios must match.
  466. */
  467. static unsigned long __init tpacpi_check_quirks(
  468. const struct tpacpi_quirk *qlist,
  469. unsigned int qlist_size)
  470. {
  471. while (qlist_size) {
  472. if ((qlist->vendor == thinkpad_id.vendor ||
  473. qlist->vendor == TPACPI_MATCH_ANY) &&
  474. (qlist->bios == thinkpad_id.bios_model ||
  475. qlist->bios == TPACPI_MATCH_ANY) &&
  476. (qlist->ec == thinkpad_id.ec_model ||
  477. qlist->ec == TPACPI_MATCH_ANY))
  478. return qlist->quirks;
  479. qlist_size--;
  480. qlist++;
  481. }
  482. return 0;
  483. }
  484. static __always_inline bool __pure __init tpacpi_is_lenovo(void)
  485. {
  486. return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
  487. }
  488. static __always_inline bool __pure __init tpacpi_is_ibm(void)
  489. {
  490. return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
  491. }
  492. /****************************************************************************
  493. ****************************************************************************
  494. *
  495. * ACPI Helpers and device model
  496. *
  497. ****************************************************************************
  498. ****************************************************************************/
  499. /*************************************************************************
  500. * ACPI basic handles
  501. */
  502. static acpi_handle root_handle;
  503. static acpi_handle ec_handle;
  504. #define TPACPI_HANDLE(object, parent, paths...) \
  505. static acpi_handle object##_handle; \
  506. static const acpi_handle * const object##_parent __initconst = \
  507. &parent##_handle; \
  508. static char *object##_paths[] __initdata = { paths }
  509. TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  510. TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  511. TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
  512. /* T4x, X31, X40 */
  513. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  514. "\\CMS", /* R40, R40e */
  515. ); /* all others */
  516. TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  517. "^HKEY", /* R30, R31 */
  518. "HKEY", /* all others */
  519. ); /* 570 */
  520. /*************************************************************************
  521. * ACPI helpers
  522. */
  523. static int acpi_evalf(acpi_handle handle,
  524. int *res, char *method, char *fmt, ...)
  525. {
  526. char *fmt0 = fmt;
  527. struct acpi_object_list params;
  528. union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
  529. struct acpi_buffer result, *resultp;
  530. union acpi_object out_obj;
  531. acpi_status status;
  532. va_list ap;
  533. char res_type;
  534. int success;
  535. int quiet;
  536. if (!*fmt) {
  537. pr_err("acpi_evalf() called with empty format\n");
  538. return 0;
  539. }
  540. if (*fmt == 'q') {
  541. quiet = 1;
  542. fmt++;
  543. } else
  544. quiet = 0;
  545. res_type = *(fmt++);
  546. params.count = 0;
  547. params.pointer = &in_objs[0];
  548. va_start(ap, fmt);
  549. while (*fmt) {
  550. char c = *(fmt++);
  551. switch (c) {
  552. case 'd': /* int */
  553. in_objs[params.count].integer.value = va_arg(ap, int);
  554. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  555. break;
  556. /* add more types as needed */
  557. default:
  558. pr_err("acpi_evalf() called with invalid format character '%c'\n",
  559. c);
  560. va_end(ap);
  561. return 0;
  562. }
  563. }
  564. va_end(ap);
  565. if (res_type != 'v') {
  566. result.length = sizeof(out_obj);
  567. result.pointer = &out_obj;
  568. resultp = &result;
  569. } else
  570. resultp = NULL;
  571. status = acpi_evaluate_object(handle, method, &params, resultp);
  572. switch (res_type) {
  573. case 'd': /* int */
  574. success = (status == AE_OK &&
  575. out_obj.type == ACPI_TYPE_INTEGER);
  576. if (success && res)
  577. *res = out_obj.integer.value;
  578. break;
  579. case 'v': /* void */
  580. success = status == AE_OK;
  581. break;
  582. /* add more types as needed */
  583. default:
  584. pr_err("acpi_evalf() called with invalid format character '%c'\n",
  585. res_type);
  586. return 0;
  587. }
  588. if (!success && !quiet)
  589. pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
  590. method, fmt0, acpi_format_exception(status));
  591. return success;
  592. }
  593. static int acpi_ec_read(int i, u8 *p)
  594. {
  595. int v;
  596. if (ecrd_handle) {
  597. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  598. return 0;
  599. *p = v;
  600. } else {
  601. if (ec_read(i, p) < 0)
  602. return 0;
  603. }
  604. return 1;
  605. }
  606. static int acpi_ec_write(int i, u8 v)
  607. {
  608. if (ecwr_handle) {
  609. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  610. return 0;
  611. } else {
  612. if (ec_write(i, v) < 0)
  613. return 0;
  614. }
  615. return 1;
  616. }
  617. static int issue_thinkpad_cmos_command(int cmos_cmd)
  618. {
  619. if (!cmos_handle)
  620. return -ENXIO;
  621. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  622. return -EIO;
  623. return 0;
  624. }
  625. /*************************************************************************
  626. * ACPI device model
  627. */
  628. #define TPACPI_ACPIHANDLE_INIT(object) \
  629. drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
  630. object##_paths, ARRAY_SIZE(object##_paths))
  631. static void __init drv_acpi_handle_init(const char *name,
  632. acpi_handle *handle, const acpi_handle parent,
  633. char **paths, const int num_paths)
  634. {
  635. int i;
  636. acpi_status status;
  637. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  638. name);
  639. for (i = 0; i < num_paths; i++) {
  640. status = acpi_get_handle(parent, paths[i], handle);
  641. if (ACPI_SUCCESS(status)) {
  642. dbg_printk(TPACPI_DBG_INIT,
  643. "Found ACPI handle %s for %s\n",
  644. paths[i], name);
  645. return;
  646. }
  647. }
  648. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  649. name);
  650. *handle = NULL;
  651. }
  652. static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
  653. u32 level, void *context, void **return_value)
  654. {
  655. if (!strcmp(context, "video")) {
  656. struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
  657. if (!dev || strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
  658. return AE_OK;
  659. }
  660. *(acpi_handle *)return_value = handle;
  661. return AE_CTRL_TERMINATE;
  662. }
  663. static void __init tpacpi_acpi_handle_locate(const char *name,
  664. const char *hid,
  665. acpi_handle *handle)
  666. {
  667. acpi_status status;
  668. acpi_handle device_found;
  669. BUG_ON(!name || !handle);
  670. vdbg_printk(TPACPI_DBG_INIT,
  671. "trying to locate ACPI handle for %s, using HID %s\n",
  672. name, hid ? hid : "NULL");
  673. memset(&device_found, 0, sizeof(device_found));
  674. status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
  675. (void *)name, &device_found);
  676. *handle = NULL;
  677. if (ACPI_SUCCESS(status)) {
  678. *handle = device_found;
  679. dbg_printk(TPACPI_DBG_INIT,
  680. "Found ACPI handle for %s\n", name);
  681. } else {
  682. vdbg_printk(TPACPI_DBG_INIT,
  683. "Could not locate an ACPI handle for %s: %s\n",
  684. name, acpi_format_exception(status));
  685. }
  686. }
  687. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  688. {
  689. struct ibm_struct *ibm = data;
  690. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  691. return;
  692. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  693. return;
  694. ibm->acpi->notify(ibm, event);
  695. }
  696. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  697. {
  698. acpi_status status;
  699. BUG_ON(!ibm->acpi);
  700. if (!*ibm->acpi->handle)
  701. return 0;
  702. vdbg_printk(TPACPI_DBG_INIT,
  703. "setting up ACPI notify for %s\n", ibm->name);
  704. ibm->acpi->device = acpi_fetch_acpi_dev(*ibm->acpi->handle);
  705. if (!ibm->acpi->device) {
  706. pr_err("acpi_fetch_acpi_dev(%s) failed\n", ibm->name);
  707. return -ENODEV;
  708. }
  709. ibm->acpi->device->driver_data = ibm;
  710. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  711. TPACPI_ACPI_EVENT_PREFIX,
  712. ibm->name);
  713. status = acpi_install_notify_handler(*ibm->acpi->handle,
  714. ibm->acpi->type, dispatch_acpi_notify, ibm);
  715. if (ACPI_FAILURE(status)) {
  716. if (status == AE_ALREADY_EXISTS) {
  717. pr_notice("another device driver is already handling %s events\n",
  718. ibm->name);
  719. } else {
  720. pr_err("acpi_install_notify_handler(%s) failed: %s\n",
  721. ibm->name, acpi_format_exception(status));
  722. }
  723. return -ENODEV;
  724. }
  725. ibm->flags.acpi_notify_installed = 1;
  726. return 0;
  727. }
  728. static int __init tpacpi_device_add(struct acpi_device *device)
  729. {
  730. return 0;
  731. }
  732. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  733. {
  734. int rc;
  735. dbg_printk(TPACPI_DBG_INIT,
  736. "registering %s as an ACPI driver\n", ibm->name);
  737. BUG_ON(!ibm->acpi);
  738. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  739. if (!ibm->acpi->driver) {
  740. pr_err("failed to allocate memory for ibm->acpi->driver\n");
  741. return -ENOMEM;
  742. }
  743. sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
  744. ibm->acpi->driver->ids = ibm->acpi->hid;
  745. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  746. rc = acpi_bus_register_driver(ibm->acpi->driver);
  747. if (rc < 0) {
  748. pr_err("acpi_bus_register_driver(%s) failed: %d\n",
  749. ibm->name, rc);
  750. kfree(ibm->acpi->driver);
  751. ibm->acpi->driver = NULL;
  752. } else if (!rc)
  753. ibm->flags.acpi_driver_registered = 1;
  754. return rc;
  755. }
  756. /****************************************************************************
  757. ****************************************************************************
  758. *
  759. * Procfs Helpers
  760. *
  761. ****************************************************************************
  762. ****************************************************************************/
  763. static int dispatch_proc_show(struct seq_file *m, void *v)
  764. {
  765. struct ibm_struct *ibm = m->private;
  766. if (!ibm || !ibm->read)
  767. return -EINVAL;
  768. return ibm->read(m);
  769. }
  770. static int dispatch_proc_open(struct inode *inode, struct file *file)
  771. {
  772. return single_open(file, dispatch_proc_show, pde_data(inode));
  773. }
  774. static ssize_t dispatch_proc_write(struct file *file,
  775. const char __user *userbuf,
  776. size_t count, loff_t *pos)
  777. {
  778. struct ibm_struct *ibm = pde_data(file_inode(file));
  779. char *kernbuf;
  780. int ret;
  781. if (!ibm || !ibm->write)
  782. return -EINVAL;
  783. if (count > PAGE_SIZE - 1)
  784. return -EINVAL;
  785. kernbuf = memdup_user_nul(userbuf, count);
  786. if (IS_ERR(kernbuf))
  787. return PTR_ERR(kernbuf);
  788. ret = ibm->write(kernbuf);
  789. if (ret == 0)
  790. ret = count;
  791. kfree(kernbuf);
  792. return ret;
  793. }
  794. static const struct proc_ops dispatch_proc_ops = {
  795. .proc_open = dispatch_proc_open,
  796. .proc_read = seq_read,
  797. .proc_lseek = seq_lseek,
  798. .proc_release = single_release,
  799. .proc_write = dispatch_proc_write,
  800. };
  801. /****************************************************************************
  802. ****************************************************************************
  803. *
  804. * Device model: input, hwmon and platform
  805. *
  806. ****************************************************************************
  807. ****************************************************************************/
  808. static struct platform_device *tpacpi_pdev;
  809. static struct platform_device *tpacpi_sensors_pdev;
  810. static struct device *tpacpi_hwmon;
  811. static struct input_dev *tpacpi_inputdev;
  812. static struct mutex tpacpi_inputdev_send_mutex;
  813. static LIST_HEAD(tpacpi_all_drivers);
  814. #ifdef CONFIG_PM_SLEEP
  815. static int tpacpi_suspend_handler(struct device *dev)
  816. {
  817. struct ibm_struct *ibm, *itmp;
  818. list_for_each_entry_safe(ibm, itmp,
  819. &tpacpi_all_drivers,
  820. all_drivers) {
  821. if (ibm->suspend)
  822. (ibm->suspend)();
  823. }
  824. return 0;
  825. }
  826. static int tpacpi_resume_handler(struct device *dev)
  827. {
  828. struct ibm_struct *ibm, *itmp;
  829. list_for_each_entry_safe(ibm, itmp,
  830. &tpacpi_all_drivers,
  831. all_drivers) {
  832. if (ibm->resume)
  833. (ibm->resume)();
  834. }
  835. return 0;
  836. }
  837. #endif
  838. static SIMPLE_DEV_PM_OPS(tpacpi_pm,
  839. tpacpi_suspend_handler, tpacpi_resume_handler);
  840. static void tpacpi_shutdown_handler(struct platform_device *pdev)
  841. {
  842. struct ibm_struct *ibm, *itmp;
  843. list_for_each_entry_safe(ibm, itmp,
  844. &tpacpi_all_drivers,
  845. all_drivers) {
  846. if (ibm->shutdown)
  847. (ibm->shutdown)();
  848. }
  849. }
  850. /*************************************************************************
  851. * sysfs support helpers
  852. */
  853. static int parse_strtoul(const char *buf,
  854. unsigned long max, unsigned long *value)
  855. {
  856. char *endp;
  857. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  858. endp = skip_spaces(endp);
  859. if (*endp || *value > max)
  860. return -EINVAL;
  861. return 0;
  862. }
  863. static void tpacpi_disable_brightness_delay(void)
  864. {
  865. if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
  866. pr_notice("ACPI backlight control delay disabled\n");
  867. }
  868. static void printk_deprecated_attribute(const char * const what,
  869. const char * const details)
  870. {
  871. tpacpi_log_usertask("deprecated sysfs attribute");
  872. pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
  873. what, details);
  874. }
  875. /*************************************************************************
  876. * rfkill and radio control support helpers
  877. */
  878. /*
  879. * ThinkPad-ACPI firmware handling model:
  880. *
  881. * WLSW (master wireless switch) is event-driven, and is common to all
  882. * firmware-controlled radios. It cannot be controlled, just monitored,
  883. * as expected. It overrides all radio state in firmware
  884. *
  885. * The kernel, a masked-off hotkey, and WLSW can change the radio state
  886. * (TODO: verify how WLSW interacts with the returned radio state).
  887. *
  888. * The only time there are shadow radio state changes, is when
  889. * masked-off hotkeys are used.
  890. */
  891. /*
  892. * Internal driver API for radio state:
  893. *
  894. * int: < 0 = error, otherwise enum tpacpi_rfkill_state
  895. * bool: true means radio blocked (off)
  896. */
  897. enum tpacpi_rfkill_state {
  898. TPACPI_RFK_RADIO_OFF = 0,
  899. TPACPI_RFK_RADIO_ON
  900. };
  901. /* rfkill switches */
  902. enum tpacpi_rfk_id {
  903. TPACPI_RFK_BLUETOOTH_SW_ID = 0,
  904. TPACPI_RFK_WWAN_SW_ID,
  905. TPACPI_RFK_UWB_SW_ID,
  906. TPACPI_RFK_SW_MAX
  907. };
  908. static const char *tpacpi_rfkill_names[] = {
  909. [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
  910. [TPACPI_RFK_WWAN_SW_ID] = "wwan",
  911. [TPACPI_RFK_UWB_SW_ID] = "uwb",
  912. [TPACPI_RFK_SW_MAX] = NULL
  913. };
  914. /* ThinkPad-ACPI rfkill subdriver */
  915. struct tpacpi_rfk {
  916. struct rfkill *rfkill;
  917. enum tpacpi_rfk_id id;
  918. const struct tpacpi_rfk_ops *ops;
  919. };
  920. struct tpacpi_rfk_ops {
  921. /* firmware interface */
  922. int (*get_status)(void);
  923. int (*set_status)(const enum tpacpi_rfkill_state);
  924. };
  925. static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
  926. /* Query FW and update rfkill sw state for a given rfkill switch */
  927. static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
  928. {
  929. int status;
  930. if (!tp_rfk)
  931. return -ENODEV;
  932. status = (tp_rfk->ops->get_status)();
  933. if (status < 0)
  934. return status;
  935. rfkill_set_sw_state(tp_rfk->rfkill,
  936. (status == TPACPI_RFK_RADIO_OFF));
  937. return status;
  938. }
  939. /*
  940. * Sync the HW-blocking state of all rfkill switches,
  941. * do notice it causes the rfkill core to schedule uevents
  942. */
  943. static void tpacpi_rfk_update_hwblock_state(bool blocked)
  944. {
  945. unsigned int i;
  946. struct tpacpi_rfk *tp_rfk;
  947. for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
  948. tp_rfk = tpacpi_rfkill_switches[i];
  949. if (tp_rfk) {
  950. if (rfkill_set_hw_state(tp_rfk->rfkill,
  951. blocked)) {
  952. /* ignore -- we track sw block */
  953. }
  954. }
  955. }
  956. }
  957. /* Call to get the WLSW state from the firmware */
  958. static int hotkey_get_wlsw(void);
  959. /* Call to query WLSW state and update all rfkill switches */
  960. static bool tpacpi_rfk_check_hwblock_state(void)
  961. {
  962. int res = hotkey_get_wlsw();
  963. int hw_blocked;
  964. /* When unknown or unsupported, we have to assume it is unblocked */
  965. if (res < 0)
  966. return false;
  967. hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
  968. tpacpi_rfk_update_hwblock_state(hw_blocked);
  969. return hw_blocked;
  970. }
  971. static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
  972. {
  973. struct tpacpi_rfk *tp_rfk = data;
  974. int res;
  975. dbg_printk(TPACPI_DBG_RFKILL,
  976. "request to change radio state to %s\n",
  977. blocked ? "blocked" : "unblocked");
  978. /* try to set radio state */
  979. res = (tp_rfk->ops->set_status)(blocked ?
  980. TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
  981. /* and update the rfkill core with whatever the FW really did */
  982. tpacpi_rfk_update_swstate(tp_rfk);
  983. return (res < 0) ? res : 0;
  984. }
  985. static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
  986. .set_block = tpacpi_rfk_hook_set_block,
  987. };
  988. static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
  989. const struct tpacpi_rfk_ops *tp_rfkops,
  990. const enum rfkill_type rfktype,
  991. const char *name,
  992. const bool set_default)
  993. {
  994. struct tpacpi_rfk *atp_rfk;
  995. int res;
  996. bool sw_state = false;
  997. bool hw_state;
  998. int sw_status;
  999. BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
  1000. atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
  1001. if (atp_rfk)
  1002. atp_rfk->rfkill = rfkill_alloc(name,
  1003. &tpacpi_pdev->dev,
  1004. rfktype,
  1005. &tpacpi_rfk_rfkill_ops,
  1006. atp_rfk);
  1007. if (!atp_rfk || !atp_rfk->rfkill) {
  1008. pr_err("failed to allocate memory for rfkill class\n");
  1009. kfree(atp_rfk);
  1010. return -ENOMEM;
  1011. }
  1012. atp_rfk->id = id;
  1013. atp_rfk->ops = tp_rfkops;
  1014. sw_status = (tp_rfkops->get_status)();
  1015. if (sw_status < 0) {
  1016. pr_err("failed to read initial state for %s, error %d\n",
  1017. name, sw_status);
  1018. } else {
  1019. sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
  1020. if (set_default) {
  1021. /* try to keep the initial state, since we ask the
  1022. * firmware to preserve it across S5 in NVRAM */
  1023. rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
  1024. }
  1025. }
  1026. hw_state = tpacpi_rfk_check_hwblock_state();
  1027. rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
  1028. res = rfkill_register(atp_rfk->rfkill);
  1029. if (res < 0) {
  1030. pr_err("failed to register %s rfkill switch: %d\n", name, res);
  1031. rfkill_destroy(atp_rfk->rfkill);
  1032. kfree(atp_rfk);
  1033. return res;
  1034. }
  1035. tpacpi_rfkill_switches[id] = atp_rfk;
  1036. pr_info("rfkill switch %s: radio is %sblocked\n",
  1037. name, (sw_state || hw_state) ? "" : "un");
  1038. return 0;
  1039. }
  1040. static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
  1041. {
  1042. struct tpacpi_rfk *tp_rfk;
  1043. BUG_ON(id >= TPACPI_RFK_SW_MAX);
  1044. tp_rfk = tpacpi_rfkill_switches[id];
  1045. if (tp_rfk) {
  1046. rfkill_unregister(tp_rfk->rfkill);
  1047. rfkill_destroy(tp_rfk->rfkill);
  1048. tpacpi_rfkill_switches[id] = NULL;
  1049. kfree(tp_rfk);
  1050. }
  1051. }
  1052. static void printk_deprecated_rfkill_attribute(const char * const what)
  1053. {
  1054. printk_deprecated_attribute(what,
  1055. "Please switch to generic rfkill before year 2010");
  1056. }
  1057. /* sysfs <radio> enable ------------------------------------------------ */
  1058. static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
  1059. struct device_attribute *attr,
  1060. char *buf)
  1061. {
  1062. int status;
  1063. printk_deprecated_rfkill_attribute(attr->attr.name);
  1064. /* This is in the ABI... */
  1065. if (tpacpi_rfk_check_hwblock_state()) {
  1066. status = TPACPI_RFK_RADIO_OFF;
  1067. } else {
  1068. status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1069. if (status < 0)
  1070. return status;
  1071. }
  1072. return sysfs_emit(buf, "%d\n",
  1073. (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
  1074. }
  1075. static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
  1076. struct device_attribute *attr,
  1077. const char *buf, size_t count)
  1078. {
  1079. unsigned long t;
  1080. int res;
  1081. printk_deprecated_rfkill_attribute(attr->attr.name);
  1082. if (parse_strtoul(buf, 1, &t))
  1083. return -EINVAL;
  1084. tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
  1085. /* This is in the ABI... */
  1086. if (tpacpi_rfk_check_hwblock_state() && !!t)
  1087. return -EPERM;
  1088. res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
  1089. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
  1090. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1091. return (res < 0) ? res : count;
  1092. }
  1093. /* procfs -------------------------------------------------------------- */
  1094. static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
  1095. {
  1096. if (id >= TPACPI_RFK_SW_MAX)
  1097. seq_printf(m, "status:\t\tnot supported\n");
  1098. else {
  1099. int status;
  1100. /* This is in the ABI... */
  1101. if (tpacpi_rfk_check_hwblock_state()) {
  1102. status = TPACPI_RFK_RADIO_OFF;
  1103. } else {
  1104. status = tpacpi_rfk_update_swstate(
  1105. tpacpi_rfkill_switches[id]);
  1106. if (status < 0)
  1107. return status;
  1108. }
  1109. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status == TPACPI_RFK_RADIO_ON));
  1110. seq_printf(m, "commands:\tenable, disable\n");
  1111. }
  1112. return 0;
  1113. }
  1114. static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
  1115. {
  1116. char *cmd;
  1117. int status = -1;
  1118. int res = 0;
  1119. if (id >= TPACPI_RFK_SW_MAX)
  1120. return -ENODEV;
  1121. while ((cmd = strsep(&buf, ","))) {
  1122. if (strstarts(cmd, "enable"))
  1123. status = TPACPI_RFK_RADIO_ON;
  1124. else if (strstarts(cmd, "disable"))
  1125. status = TPACPI_RFK_RADIO_OFF;
  1126. else
  1127. return -EINVAL;
  1128. }
  1129. if (status != -1) {
  1130. tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
  1131. str_enable_disable(status == TPACPI_RFK_RADIO_ON),
  1132. tpacpi_rfkill_names[id]);
  1133. res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
  1134. tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
  1135. }
  1136. return res;
  1137. }
  1138. /*************************************************************************
  1139. * thinkpad-acpi driver attributes
  1140. */
  1141. /* interface_version --------------------------------------------------- */
  1142. static ssize_t interface_version_show(struct device_driver *drv, char *buf)
  1143. {
  1144. return sysfs_emit(buf, "0x%08x\n", TPACPI_SYSFS_VERSION);
  1145. }
  1146. static DRIVER_ATTR_RO(interface_version);
  1147. /* debug_level --------------------------------------------------------- */
  1148. static ssize_t debug_level_show(struct device_driver *drv, char *buf)
  1149. {
  1150. return sysfs_emit(buf, "0x%04x\n", dbg_level);
  1151. }
  1152. static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
  1153. size_t count)
  1154. {
  1155. unsigned long t;
  1156. if (parse_strtoul(buf, 0xffff, &t))
  1157. return -EINVAL;
  1158. dbg_level = t;
  1159. return count;
  1160. }
  1161. static DRIVER_ATTR_RW(debug_level);
  1162. /* version ------------------------------------------------------------- */
  1163. static ssize_t version_show(struct device_driver *drv, char *buf)
  1164. {
  1165. return sysfs_emit(buf, "%s v%s\n",
  1166. TPACPI_DESC, TPACPI_VERSION);
  1167. }
  1168. static DRIVER_ATTR_RO(version);
  1169. /* --------------------------------------------------------------------- */
  1170. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1171. /* wlsw_emulstate ------------------------------------------------------ */
  1172. static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
  1173. {
  1174. return sysfs_emit(buf, "%d\n", !!tpacpi_wlsw_emulstate);
  1175. }
  1176. static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
  1177. size_t count)
  1178. {
  1179. unsigned long t;
  1180. if (parse_strtoul(buf, 1, &t))
  1181. return -EINVAL;
  1182. if (tpacpi_wlsw_emulstate != !!t) {
  1183. tpacpi_wlsw_emulstate = !!t;
  1184. tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
  1185. }
  1186. return count;
  1187. }
  1188. static DRIVER_ATTR_RW(wlsw_emulstate);
  1189. /* bluetooth_emulstate ------------------------------------------------- */
  1190. static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
  1191. {
  1192. return sysfs_emit(buf, "%d\n", !!tpacpi_bluetooth_emulstate);
  1193. }
  1194. static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
  1195. const char *buf, size_t count)
  1196. {
  1197. unsigned long t;
  1198. if (parse_strtoul(buf, 1, &t))
  1199. return -EINVAL;
  1200. tpacpi_bluetooth_emulstate = !!t;
  1201. return count;
  1202. }
  1203. static DRIVER_ATTR_RW(bluetooth_emulstate);
  1204. /* wwan_emulstate ------------------------------------------------- */
  1205. static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
  1206. {
  1207. return sysfs_emit(buf, "%d\n", !!tpacpi_wwan_emulstate);
  1208. }
  1209. static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
  1210. size_t count)
  1211. {
  1212. unsigned long t;
  1213. if (parse_strtoul(buf, 1, &t))
  1214. return -EINVAL;
  1215. tpacpi_wwan_emulstate = !!t;
  1216. return count;
  1217. }
  1218. static DRIVER_ATTR_RW(wwan_emulstate);
  1219. /* uwb_emulstate ------------------------------------------------- */
  1220. static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
  1221. {
  1222. return sysfs_emit(buf, "%d\n", !!tpacpi_uwb_emulstate);
  1223. }
  1224. static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
  1225. size_t count)
  1226. {
  1227. unsigned long t;
  1228. if (parse_strtoul(buf, 1, &t))
  1229. return -EINVAL;
  1230. tpacpi_uwb_emulstate = !!t;
  1231. return count;
  1232. }
  1233. static DRIVER_ATTR_RW(uwb_emulstate);
  1234. #endif
  1235. /*************************************************************************
  1236. * Firmware Data
  1237. */
  1238. /*
  1239. * Table of recommended minimum BIOS versions
  1240. *
  1241. * Reasons for listing:
  1242. * 1. Stable BIOS, listed because the unknown amount of
  1243. * bugs and bad ACPI behaviour on older versions
  1244. *
  1245. * 2. BIOS or EC fw with known bugs that trigger on Linux
  1246. *
  1247. * 3. BIOS with known reduced functionality in older versions
  1248. *
  1249. * We recommend the latest BIOS and EC version.
  1250. * We only support the latest BIOS and EC fw version as a rule.
  1251. *
  1252. * Sources: IBM ThinkPad Public Web Documents (update changelogs),
  1253. * Information from users in ThinkWiki
  1254. *
  1255. * WARNING: we use this table also to detect that the machine is
  1256. * a ThinkPad in some cases, so don't remove entries lightly.
  1257. */
  1258. #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
  1259. { .vendor = (__v), \
  1260. .bios = TPID(__id1, __id2), \
  1261. .ec = TPACPI_MATCH_ANY, \
  1262. .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
  1263. | TPVER(__bv1, __bv2) }
  1264. #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
  1265. __eid, __ev1, __ev2) \
  1266. { .vendor = (__v), \
  1267. .bios = TPID(__bid1, __bid2), \
  1268. .ec = __eid, \
  1269. .quirks = TPVER(__ev1, __ev2) << 16 \
  1270. | TPVER(__bv1, __bv2) }
  1271. #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
  1272. TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
  1273. /* Outdated IBM BIOSes often lack the EC id string */
  1274. #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1275. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1276. __bv1, __bv2, TPID(__id1, __id2), \
  1277. __ev1, __ev2), \
  1278. TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
  1279. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1280. __ev1, __ev2)
  1281. /* Outdated IBM BIOSes often lack the EC id string */
  1282. #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
  1283. __eid1, __eid2, __ev1, __ev2) \
  1284. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1285. __bv1, __bv2, TPID(__eid1, __eid2), \
  1286. __ev1, __ev2), \
  1287. TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
  1288. __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
  1289. __ev1, __ev2)
  1290. #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
  1291. TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
  1292. #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
  1293. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
  1294. __bv1, __bv2, TPID(__id1, __id2), \
  1295. __ev1, __ev2)
  1296. #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
  1297. __eid1, __eid2, __ev1, __ev2) \
  1298. TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
  1299. __bv1, __bv2, TPID(__eid1, __eid2), \
  1300. __ev1, __ev2)
  1301. static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
  1302. /* Numeric models ------------------ */
  1303. /* FW MODEL BIOS VERS */
  1304. TPV_QI0('I', 'M', '6', '5'), /* 570 */
  1305. TPV_QI0('I', 'U', '2', '6'), /* 570E */
  1306. TPV_QI0('I', 'B', '5', '4'), /* 600 */
  1307. TPV_QI0('I', 'H', '4', '7'), /* 600E */
  1308. TPV_QI0('I', 'N', '3', '6'), /* 600E */
  1309. TPV_QI0('I', 'T', '5', '5'), /* 600X */
  1310. TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
  1311. TPV_QI0('I', 'I', '4', '2'), /* 770X */
  1312. TPV_QI0('I', 'O', '2', '3'), /* 770Z */
  1313. /* A-series ------------------------- */
  1314. /* FW MODEL BIOS VERS EC VERS */
  1315. TPV_QI0('I', 'W', '5', '9'), /* A20m */
  1316. TPV_QI0('I', 'V', '6', '9'), /* A20p */
  1317. TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
  1318. TPV_QI0('K', 'U', '3', '6'), /* A21e */
  1319. TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
  1320. TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
  1321. TPV_QI0('1', 'B', '1', '7'), /* A22e */
  1322. TPV_QI0('1', '3', '2', '0'), /* A22m */
  1323. TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
  1324. TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
  1325. TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
  1326. /* G-series ------------------------- */
  1327. /* FW MODEL BIOS VERS */
  1328. TPV_QI0('1', 'T', 'A', '6'), /* G40 */
  1329. TPV_QI0('1', 'X', '5', '7'), /* G41 */
  1330. /* R-series, T-series --------------- */
  1331. /* FW MODEL BIOS VERS EC VERS */
  1332. TPV_QI0('1', 'C', 'F', '0'), /* R30 */
  1333. TPV_QI0('1', 'F', 'F', '1'), /* R31 */
  1334. TPV_QI0('1', 'M', '9', '7'), /* R32 */
  1335. TPV_QI0('1', 'O', '6', '1'), /* R40 */
  1336. TPV_QI0('1', 'P', '6', '5'), /* R40 */
  1337. TPV_QI0('1', 'S', '7', '0'), /* R40e */
  1338. TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
  1339. T40/p, T41/p, T42/p (1) */
  1340. TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
  1341. TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
  1342. TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
  1343. TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
  1344. TPV_QI0('I', 'Y', '6', '1'), /* T20 */
  1345. TPV_QI0('K', 'Z', '3', '4'), /* T21 */
  1346. TPV_QI0('1', '6', '3', '2'), /* T22 */
  1347. TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
  1348. TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
  1349. TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
  1350. TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
  1351. TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
  1352. TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
  1353. /* BIOS FW BIOS VERS EC FW EC VERS */
  1354. TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
  1355. TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
  1356. /* X-series ------------------------- */
  1357. /* FW MODEL BIOS VERS EC VERS */
  1358. TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
  1359. TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
  1360. TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
  1361. TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
  1362. TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
  1363. TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
  1364. TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
  1365. TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
  1366. TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
  1367. /* (0) - older versions lack DMI EC fw string and functionality */
  1368. /* (1) - older versions known to lack functionality */
  1369. };
  1370. #undef TPV_QL1
  1371. #undef TPV_QL0
  1372. #undef TPV_QI2
  1373. #undef TPV_QI1
  1374. #undef TPV_QI0
  1375. #undef TPV_Q_X
  1376. #undef TPV_Q
  1377. static void __init tpacpi_check_outdated_fw(void)
  1378. {
  1379. unsigned long fwvers;
  1380. u16 ec_version, bios_version;
  1381. fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1382. ARRAY_SIZE(tpacpi_bios_version_qtable));
  1383. if (!fwvers)
  1384. return;
  1385. bios_version = fwvers & 0xffffU;
  1386. ec_version = (fwvers >> 16) & 0xffffU;
  1387. /* note that unknown versions are set to 0x0000 and we use that */
  1388. if ((bios_version > thinkpad_id.bios_release) ||
  1389. (ec_version > thinkpad_id.ec_release &&
  1390. ec_version != TPACPI_MATCH_ANY_VERSION)) {
  1391. /*
  1392. * The changelogs would let us track down the exact
  1393. * reason, but it is just too much of a pain to track
  1394. * it. We only list BIOSes that are either really
  1395. * broken, or really stable to begin with, so it is
  1396. * best if the user upgrades the firmware anyway.
  1397. */
  1398. pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
  1399. pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
  1400. }
  1401. }
  1402. static bool __init tpacpi_is_fw_known(void)
  1403. {
  1404. return tpacpi_check_quirks(tpacpi_bios_version_qtable,
  1405. ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
  1406. }
  1407. /****************************************************************************
  1408. ****************************************************************************
  1409. *
  1410. * Subdrivers
  1411. *
  1412. ****************************************************************************
  1413. ****************************************************************************/
  1414. /*************************************************************************
  1415. * thinkpad-acpi metadata subdriver
  1416. */
  1417. static int thinkpad_acpi_driver_read(struct seq_file *m)
  1418. {
  1419. seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
  1420. seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
  1421. return 0;
  1422. }
  1423. static struct ibm_struct thinkpad_acpi_driver_data = {
  1424. .name = "driver",
  1425. .read = thinkpad_acpi_driver_read,
  1426. };
  1427. /*************************************************************************
  1428. * Hotkey subdriver
  1429. */
  1430. /*
  1431. * ThinkPad firmware event model
  1432. *
  1433. * The ThinkPad firmware has two main event interfaces: normal ACPI
  1434. * notifications (which follow the ACPI standard), and a private event
  1435. * interface.
  1436. *
  1437. * The private event interface also issues events for the hotkeys. As
  1438. * the driver gained features, the event handling code ended up being
  1439. * built around the hotkey subdriver. This will need to be refactored
  1440. * to a more formal event API eventually.
  1441. *
  1442. * Some "hotkeys" are actually supposed to be used as event reports,
  1443. * such as "brightness has changed", "volume has changed", depending on
  1444. * the ThinkPad model and how the firmware is operating.
  1445. *
  1446. * Unlike other classes, hotkey-class events have mask/unmask control on
  1447. * non-ancient firmware. However, how it behaves changes a lot with the
  1448. * firmware model and version.
  1449. */
  1450. enum { /* hot key scan codes (derived from ACPI DSDT) */
  1451. TP_ACPI_HOTKEYSCAN_FNF1 = 0,
  1452. TP_ACPI_HOTKEYSCAN_FNF2,
  1453. TP_ACPI_HOTKEYSCAN_FNF3,
  1454. TP_ACPI_HOTKEYSCAN_FNF4,
  1455. TP_ACPI_HOTKEYSCAN_FNF5,
  1456. TP_ACPI_HOTKEYSCAN_FNF6,
  1457. TP_ACPI_HOTKEYSCAN_FNF7,
  1458. TP_ACPI_HOTKEYSCAN_FNF8,
  1459. TP_ACPI_HOTKEYSCAN_FNF9,
  1460. TP_ACPI_HOTKEYSCAN_FNF10,
  1461. TP_ACPI_HOTKEYSCAN_FNF11,
  1462. TP_ACPI_HOTKEYSCAN_FNF12,
  1463. TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
  1464. TP_ACPI_HOTKEYSCAN_FNINSERT,
  1465. TP_ACPI_HOTKEYSCAN_FNDELETE,
  1466. TP_ACPI_HOTKEYSCAN_FNHOME,
  1467. TP_ACPI_HOTKEYSCAN_FNEND,
  1468. TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1469. TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
  1470. TP_ACPI_HOTKEYSCAN_FNSPACE,
  1471. TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1472. TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1473. TP_ACPI_HOTKEYSCAN_MUTE,
  1474. TP_ACPI_HOTKEYSCAN_THINKPAD,
  1475. TP_ACPI_HOTKEYSCAN_UNK1,
  1476. TP_ACPI_HOTKEYSCAN_UNK2,
  1477. TP_ACPI_HOTKEYSCAN_MICMUTE,
  1478. TP_ACPI_HOTKEYSCAN_UNK4,
  1479. TP_ACPI_HOTKEYSCAN_CONFIG,
  1480. TP_ACPI_HOTKEYSCAN_SEARCH,
  1481. TP_ACPI_HOTKEYSCAN_SCALE,
  1482. TP_ACPI_HOTKEYSCAN_FILE,
  1483. /* Adaptive keyboard keycodes */
  1484. TP_ACPI_HOTKEYSCAN_ADAPTIVE_START, /* 32 / 0x20 */
  1485. TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
  1486. TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
  1487. TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
  1488. TP_ACPI_HOTKEYSCAN_CLOUD,
  1489. TP_ACPI_HOTKEYSCAN_UNK9,
  1490. TP_ACPI_HOTKEYSCAN_VOICE,
  1491. TP_ACPI_HOTKEYSCAN_UNK10,
  1492. TP_ACPI_HOTKEYSCAN_GESTURES,
  1493. TP_ACPI_HOTKEYSCAN_UNK11,
  1494. TP_ACPI_HOTKEYSCAN_UNK12,
  1495. TP_ACPI_HOTKEYSCAN_UNK13,
  1496. TP_ACPI_HOTKEYSCAN_CONFIG2,
  1497. TP_ACPI_HOTKEYSCAN_NEW_TAB,
  1498. TP_ACPI_HOTKEYSCAN_RELOAD,
  1499. TP_ACPI_HOTKEYSCAN_BACK,
  1500. TP_ACPI_HOTKEYSCAN_MIC_DOWN,
  1501. TP_ACPI_HOTKEYSCAN_MIC_UP,
  1502. TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
  1503. TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
  1504. TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
  1505. /* Lenovo extended keymap, starting at 0x1300 */
  1506. TP_ACPI_HOTKEYSCAN_EXTENDED_START, /* 52 / 0x34 */
  1507. /* first new observed key (star, favorites) is 0x1311 */
  1508. TP_ACPI_HOTKEYSCAN_STAR = 69,
  1509. TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
  1510. TP_ACPI_HOTKEYSCAN_CALCULATOR,
  1511. TP_ACPI_HOTKEYSCAN_BLUETOOTH,
  1512. TP_ACPI_HOTKEYSCAN_KEYBOARD,
  1513. TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, /* Used by "Lenovo Quick Clean" */
  1514. TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER,
  1515. TP_ACPI_HOTKEYSCAN_PICKUP_PHONE,
  1516. TP_ACPI_HOTKEYSCAN_HANGUP_PHONE,
  1517. };
  1518. enum { /* Keys/events available through NVRAM polling */
  1519. TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
  1520. TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
  1521. };
  1522. enum { /* Positions of some of the keys in hotkey masks */
  1523. TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
  1524. TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
  1525. TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
  1526. TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
  1527. TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
  1528. TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
  1529. TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
  1530. TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
  1531. TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
  1532. TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
  1533. TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
  1534. };
  1535. enum { /* NVRAM to ACPI HKEY group map */
  1536. TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
  1537. TP_ACPI_HKEY_ZOOM_MASK |
  1538. TP_ACPI_HKEY_DISPSWTCH_MASK |
  1539. TP_ACPI_HKEY_HIBERNATE_MASK,
  1540. TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
  1541. TP_ACPI_HKEY_BRGHTDWN_MASK,
  1542. TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
  1543. TP_ACPI_HKEY_VOLDWN_MASK |
  1544. TP_ACPI_HKEY_MUTE_MASK,
  1545. };
  1546. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1547. struct tp_nvram_state {
  1548. u16 thinkpad_toggle:1;
  1549. u16 zoom_toggle:1;
  1550. u16 display_toggle:1;
  1551. u16 thinklight_toggle:1;
  1552. u16 hibernate_toggle:1;
  1553. u16 displayexp_toggle:1;
  1554. u16 display_state:1;
  1555. u16 brightness_toggle:1;
  1556. u16 volume_toggle:1;
  1557. u16 mute:1;
  1558. u8 brightness_level;
  1559. u8 volume_level;
  1560. };
  1561. /* kthread for the hotkey poller */
  1562. static struct task_struct *tpacpi_hotkey_task;
  1563. /*
  1564. * Acquire mutex to write poller control variables as an
  1565. * atomic block.
  1566. *
  1567. * Increment hotkey_config_change when changing them if you
  1568. * want the kthread to forget old state.
  1569. *
  1570. * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1571. */
  1572. static struct mutex hotkey_thread_data_mutex;
  1573. static unsigned int hotkey_config_change;
  1574. /*
  1575. * hotkey poller control variables
  1576. *
  1577. * Must be atomic or readers will also need to acquire mutex
  1578. *
  1579. * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
  1580. * should be used only when the changes need to be taken as
  1581. * a block, OR when one needs to force the kthread to forget
  1582. * old state.
  1583. */
  1584. static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
  1585. static unsigned int hotkey_poll_freq = 10; /* Hz */
  1586. #define HOTKEY_CONFIG_CRITICAL_START \
  1587. do { \
  1588. mutex_lock(&hotkey_thread_data_mutex); \
  1589. hotkey_config_change++; \
  1590. } while (0);
  1591. #define HOTKEY_CONFIG_CRITICAL_END \
  1592. mutex_unlock(&hotkey_thread_data_mutex);
  1593. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1594. #define hotkey_source_mask 0U
  1595. #define HOTKEY_CONFIG_CRITICAL_START
  1596. #define HOTKEY_CONFIG_CRITICAL_END
  1597. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  1598. static struct mutex hotkey_mutex;
  1599. static enum { /* Reasons for waking up */
  1600. TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
  1601. TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
  1602. TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
  1603. } hotkey_wakeup_reason;
  1604. static int hotkey_autosleep_ack;
  1605. static u32 hotkey_orig_mask; /* events the BIOS had enabled */
  1606. static u32 hotkey_all_mask; /* all events supported in fw */
  1607. static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
  1608. static u32 hotkey_reserved_mask; /* events better left disabled */
  1609. static u32 hotkey_driver_mask; /* events needed by the driver */
  1610. static u32 hotkey_user_mask; /* events visible to userspace */
  1611. static u32 hotkey_acpi_mask; /* events enabled in firmware */
  1612. static bool tpacpi_driver_event(const unsigned int hkey_event);
  1613. static void hotkey_poll_setup(const bool may_warn);
  1614. /* HKEY.MHKG() return bits */
  1615. #define TP_HOTKEY_TABLET_MASK (1 << 3)
  1616. enum {
  1617. TP_ACPI_MULTI_MODE_INVALID = 0,
  1618. TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
  1619. TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
  1620. TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
  1621. TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
  1622. TP_ACPI_MULTI_MODE_STAND = 1 << 4,
  1623. TP_ACPI_MULTI_MODE_TENT = 1 << 5,
  1624. TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
  1625. };
  1626. enum {
  1627. /* The following modes are considered tablet mode for the purpose of
  1628. * reporting the status to userspace. i.e. in all these modes it makes
  1629. * sense to disable the laptop input devices such as touchpad and
  1630. * keyboard.
  1631. */
  1632. TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
  1633. TP_ACPI_MULTI_MODE_STAND |
  1634. TP_ACPI_MULTI_MODE_TENT |
  1635. TP_ACPI_MULTI_MODE_STAND_TENT,
  1636. };
  1637. static int hotkey_get_wlsw(void)
  1638. {
  1639. int status;
  1640. if (!tp_features.hotkey_wlsw)
  1641. return -ENODEV;
  1642. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  1643. if (dbg_wlswemul)
  1644. return (tpacpi_wlsw_emulstate) ?
  1645. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1646. #endif
  1647. if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
  1648. return -EIO;
  1649. return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  1650. }
  1651. static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
  1652. {
  1653. int type = (s >> 16) & 0xffff;
  1654. int value = s & 0xffff;
  1655. int mode = TP_ACPI_MULTI_MODE_INVALID;
  1656. int valid_modes = 0;
  1657. if (has_tablet_mode)
  1658. *has_tablet_mode = 0;
  1659. switch (type) {
  1660. case 1:
  1661. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1662. TP_ACPI_MULTI_MODE_TABLET |
  1663. TP_ACPI_MULTI_MODE_STAND_TENT;
  1664. break;
  1665. case 2:
  1666. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1667. TP_ACPI_MULTI_MODE_FLAT |
  1668. TP_ACPI_MULTI_MODE_TABLET |
  1669. TP_ACPI_MULTI_MODE_STAND |
  1670. TP_ACPI_MULTI_MODE_TENT;
  1671. break;
  1672. case 3:
  1673. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1674. TP_ACPI_MULTI_MODE_FLAT;
  1675. break;
  1676. case 4:
  1677. case 5:
  1678. /* In mode 4, FLAT is not specified as a valid mode. However,
  1679. * it can be seen at least on the X1 Yoga 2nd Generation.
  1680. */
  1681. valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
  1682. TP_ACPI_MULTI_MODE_FLAT |
  1683. TP_ACPI_MULTI_MODE_TABLET |
  1684. TP_ACPI_MULTI_MODE_STAND |
  1685. TP_ACPI_MULTI_MODE_TENT;
  1686. break;
  1687. default:
  1688. pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
  1689. type, value, TPACPI_MAIL);
  1690. return 0;
  1691. }
  1692. if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
  1693. *has_tablet_mode = 1;
  1694. switch (value) {
  1695. case 1:
  1696. mode = TP_ACPI_MULTI_MODE_LAPTOP;
  1697. break;
  1698. case 2:
  1699. mode = TP_ACPI_MULTI_MODE_FLAT;
  1700. break;
  1701. case 3:
  1702. mode = TP_ACPI_MULTI_MODE_TABLET;
  1703. break;
  1704. case 4:
  1705. if (type == 1)
  1706. mode = TP_ACPI_MULTI_MODE_STAND_TENT;
  1707. else
  1708. mode = TP_ACPI_MULTI_MODE_STAND;
  1709. break;
  1710. case 5:
  1711. mode = TP_ACPI_MULTI_MODE_TENT;
  1712. break;
  1713. default:
  1714. if (type == 5 && value == 0xffff) {
  1715. pr_warn("Multi mode status is undetected, assuming laptop\n");
  1716. return 0;
  1717. }
  1718. }
  1719. if (!(mode & valid_modes)) {
  1720. pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
  1721. value, type, TPACPI_MAIL);
  1722. return 0;
  1723. }
  1724. return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
  1725. }
  1726. static int hotkey_get_tablet_mode(int *status)
  1727. {
  1728. int s;
  1729. switch (tp_features.hotkey_tablet) {
  1730. case TP_HOTKEY_TABLET_USES_MHKG:
  1731. if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
  1732. return -EIO;
  1733. *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
  1734. break;
  1735. case TP_HOTKEY_TABLET_USES_GMMS:
  1736. if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
  1737. return -EIO;
  1738. *status = hotkey_gmms_get_tablet_mode(s, NULL);
  1739. break;
  1740. default:
  1741. break;
  1742. }
  1743. return 0;
  1744. }
  1745. /*
  1746. * Reads current event mask from firmware, and updates
  1747. * hotkey_acpi_mask accordingly. Also resets any bits
  1748. * from hotkey_user_mask that are unavailable to be
  1749. * delivered (shadow requirement of the userspace ABI).
  1750. */
  1751. static int hotkey_mask_get(void)
  1752. {
  1753. lockdep_assert_held(&hotkey_mutex);
  1754. if (tp_features.hotkey_mask) {
  1755. u32 m = 0;
  1756. if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
  1757. return -EIO;
  1758. hotkey_acpi_mask = m;
  1759. } else {
  1760. /* no mask support doesn't mean no event support... */
  1761. hotkey_acpi_mask = hotkey_all_mask;
  1762. }
  1763. /* sync userspace-visible mask */
  1764. hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
  1765. return 0;
  1766. }
  1767. static void hotkey_mask_warn_incomplete_mask(void)
  1768. {
  1769. /* log only what the user can fix... */
  1770. const u32 wantedmask = hotkey_driver_mask &
  1771. ~(hotkey_acpi_mask | hotkey_source_mask) &
  1772. (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
  1773. if (wantedmask)
  1774. pr_notice("required events 0x%08x not enabled!\n", wantedmask);
  1775. }
  1776. /*
  1777. * Set the firmware mask when supported
  1778. *
  1779. * Also calls hotkey_mask_get to update hotkey_acpi_mask.
  1780. *
  1781. * NOTE: does not set bits in hotkey_user_mask, but may reset them.
  1782. */
  1783. static int hotkey_mask_set(u32 mask)
  1784. {
  1785. int i;
  1786. int rc = 0;
  1787. const u32 fwmask = mask & ~hotkey_source_mask;
  1788. lockdep_assert_held(&hotkey_mutex);
  1789. if (tp_features.hotkey_mask) {
  1790. for (i = 0; i < 32; i++) {
  1791. if (!acpi_evalf(hkey_handle,
  1792. NULL, "MHKM", "vdd", i + 1,
  1793. !!(mask & (1 << i)))) {
  1794. rc = -EIO;
  1795. break;
  1796. }
  1797. }
  1798. }
  1799. /*
  1800. * We *must* make an inconditional call to hotkey_mask_get to
  1801. * refresh hotkey_acpi_mask and update hotkey_user_mask
  1802. *
  1803. * Take the opportunity to also log when we cannot _enable_
  1804. * a given event.
  1805. */
  1806. if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
  1807. pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
  1808. fwmask, hotkey_acpi_mask);
  1809. }
  1810. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
  1811. hotkey_mask_warn_incomplete_mask();
  1812. return rc;
  1813. }
  1814. /*
  1815. * Sets hotkey_user_mask and tries to set the firmware mask
  1816. */
  1817. static int hotkey_user_mask_set(const u32 mask)
  1818. {
  1819. int rc;
  1820. lockdep_assert_held(&hotkey_mutex);
  1821. /* Give people a chance to notice they are doing something that
  1822. * is bound to go boom on their users sooner or later */
  1823. if (!tp_warned.hotkey_mask_ff &&
  1824. (mask == 0xffff || mask == 0xffffff ||
  1825. mask == 0xffffffff)) {
  1826. tp_warned.hotkey_mask_ff = 1;
  1827. pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
  1828. mask);
  1829. pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
  1830. }
  1831. /* Try to enable what the user asked for, plus whatever we need.
  1832. * this syncs everything but won't enable bits in hotkey_user_mask */
  1833. rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
  1834. /* Enable the available bits in hotkey_user_mask */
  1835. hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
  1836. return rc;
  1837. }
  1838. /*
  1839. * Sets the driver hotkey mask.
  1840. *
  1841. * Can be called even if the hotkey subdriver is inactive
  1842. */
  1843. static int tpacpi_hotkey_driver_mask_set(const u32 mask)
  1844. {
  1845. int rc;
  1846. /* Do the right thing if hotkey_init has not been called yet */
  1847. if (!tp_features.hotkey) {
  1848. hotkey_driver_mask = mask;
  1849. return 0;
  1850. }
  1851. mutex_lock(&hotkey_mutex);
  1852. HOTKEY_CONFIG_CRITICAL_START
  1853. hotkey_driver_mask = mask;
  1854. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1855. hotkey_source_mask |= (mask & ~hotkey_all_mask);
  1856. #endif
  1857. HOTKEY_CONFIG_CRITICAL_END
  1858. rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
  1859. ~hotkey_source_mask);
  1860. hotkey_poll_setup(true);
  1861. mutex_unlock(&hotkey_mutex);
  1862. return rc;
  1863. }
  1864. static int hotkey_status_get(int *status)
  1865. {
  1866. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  1867. return -EIO;
  1868. return 0;
  1869. }
  1870. static int hotkey_status_set(bool enable)
  1871. {
  1872. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
  1873. return -EIO;
  1874. return 0;
  1875. }
  1876. static void tpacpi_input_send_tabletsw(void)
  1877. {
  1878. int state;
  1879. if (tp_features.hotkey_tablet &&
  1880. !hotkey_get_tablet_mode(&state)) {
  1881. mutex_lock(&tpacpi_inputdev_send_mutex);
  1882. input_report_switch(tpacpi_inputdev,
  1883. SW_TABLET_MODE, !!state);
  1884. input_sync(tpacpi_inputdev);
  1885. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1886. }
  1887. }
  1888. static bool tpacpi_input_send_key(const u32 hkey, bool *send_acpi_ev)
  1889. {
  1890. bool known_ev;
  1891. u32 scancode;
  1892. if (tpacpi_driver_event(hkey))
  1893. return true;
  1894. /*
  1895. * Before the conversion to using the sparse-keymap helpers the driver used to
  1896. * map the hkey event codes to 0x00 - 0x4d scancodes so that a straight scancode
  1897. * indexed array could be used to map scancodes to keycodes:
  1898. *
  1899. * 0x1001 - 0x1020 -> 0x00 - 0x1f (Original ThinkPad events)
  1900. * 0x1103 - 0x1116 -> 0x20 - 0x33 (Adaptive keyboard, 2014 X1 Carbon)
  1901. * 0x1300 - 0x1319 -> 0x34 - 0x4d (Additional keys send in 2017+ models)
  1902. *
  1903. * The sparse-keymap tables still use these scancodes for these ranges to
  1904. * preserve userspace API compatibility (e.g. hwdb keymappings).
  1905. */
  1906. if (hkey >= TP_HKEY_EV_ORIG_KEY_START &&
  1907. hkey <= TP_HKEY_EV_ORIG_KEY_END) {
  1908. scancode = hkey - TP_HKEY_EV_ORIG_KEY_START;
  1909. if (!(hotkey_user_mask & (1 << scancode)))
  1910. return true; /* Not reported but still a known code */
  1911. } else if (hkey >= TP_HKEY_EV_ADAPTIVE_KEY_START &&
  1912. hkey <= TP_HKEY_EV_ADAPTIVE_KEY_END) {
  1913. scancode = hkey - TP_HKEY_EV_ADAPTIVE_KEY_START +
  1914. TP_ACPI_HOTKEYSCAN_ADAPTIVE_START;
  1915. } else if (hkey >= TP_HKEY_EV_EXTENDED_KEY_START &&
  1916. hkey <= TP_HKEY_EV_EXTENDED_KEY_END) {
  1917. scancode = hkey - TP_HKEY_EV_EXTENDED_KEY_START +
  1918. TP_ACPI_HOTKEYSCAN_EXTENDED_START;
  1919. } else {
  1920. /*
  1921. * Do not send ACPI netlink events for unknown hotkeys, to
  1922. * avoid userspace starting to rely on them. Instead these
  1923. * should be added to the keymap to send evdev events.
  1924. */
  1925. if (send_acpi_ev)
  1926. *send_acpi_ev = false;
  1927. scancode = hkey;
  1928. }
  1929. mutex_lock(&tpacpi_inputdev_send_mutex);
  1930. known_ev = sparse_keymap_report_event(tpacpi_inputdev, scancode, 1, true);
  1931. mutex_unlock(&tpacpi_inputdev_send_mutex);
  1932. return known_ev;
  1933. }
  1934. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  1935. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
  1936. /* Do NOT call without validating scancode first */
  1937. static void tpacpi_hotkey_send_key(unsigned int scancode)
  1938. {
  1939. tpacpi_input_send_key(TP_HKEY_EV_ORIG_KEY_START + scancode, NULL);
  1940. }
  1941. static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
  1942. {
  1943. u8 d;
  1944. if (m & TP_NVRAM_HKEY_GROUP_HK2) {
  1945. d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
  1946. n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
  1947. n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
  1948. n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
  1949. n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
  1950. }
  1951. if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
  1952. d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
  1953. n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
  1954. }
  1955. if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
  1956. d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
  1957. n->displayexp_toggle =
  1958. !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
  1959. }
  1960. if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
  1961. d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  1962. n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  1963. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  1964. n->brightness_toggle =
  1965. !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
  1966. }
  1967. if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
  1968. d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  1969. n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
  1970. >> TP_NVRAM_POS_LEVEL_VOLUME;
  1971. n->mute = !!(d & TP_NVRAM_MASK_MUTE);
  1972. n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
  1973. }
  1974. }
  1975. #define TPACPI_COMPARE_KEY(__scancode, __member) \
  1976. do { \
  1977. if ((event_mask & (1 << __scancode)) && \
  1978. oldn->__member != newn->__member) \
  1979. tpacpi_hotkey_send_key(__scancode); \
  1980. } while (0)
  1981. #define TPACPI_MAY_SEND_KEY(__scancode) \
  1982. do { \
  1983. if (event_mask & (1 << __scancode)) \
  1984. tpacpi_hotkey_send_key(__scancode); \
  1985. } while (0)
  1986. static void issue_volchange(const unsigned int oldvol,
  1987. const unsigned int newvol,
  1988. const u32 event_mask)
  1989. {
  1990. unsigned int i = oldvol;
  1991. while (i > newvol) {
  1992. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  1993. i--;
  1994. }
  1995. while (i < newvol) {
  1996. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  1997. i++;
  1998. }
  1999. }
  2000. static void issue_brightnesschange(const unsigned int oldbrt,
  2001. const unsigned int newbrt,
  2002. const u32 event_mask)
  2003. {
  2004. unsigned int i = oldbrt;
  2005. while (i > newbrt) {
  2006. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  2007. i--;
  2008. }
  2009. while (i < newbrt) {
  2010. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  2011. i++;
  2012. }
  2013. }
  2014. static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
  2015. struct tp_nvram_state *newn,
  2016. const u32 event_mask)
  2017. {
  2018. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
  2019. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
  2020. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
  2021. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
  2022. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
  2023. TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
  2024. /*
  2025. * Handle volume
  2026. *
  2027. * This code is supposed to duplicate the IBM firmware behaviour:
  2028. * - Pressing MUTE issues mute hotkey message, even when already mute
  2029. * - Pressing Volume up/down issues volume up/down hotkey messages,
  2030. * even when already at maximum or minimum volume
  2031. * - The act of unmuting issues volume up/down notification,
  2032. * depending which key was used to unmute
  2033. *
  2034. * We are constrained to what the NVRAM can tell us, which is not much
  2035. * and certainly not enough if more than one volume hotkey was pressed
  2036. * since the last poll cycle.
  2037. *
  2038. * Just to make our life interesting, some newer Lenovo ThinkPads have
  2039. * bugs in the BIOS and may fail to update volume_toggle properly.
  2040. */
  2041. if (newn->mute) {
  2042. /* muted */
  2043. if (!oldn->mute ||
  2044. oldn->volume_toggle != newn->volume_toggle ||
  2045. oldn->volume_level != newn->volume_level) {
  2046. /* recently muted, or repeated mute keypress, or
  2047. * multiple presses ending in mute */
  2048. issue_volchange(oldn->volume_level, newn->volume_level,
  2049. event_mask);
  2050. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
  2051. }
  2052. } else {
  2053. /* unmute */
  2054. if (oldn->mute) {
  2055. /* recently unmuted, issue 'unmute' keypress */
  2056. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2057. }
  2058. if (oldn->volume_level != newn->volume_level) {
  2059. issue_volchange(oldn->volume_level, newn->volume_level,
  2060. event_mask);
  2061. } else if (oldn->volume_toggle != newn->volume_toggle) {
  2062. /* repeated vol up/down keypress at end of scale ? */
  2063. if (newn->volume_level == 0)
  2064. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
  2065. else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
  2066. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
  2067. }
  2068. }
  2069. /* handle brightness */
  2070. if (oldn->brightness_level != newn->brightness_level) {
  2071. issue_brightnesschange(oldn->brightness_level,
  2072. newn->brightness_level, event_mask);
  2073. } else if (oldn->brightness_toggle != newn->brightness_toggle) {
  2074. /* repeated key presses that didn't change state */
  2075. if (newn->brightness_level == 0)
  2076. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
  2077. else if (newn->brightness_level >= bright_maxlvl
  2078. && !tp_features.bright_unkfw)
  2079. TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
  2080. }
  2081. #undef TPACPI_COMPARE_KEY
  2082. #undef TPACPI_MAY_SEND_KEY
  2083. }
  2084. /*
  2085. * Polling driver
  2086. *
  2087. * We track all events in hotkey_source_mask all the time, since
  2088. * most of them are edge-based. We only issue those requested by
  2089. * hotkey_user_mask or hotkey_driver_mask, though.
  2090. */
  2091. static int hotkey_kthread(void *data)
  2092. {
  2093. struct tp_nvram_state s[2] = { 0 };
  2094. u32 poll_mask, event_mask;
  2095. unsigned int si, so;
  2096. unsigned long t;
  2097. unsigned int change_detector;
  2098. unsigned int poll_freq;
  2099. bool was_frozen;
  2100. if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
  2101. goto exit;
  2102. set_freezable();
  2103. so = 0;
  2104. si = 1;
  2105. t = 0;
  2106. /* Initial state for compares */
  2107. mutex_lock(&hotkey_thread_data_mutex);
  2108. change_detector = hotkey_config_change;
  2109. poll_mask = hotkey_source_mask;
  2110. event_mask = hotkey_source_mask &
  2111. (hotkey_driver_mask | hotkey_user_mask);
  2112. poll_freq = hotkey_poll_freq;
  2113. mutex_unlock(&hotkey_thread_data_mutex);
  2114. hotkey_read_nvram(&s[so], poll_mask);
  2115. while (!kthread_should_stop()) {
  2116. if (t == 0) {
  2117. if (likely(poll_freq))
  2118. t = 1000/poll_freq;
  2119. else
  2120. t = 100; /* should never happen... */
  2121. }
  2122. t = msleep_interruptible(t);
  2123. if (unlikely(kthread_freezable_should_stop(&was_frozen)))
  2124. break;
  2125. if (t > 0 && !was_frozen)
  2126. continue;
  2127. mutex_lock(&hotkey_thread_data_mutex);
  2128. if (was_frozen || hotkey_config_change != change_detector) {
  2129. /* forget old state on thaw or config change */
  2130. si = so;
  2131. t = 0;
  2132. change_detector = hotkey_config_change;
  2133. }
  2134. poll_mask = hotkey_source_mask;
  2135. event_mask = hotkey_source_mask &
  2136. (hotkey_driver_mask | hotkey_user_mask);
  2137. poll_freq = hotkey_poll_freq;
  2138. mutex_unlock(&hotkey_thread_data_mutex);
  2139. if (likely(poll_mask)) {
  2140. hotkey_read_nvram(&s[si], poll_mask);
  2141. if (likely(si != so)) {
  2142. hotkey_compare_and_issue_event(&s[so], &s[si],
  2143. event_mask);
  2144. }
  2145. }
  2146. so = si;
  2147. si ^= 1;
  2148. }
  2149. exit:
  2150. return 0;
  2151. }
  2152. static void hotkey_poll_stop_sync(void)
  2153. {
  2154. lockdep_assert_held(&hotkey_mutex);
  2155. if (tpacpi_hotkey_task) {
  2156. kthread_stop(tpacpi_hotkey_task);
  2157. tpacpi_hotkey_task = NULL;
  2158. }
  2159. }
  2160. static void hotkey_poll_setup(const bool may_warn)
  2161. {
  2162. const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
  2163. const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
  2164. lockdep_assert_held(&hotkey_mutex);
  2165. if (hotkey_poll_freq > 0 &&
  2166. (poll_driver_mask ||
  2167. (poll_user_mask && tpacpi_inputdev->users > 0))) {
  2168. if (!tpacpi_hotkey_task) {
  2169. tpacpi_hotkey_task = kthread_run(hotkey_kthread,
  2170. NULL, TPACPI_NVRAM_KTHREAD_NAME);
  2171. if (IS_ERR(tpacpi_hotkey_task)) {
  2172. tpacpi_hotkey_task = NULL;
  2173. pr_err("could not create kernel thread for hotkey polling\n");
  2174. }
  2175. }
  2176. } else {
  2177. hotkey_poll_stop_sync();
  2178. if (may_warn && (poll_driver_mask || poll_user_mask) &&
  2179. hotkey_poll_freq == 0) {
  2180. pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
  2181. poll_user_mask, poll_driver_mask);
  2182. }
  2183. }
  2184. }
  2185. static void hotkey_poll_setup_safe(const bool may_warn)
  2186. {
  2187. mutex_lock(&hotkey_mutex);
  2188. hotkey_poll_setup(may_warn);
  2189. mutex_unlock(&hotkey_mutex);
  2190. }
  2191. static void hotkey_poll_set_freq(unsigned int freq)
  2192. {
  2193. lockdep_assert_held(&hotkey_mutex);
  2194. if (!freq)
  2195. hotkey_poll_stop_sync();
  2196. hotkey_poll_freq = freq;
  2197. }
  2198. #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2199. static void hotkey_poll_setup(const bool __unused)
  2200. {
  2201. }
  2202. static void hotkey_poll_setup_safe(const bool __unused)
  2203. {
  2204. }
  2205. static void hotkey_poll_stop_sync(void)
  2206. {
  2207. }
  2208. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2209. static int hotkey_inputdev_open(struct input_dev *dev)
  2210. {
  2211. switch (tpacpi_lifecycle) {
  2212. case TPACPI_LIFE_INIT:
  2213. case TPACPI_LIFE_RUNNING:
  2214. hotkey_poll_setup_safe(false);
  2215. return 0;
  2216. case TPACPI_LIFE_EXITING:
  2217. return -EBUSY;
  2218. }
  2219. /* Should only happen if tpacpi_lifecycle is corrupt */
  2220. BUG();
  2221. return -EBUSY;
  2222. }
  2223. static void hotkey_inputdev_close(struct input_dev *dev)
  2224. {
  2225. /* disable hotkey polling when possible */
  2226. if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
  2227. !(hotkey_source_mask & hotkey_driver_mask))
  2228. hotkey_poll_setup_safe(false);
  2229. }
  2230. /* sysfs hotkey enable ------------------------------------------------- */
  2231. static ssize_t hotkey_enable_show(struct device *dev,
  2232. struct device_attribute *attr,
  2233. char *buf)
  2234. {
  2235. int res, status;
  2236. printk_deprecated_attribute("hotkey_enable",
  2237. "Hotkey reporting is always enabled");
  2238. res = hotkey_status_get(&status);
  2239. if (res)
  2240. return res;
  2241. return sysfs_emit(buf, "%d\n", status);
  2242. }
  2243. static ssize_t hotkey_enable_store(struct device *dev,
  2244. struct device_attribute *attr,
  2245. const char *buf, size_t count)
  2246. {
  2247. unsigned long t;
  2248. printk_deprecated_attribute("hotkey_enable",
  2249. "Hotkeys can be disabled through hotkey_mask");
  2250. if (parse_strtoul(buf, 1, &t))
  2251. return -EINVAL;
  2252. if (t == 0)
  2253. return -EPERM;
  2254. return count;
  2255. }
  2256. static DEVICE_ATTR_RW(hotkey_enable);
  2257. /* sysfs hotkey mask --------------------------------------------------- */
  2258. static ssize_t hotkey_mask_show(struct device *dev,
  2259. struct device_attribute *attr,
  2260. char *buf)
  2261. {
  2262. return sysfs_emit(buf, "0x%08x\n", hotkey_user_mask);
  2263. }
  2264. static ssize_t hotkey_mask_store(struct device *dev,
  2265. struct device_attribute *attr,
  2266. const char *buf, size_t count)
  2267. {
  2268. unsigned long t;
  2269. int res;
  2270. if (parse_strtoul(buf, 0xffffffffUL, &t))
  2271. return -EINVAL;
  2272. if (mutex_lock_killable(&hotkey_mutex))
  2273. return -ERESTARTSYS;
  2274. res = hotkey_user_mask_set(t);
  2275. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2276. hotkey_poll_setup(true);
  2277. #endif
  2278. mutex_unlock(&hotkey_mutex);
  2279. tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
  2280. return (res) ? res : count;
  2281. }
  2282. static DEVICE_ATTR_RW(hotkey_mask);
  2283. /* sysfs hotkey bios_enabled ------------------------------------------- */
  2284. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  2285. struct device_attribute *attr,
  2286. char *buf)
  2287. {
  2288. return sysfs_emit(buf, "0\n");
  2289. }
  2290. static DEVICE_ATTR_RO(hotkey_bios_enabled);
  2291. /* sysfs hotkey bios_mask ---------------------------------------------- */
  2292. static ssize_t hotkey_bios_mask_show(struct device *dev,
  2293. struct device_attribute *attr,
  2294. char *buf)
  2295. {
  2296. printk_deprecated_attribute("hotkey_bios_mask",
  2297. "This attribute is useless.");
  2298. return sysfs_emit(buf, "0x%08x\n", hotkey_orig_mask);
  2299. }
  2300. static DEVICE_ATTR_RO(hotkey_bios_mask);
  2301. /* sysfs hotkey all_mask ----------------------------------------------- */
  2302. static ssize_t hotkey_all_mask_show(struct device *dev,
  2303. struct device_attribute *attr,
  2304. char *buf)
  2305. {
  2306. return sysfs_emit(buf, "0x%08x\n",
  2307. hotkey_all_mask | hotkey_source_mask);
  2308. }
  2309. static DEVICE_ATTR_RO(hotkey_all_mask);
  2310. /* sysfs hotkey all_mask ----------------------------------------------- */
  2311. static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
  2312. struct device_attribute *attr,
  2313. char *buf)
  2314. {
  2315. return sysfs_emit(buf, "0x%08x\n",
  2316. hotkey_adaptive_all_mask | hotkey_source_mask);
  2317. }
  2318. static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
  2319. /* sysfs hotkey recommended_mask --------------------------------------- */
  2320. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  2321. struct device_attribute *attr,
  2322. char *buf)
  2323. {
  2324. return sysfs_emit(buf, "0x%08x\n",
  2325. (hotkey_all_mask | hotkey_source_mask)
  2326. & ~hotkey_reserved_mask);
  2327. }
  2328. static DEVICE_ATTR_RO(hotkey_recommended_mask);
  2329. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2330. /* sysfs hotkey hotkey_source_mask ------------------------------------- */
  2331. static ssize_t hotkey_source_mask_show(struct device *dev,
  2332. struct device_attribute *attr,
  2333. char *buf)
  2334. {
  2335. return sysfs_emit(buf, "0x%08x\n", hotkey_source_mask);
  2336. }
  2337. static ssize_t hotkey_source_mask_store(struct device *dev,
  2338. struct device_attribute *attr,
  2339. const char *buf, size_t count)
  2340. {
  2341. unsigned long t;
  2342. u32 r_ev;
  2343. int rc;
  2344. if (parse_strtoul(buf, 0xffffffffUL, &t) ||
  2345. ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
  2346. return -EINVAL;
  2347. if (mutex_lock_killable(&hotkey_mutex))
  2348. return -ERESTARTSYS;
  2349. HOTKEY_CONFIG_CRITICAL_START
  2350. hotkey_source_mask = t;
  2351. HOTKEY_CONFIG_CRITICAL_END
  2352. rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
  2353. ~hotkey_source_mask);
  2354. hotkey_poll_setup(true);
  2355. /* check if events needed by the driver got disabled */
  2356. r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
  2357. & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
  2358. mutex_unlock(&hotkey_mutex);
  2359. if (rc < 0)
  2360. pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
  2361. if (r_ev)
  2362. pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
  2363. r_ev);
  2364. tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
  2365. return (rc < 0) ? rc : count;
  2366. }
  2367. static DEVICE_ATTR_RW(hotkey_source_mask);
  2368. /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
  2369. static ssize_t hotkey_poll_freq_show(struct device *dev,
  2370. struct device_attribute *attr,
  2371. char *buf)
  2372. {
  2373. return sysfs_emit(buf, "%d\n", hotkey_poll_freq);
  2374. }
  2375. static ssize_t hotkey_poll_freq_store(struct device *dev,
  2376. struct device_attribute *attr,
  2377. const char *buf, size_t count)
  2378. {
  2379. unsigned long t;
  2380. if (parse_strtoul(buf, 25, &t))
  2381. return -EINVAL;
  2382. if (mutex_lock_killable(&hotkey_mutex))
  2383. return -ERESTARTSYS;
  2384. hotkey_poll_set_freq(t);
  2385. hotkey_poll_setup(true);
  2386. mutex_unlock(&hotkey_mutex);
  2387. tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
  2388. return count;
  2389. }
  2390. static DEVICE_ATTR_RW(hotkey_poll_freq);
  2391. #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
  2392. /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
  2393. static ssize_t hotkey_radio_sw_show(struct device *dev,
  2394. struct device_attribute *attr,
  2395. char *buf)
  2396. {
  2397. int res;
  2398. res = hotkey_get_wlsw();
  2399. if (res < 0)
  2400. return res;
  2401. /* Opportunistic update */
  2402. tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
  2403. return sysfs_emit(buf, "%d\n",
  2404. (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
  2405. }
  2406. static DEVICE_ATTR_RO(hotkey_radio_sw);
  2407. static void hotkey_radio_sw_notify_change(void)
  2408. {
  2409. if (tp_features.hotkey_wlsw)
  2410. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2411. "hotkey_radio_sw");
  2412. }
  2413. /* sysfs hotkey tablet mode (pollable) --------------------------------- */
  2414. static ssize_t hotkey_tablet_mode_show(struct device *dev,
  2415. struct device_attribute *attr,
  2416. char *buf)
  2417. {
  2418. int res, s;
  2419. res = hotkey_get_tablet_mode(&s);
  2420. if (res < 0)
  2421. return res;
  2422. return sysfs_emit(buf, "%d\n", !!s);
  2423. }
  2424. static DEVICE_ATTR_RO(hotkey_tablet_mode);
  2425. static void hotkey_tablet_mode_notify_change(void)
  2426. {
  2427. if (tp_features.hotkey_tablet)
  2428. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2429. "hotkey_tablet_mode");
  2430. }
  2431. /* sysfs wakeup reason (pollable) -------------------------------------- */
  2432. static ssize_t hotkey_wakeup_reason_show(struct device *dev,
  2433. struct device_attribute *attr,
  2434. char *buf)
  2435. {
  2436. return sysfs_emit(buf, "%d\n", hotkey_wakeup_reason);
  2437. }
  2438. static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
  2439. static void hotkey_wakeup_reason_notify_change(void)
  2440. {
  2441. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2442. "wakeup_reason");
  2443. }
  2444. /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
  2445. static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
  2446. struct device_attribute *attr,
  2447. char *buf)
  2448. {
  2449. return sysfs_emit(buf, "%d\n", hotkey_autosleep_ack);
  2450. }
  2451. static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
  2452. hotkey_wakeup_hotunplug_complete_show, NULL);
  2453. static void hotkey_wakeup_hotunplug_complete_notify_change(void)
  2454. {
  2455. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
  2456. "wakeup_hotunplug_complete");
  2457. }
  2458. /* sysfs adaptive kbd mode --------------------------------------------- */
  2459. static int adaptive_keyboard_get_mode(void);
  2460. static int adaptive_keyboard_set_mode(int new_mode);
  2461. enum ADAPTIVE_KEY_MODE {
  2462. HOME_MODE,
  2463. WEB_BROWSER_MODE,
  2464. WEB_CONFERENCE_MODE,
  2465. FUNCTION_MODE,
  2466. LAYFLAT_MODE
  2467. };
  2468. static ssize_t adaptive_kbd_mode_show(struct device *dev,
  2469. struct device_attribute *attr,
  2470. char *buf)
  2471. {
  2472. int current_mode;
  2473. current_mode = adaptive_keyboard_get_mode();
  2474. if (current_mode < 0)
  2475. return current_mode;
  2476. return sysfs_emit(buf, "%d\n", current_mode);
  2477. }
  2478. static ssize_t adaptive_kbd_mode_store(struct device *dev,
  2479. struct device_attribute *attr,
  2480. const char *buf, size_t count)
  2481. {
  2482. unsigned long t;
  2483. int res;
  2484. if (parse_strtoul(buf, LAYFLAT_MODE, &t))
  2485. return -EINVAL;
  2486. res = adaptive_keyboard_set_mode(t);
  2487. return (res < 0) ? res : count;
  2488. }
  2489. static DEVICE_ATTR_RW(adaptive_kbd_mode);
  2490. static struct attribute *adaptive_kbd_attributes[] = {
  2491. &dev_attr_adaptive_kbd_mode.attr,
  2492. NULL
  2493. };
  2494. static umode_t hadaptive_kbd_attr_is_visible(struct kobject *kobj,
  2495. struct attribute *attr, int n)
  2496. {
  2497. return tp_features.has_adaptive_kbd ? attr->mode : 0;
  2498. }
  2499. static const struct attribute_group adaptive_kbd_attr_group = {
  2500. .is_visible = hadaptive_kbd_attr_is_visible,
  2501. .attrs = adaptive_kbd_attributes,
  2502. };
  2503. /* --------------------------------------------------------------------- */
  2504. static struct attribute *hotkey_attributes[] = {
  2505. &dev_attr_hotkey_enable.attr,
  2506. &dev_attr_hotkey_bios_enabled.attr,
  2507. &dev_attr_hotkey_bios_mask.attr,
  2508. &dev_attr_wakeup_reason.attr,
  2509. &dev_attr_wakeup_hotunplug_complete.attr,
  2510. &dev_attr_hotkey_mask.attr,
  2511. &dev_attr_hotkey_all_mask.attr,
  2512. &dev_attr_hotkey_adaptive_all_mask.attr,
  2513. &dev_attr_hotkey_recommended_mask.attr,
  2514. &dev_attr_hotkey_tablet_mode.attr,
  2515. &dev_attr_hotkey_radio_sw.attr,
  2516. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2517. &dev_attr_hotkey_source_mask.attr,
  2518. &dev_attr_hotkey_poll_freq.attr,
  2519. #endif
  2520. NULL
  2521. };
  2522. static umode_t hotkey_attr_is_visible(struct kobject *kobj,
  2523. struct attribute *attr, int n)
  2524. {
  2525. if (attr == &dev_attr_hotkey_tablet_mode.attr) {
  2526. if (!tp_features.hotkey_tablet)
  2527. return 0;
  2528. } else if (attr == &dev_attr_hotkey_radio_sw.attr) {
  2529. if (!tp_features.hotkey_wlsw)
  2530. return 0;
  2531. }
  2532. return attr->mode;
  2533. }
  2534. static const struct attribute_group hotkey_attr_group = {
  2535. .is_visible = hotkey_attr_is_visible,
  2536. .attrs = hotkey_attributes,
  2537. };
  2538. /*
  2539. * Sync both the hw and sw blocking state of all switches
  2540. */
  2541. static void tpacpi_send_radiosw_update(void)
  2542. {
  2543. int wlsw;
  2544. /*
  2545. * We must sync all rfkill controllers *before* issuing any
  2546. * rfkill input events, or we will race the rfkill core input
  2547. * handler.
  2548. *
  2549. * tpacpi_inputdev_send_mutex works as a synchronization point
  2550. * for the above.
  2551. *
  2552. * We optimize to avoid numerous calls to hotkey_get_wlsw.
  2553. */
  2554. wlsw = hotkey_get_wlsw();
  2555. /* Sync hw blocking state first if it is hw-blocked */
  2556. if (wlsw == TPACPI_RFK_RADIO_OFF)
  2557. tpacpi_rfk_update_hwblock_state(true);
  2558. /* Sync hw blocking state last if it is hw-unblocked */
  2559. if (wlsw == TPACPI_RFK_RADIO_ON)
  2560. tpacpi_rfk_update_hwblock_state(false);
  2561. /* Issue rfkill input event for WLSW switch */
  2562. if (!(wlsw < 0)) {
  2563. mutex_lock(&tpacpi_inputdev_send_mutex);
  2564. input_report_switch(tpacpi_inputdev,
  2565. SW_RFKILL_ALL, (wlsw > 0));
  2566. input_sync(tpacpi_inputdev);
  2567. mutex_unlock(&tpacpi_inputdev_send_mutex);
  2568. }
  2569. /*
  2570. * this can be unconditional, as we will poll state again
  2571. * if userspace uses the notify to read data
  2572. */
  2573. hotkey_radio_sw_notify_change();
  2574. }
  2575. static void hotkey_exit(void)
  2576. {
  2577. mutex_lock(&hotkey_mutex);
  2578. hotkey_poll_stop_sync();
  2579. dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
  2580. "restoring original HKEY status and mask\n");
  2581. /* yes, there is a bitwise or below, we want the
  2582. * functions to be called even if one of them fail */
  2583. if (((tp_features.hotkey_mask &&
  2584. hotkey_mask_set(hotkey_orig_mask)) |
  2585. hotkey_status_set(false)) != 0)
  2586. pr_err("failed to restore hot key mask to BIOS defaults\n");
  2587. mutex_unlock(&hotkey_mutex);
  2588. }
  2589. /*
  2590. * HKEY quirks:
  2591. * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
  2592. */
  2593. #define TPACPI_HK_Q_INIMASK 0x0001
  2594. static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
  2595. TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
  2596. TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
  2597. TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
  2598. TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
  2599. TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
  2600. TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
  2601. TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
  2602. TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
  2603. TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
  2604. TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
  2605. TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
  2606. TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
  2607. TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
  2608. TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
  2609. TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
  2610. TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
  2611. TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
  2612. TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
  2613. TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
  2614. };
  2615. static int hotkey_init_tablet_mode(void)
  2616. {
  2617. int in_tablet_mode = 0, res;
  2618. char *type = NULL;
  2619. if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
  2620. int has_tablet_mode;
  2621. in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
  2622. &has_tablet_mode);
  2623. /*
  2624. * The Yoga 11e series has 2 accelerometers described by a
  2625. * BOSC0200 ACPI node. This setup relies on a Windows service
  2626. * which calls special ACPI methods on this node to report
  2627. * the laptop/tent/tablet mode to the EC. The bmc150 iio driver
  2628. * does not support this, so skip the hotkey on these models.
  2629. */
  2630. if (has_tablet_mode && !dual_accel_detect())
  2631. tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
  2632. type = "GMMS";
  2633. } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
  2634. /* For X41t, X60t, X61t Tablets... */
  2635. tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
  2636. in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
  2637. type = "MHKG";
  2638. }
  2639. if (!tp_features.hotkey_tablet)
  2640. return 0;
  2641. pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
  2642. type, in_tablet_mode ? "tablet" : "laptop");
  2643. return in_tablet_mode;
  2644. }
  2645. static const struct key_entry keymap_ibm[] __initconst = {
  2646. /* Original hotkey mappings translated scancodes 0x00 - 0x1f */
  2647. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF1, { KEY_FN_F1 } },
  2648. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF2, { KEY_BATTERY } },
  2649. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF3, { KEY_COFFEE } },
  2650. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF4, { KEY_SLEEP } },
  2651. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF5, { KEY_WLAN } },
  2652. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF6, { KEY_FN_F6 } },
  2653. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF7, { KEY_SWITCHVIDEOMODE } },
  2654. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF8, { KEY_FN_F8 } },
  2655. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF9, { KEY_FN_F9 } },
  2656. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF10, { KEY_FN_F10 } },
  2657. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF11, { KEY_FN_F11 } },
  2658. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF12, { KEY_SUSPEND } },
  2659. /* Brightness: firmware always reacts, suppressed through hotkey_reserved_mask. */
  2660. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNHOME, { KEY_BRIGHTNESSUP } },
  2661. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNEND, { KEY_BRIGHTNESSDOWN } },
  2662. /* Thinklight: firmware always reacts, suppressed through hotkey_reserved_mask. */
  2663. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNPAGEUP, { KEY_KBDILLUMTOGGLE } },
  2664. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNSPACE, { KEY_ZOOM } },
  2665. /*
  2666. * Volume: firmware always reacts and reprograms the built-in *extra* mixer.
  2667. * Suppressed by default through hotkey_reserved_mask.
  2668. */
  2669. { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEUP, { KEY_VOLUMEUP } },
  2670. { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, { KEY_VOLUMEDOWN } },
  2671. { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE, { KEY_MUTE } },
  2672. { KE_KEY, TP_ACPI_HOTKEYSCAN_THINKPAD, { KEY_VENDOR } },
  2673. { KE_END }
  2674. };
  2675. static const struct key_entry keymap_lenovo[] __initconst = {
  2676. /* Original hotkey mappings translated scancodes 0x00 - 0x1f */
  2677. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF1, { KEY_FN_F1 } },
  2678. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF2, { KEY_COFFEE } },
  2679. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF3, { KEY_BATTERY } },
  2680. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF4, { KEY_SLEEP } },
  2681. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF5, { KEY_WLAN } },
  2682. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF6, { KEY_CAMERA, } },
  2683. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF7, { KEY_SWITCHVIDEOMODE } },
  2684. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF8, { KEY_FN_F8 } },
  2685. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF9, { KEY_FN_F9 } },
  2686. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF10, { KEY_FN_F10 } },
  2687. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF11, { KEY_FN_F11 } },
  2688. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNF12, { KEY_SUSPEND } },
  2689. /*
  2690. * These should be enabled --only-- when ACPI video is disabled and
  2691. * are handled in a special way by the init code.
  2692. */
  2693. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNHOME, { KEY_BRIGHTNESSUP } },
  2694. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNEND, { KEY_BRIGHTNESSDOWN } },
  2695. /* Suppressed by default through hotkey_reserved_mask. */
  2696. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNPAGEUP, { KEY_KBDILLUMTOGGLE } },
  2697. { KE_KEY, TP_ACPI_HOTKEYSCAN_FNSPACE, { KEY_ZOOM } },
  2698. /*
  2699. * Volume: z60/z61, T60 (BIOS version?): firmware always reacts and
  2700. * reprograms the built-in *extra* mixer.
  2701. * T60?, T61, R60?, R61: firmware and EC tries to send these over
  2702. * the regular keyboard (not through tpacpi). There are still weird bugs
  2703. * re. MUTE. May cause the BIOS to interfere with the HDA mixer.
  2704. * Suppressed by default through hotkey_reserved_mask.
  2705. */
  2706. { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEUP, { KEY_VOLUMEUP } },
  2707. { KE_KEY, TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, { KEY_VOLUMEDOWN } },
  2708. { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE, { KEY_MUTE } },
  2709. { KE_KEY, TP_ACPI_HOTKEYSCAN_THINKPAD, { KEY_VENDOR } },
  2710. { KE_KEY, TP_ACPI_HOTKEYSCAN_MICMUTE, { KEY_MICMUTE } },
  2711. { KE_KEY, TP_ACPI_HOTKEYSCAN_CONFIG, { KEY_CONFIG } },
  2712. { KE_KEY, TP_ACPI_HOTKEYSCAN_SEARCH, { KEY_SEARCH } },
  2713. { KE_KEY, TP_ACPI_HOTKEYSCAN_SCALE, { KEY_SCALE } },
  2714. { KE_KEY, TP_ACPI_HOTKEYSCAN_FILE, { KEY_FILE } },
  2715. /* Adaptive keyboard mappings for Carbon X1 2014 translated scancodes 0x20 - 0x33 */
  2716. { KE_KEY, TP_ACPI_HOTKEYSCAN_MUTE2, { KEY_RESERVED } },
  2717. { KE_KEY, TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO, { KEY_BRIGHTNESS_MIN } },
  2718. { KE_KEY, TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL, { KEY_SELECTIVE_SCREENSHOT } },
  2719. { KE_KEY, TP_ACPI_HOTKEYSCAN_CLOUD, { KEY_XFER } },
  2720. { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK9, { KEY_RESERVED } },
  2721. { KE_KEY, TP_ACPI_HOTKEYSCAN_VOICE, { KEY_VOICECOMMAND } },
  2722. { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK10, { KEY_RESERVED } },
  2723. { KE_KEY, TP_ACPI_HOTKEYSCAN_GESTURES, { KEY_RESERVED } },
  2724. { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK11, { KEY_RESERVED } },
  2725. { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK12, { KEY_RESERVED } },
  2726. { KE_KEY, TP_ACPI_HOTKEYSCAN_UNK13, { KEY_RESERVED } },
  2727. { KE_KEY, TP_ACPI_HOTKEYSCAN_CONFIG2, { KEY_CONFIG } },
  2728. { KE_KEY, TP_ACPI_HOTKEYSCAN_NEW_TAB, { KEY_RESERVED } },
  2729. { KE_KEY, TP_ACPI_HOTKEYSCAN_RELOAD, { KEY_REFRESH } },
  2730. { KE_KEY, TP_ACPI_HOTKEYSCAN_BACK, { KEY_BACK } },
  2731. { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_DOWN, { KEY_RESERVED } },
  2732. { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_UP, { KEY_RESERVED } },
  2733. { KE_KEY, TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION, { KEY_RESERVED } },
  2734. { KE_KEY, TP_ACPI_HOTKEYSCAN_CAMERA_MODE, { KEY_RESERVED } },
  2735. { KE_KEY, TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY, { KEY_RESERVED } },
  2736. /* Extended hotkeys mappings translated scancodes 0x34 - 0x4d */
  2737. { KE_KEY, TP_ACPI_HOTKEYSCAN_STAR, { KEY_BOOKMARKS } },
  2738. { KE_KEY, TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2, { KEY_SELECTIVE_SCREENSHOT } },
  2739. { KE_KEY, TP_ACPI_HOTKEYSCAN_CALCULATOR, { KEY_CALC } },
  2740. { KE_KEY, TP_ACPI_HOTKEYSCAN_BLUETOOTH, { KEY_BLUETOOTH } },
  2741. { KE_KEY, TP_ACPI_HOTKEYSCAN_KEYBOARD, { KEY_KEYBOARD } },
  2742. /* Used by "Lenovo Quick Clean" */
  2743. { KE_KEY, TP_ACPI_HOTKEYSCAN_FN_RIGHT_SHIFT, { KEY_FN_RIGHT_SHIFT } },
  2744. { KE_KEY, TP_ACPI_HOTKEYSCAN_NOTIFICATION_CENTER, { KEY_NOTIFICATION_CENTER } },
  2745. { KE_KEY, TP_ACPI_HOTKEYSCAN_PICKUP_PHONE, { KEY_PICKUP_PHONE } },
  2746. { KE_KEY, TP_ACPI_HOTKEYSCAN_HANGUP_PHONE, { KEY_HANGUP_PHONE } },
  2747. /*
  2748. * All mapping below are for raw untranslated hkey event codes mapped directly
  2749. * after switching to sparse keymap support. The mappings above use translated
  2750. * scancodes to preserve uAPI compatibility, see tpacpi_input_send_key().
  2751. */
  2752. { KE_KEY, 0x131d, { KEY_VENDOR } }, /* System debug info, similar to old ThinkPad key */
  2753. { KE_KEY, TP_HKEY_EV_TRACK_DOUBLETAP /* 0x8036 */, { KEY_PROG4 } },
  2754. { KE_END }
  2755. };
  2756. static int __init hotkey_init(struct ibm_init_struct *iibm)
  2757. {
  2758. enum keymap_index {
  2759. TPACPI_KEYMAP_IBM_GENERIC = 0,
  2760. TPACPI_KEYMAP_LENOVO_GENERIC,
  2761. };
  2762. static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
  2763. /* Generic maps (fallback) */
  2764. {
  2765. .vendor = PCI_VENDOR_ID_IBM,
  2766. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2767. .quirks = TPACPI_KEYMAP_IBM_GENERIC,
  2768. },
  2769. {
  2770. .vendor = PCI_VENDOR_ID_LENOVO,
  2771. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  2772. .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
  2773. },
  2774. };
  2775. unsigned long keymap_id, quirks;
  2776. const struct key_entry *keymap;
  2777. bool radiosw_state = false;
  2778. bool tabletsw_state = false;
  2779. int hkeyv, res, status;
  2780. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2781. "initializing hotkey subdriver\n");
  2782. BUG_ON(!tpacpi_inputdev);
  2783. BUG_ON(tpacpi_inputdev->open != NULL ||
  2784. tpacpi_inputdev->close != NULL);
  2785. TPACPI_ACPIHANDLE_INIT(hkey);
  2786. mutex_init(&hotkey_mutex);
  2787. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2788. mutex_init(&hotkey_thread_data_mutex);
  2789. #endif
  2790. /* hotkey not supported on 570 */
  2791. tp_features.hotkey = hkey_handle != NULL;
  2792. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2793. "hotkeys are %s\n",
  2794. str_supported(tp_features.hotkey));
  2795. if (!tp_features.hotkey)
  2796. return -ENODEV;
  2797. quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
  2798. ARRAY_SIZE(tpacpi_hotkey_qtable));
  2799. tpacpi_disable_brightness_delay();
  2800. /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
  2801. A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
  2802. for HKEY interface version 0x100 */
  2803. if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
  2804. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2805. "firmware HKEY interface version: 0x%x\n",
  2806. hkeyv);
  2807. switch (hkeyv >> 8) {
  2808. case 1:
  2809. /*
  2810. * MHKV 0x100 in A31, R40, R40e,
  2811. * T4x, X31, and later
  2812. */
  2813. /* Paranoia check AND init hotkey_all_mask */
  2814. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2815. "MHKA", "qd")) {
  2816. pr_err("missing MHKA handler, please report this to %s\n",
  2817. TPACPI_MAIL);
  2818. /* Fallback: pre-init for FN+F3,F4,F12 */
  2819. hotkey_all_mask = 0x080cU;
  2820. } else {
  2821. tp_features.hotkey_mask = 1;
  2822. }
  2823. break;
  2824. case 2:
  2825. /*
  2826. * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
  2827. */
  2828. /* Paranoia check AND init hotkey_all_mask */
  2829. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  2830. "MHKA", "dd", 1)) {
  2831. pr_err("missing MHKA handler, please report this to %s\n",
  2832. TPACPI_MAIL);
  2833. /* Fallback: pre-init for FN+F3,F4,F12 */
  2834. hotkey_all_mask = 0x080cU;
  2835. } else {
  2836. tp_features.hotkey_mask = 1;
  2837. }
  2838. /*
  2839. * Check if we have an adaptive keyboard, like on the
  2840. * Lenovo Carbon X1 2014 (2nd Gen).
  2841. */
  2842. if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
  2843. "MHKA", "dd", 2)) {
  2844. if (hotkey_adaptive_all_mask != 0)
  2845. tp_features.has_adaptive_kbd = true;
  2846. } else {
  2847. tp_features.has_adaptive_kbd = false;
  2848. hotkey_adaptive_all_mask = 0x0U;
  2849. }
  2850. break;
  2851. default:
  2852. pr_err("unknown version of the HKEY interface: 0x%x\n",
  2853. hkeyv);
  2854. pr_err("please report this to %s\n", TPACPI_MAIL);
  2855. break;
  2856. }
  2857. }
  2858. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2859. "hotkey masks are %s\n",
  2860. str_supported(tp_features.hotkey_mask));
  2861. /* Init hotkey_all_mask if not initialized yet */
  2862. if (!tp_features.hotkey_mask && !hotkey_all_mask &&
  2863. (quirks & TPACPI_HK_Q_INIMASK))
  2864. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  2865. /* Init hotkey_acpi_mask and hotkey_orig_mask */
  2866. if (tp_features.hotkey_mask) {
  2867. /* hotkey_source_mask *must* be zero for
  2868. * the first hotkey_mask_get to return hotkey_orig_mask */
  2869. mutex_lock(&hotkey_mutex);
  2870. res = hotkey_mask_get();
  2871. mutex_unlock(&hotkey_mutex);
  2872. if (res)
  2873. return res;
  2874. hotkey_orig_mask = hotkey_acpi_mask;
  2875. } else {
  2876. hotkey_orig_mask = hotkey_all_mask;
  2877. hotkey_acpi_mask = hotkey_all_mask;
  2878. }
  2879. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  2880. if (dbg_wlswemul) {
  2881. tp_features.hotkey_wlsw = 1;
  2882. radiosw_state = !!tpacpi_wlsw_emulstate;
  2883. pr_info("radio switch emulation enabled\n");
  2884. } else
  2885. #endif
  2886. /* Not all thinkpads have a hardware radio switch */
  2887. if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  2888. tp_features.hotkey_wlsw = 1;
  2889. radiosw_state = !!status;
  2890. pr_info("radio switch found; radios are %s\n", str_enabled_disabled(status & BIT(0)));
  2891. }
  2892. tabletsw_state = hotkey_init_tablet_mode();
  2893. /* Set up key map */
  2894. keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
  2895. ARRAY_SIZE(tpacpi_keymap_qtable));
  2896. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2897. "using keymap number %lu\n", keymap_id);
  2898. /* Keys which should be reserved on both IBM and Lenovo models */
  2899. hotkey_reserved_mask = TP_ACPI_HKEY_KBD_LIGHT_MASK |
  2900. TP_ACPI_HKEY_VOLUP_MASK |
  2901. TP_ACPI_HKEY_VOLDWN_MASK |
  2902. TP_ACPI_HKEY_MUTE_MASK;
  2903. /*
  2904. * Reserve brightness up/down unconditionally on IBM models, on Lenovo
  2905. * models these are disabled based on acpi_video_get_backlight_type().
  2906. */
  2907. if (keymap_id == TPACPI_KEYMAP_IBM_GENERIC) {
  2908. hotkey_reserved_mask |= TP_ACPI_HKEY_BRGHTUP_MASK |
  2909. TP_ACPI_HKEY_BRGHTDWN_MASK;
  2910. keymap = keymap_ibm;
  2911. } else {
  2912. keymap = keymap_lenovo;
  2913. }
  2914. res = sparse_keymap_setup(tpacpi_inputdev, keymap, NULL);
  2915. if (res)
  2916. return res;
  2917. if (tp_features.hotkey_wlsw) {
  2918. input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
  2919. input_report_switch(tpacpi_inputdev,
  2920. SW_RFKILL_ALL, radiosw_state);
  2921. }
  2922. if (tp_features.hotkey_tablet) {
  2923. input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
  2924. input_report_switch(tpacpi_inputdev,
  2925. SW_TABLET_MODE, tabletsw_state);
  2926. }
  2927. /* Do not issue duplicate brightness change events to
  2928. * userspace. tpacpi_detect_brightness_capabilities() must have
  2929. * been called before this point */
  2930. if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
  2931. pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
  2932. pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
  2933. /* Disable brightness up/down on Lenovo thinkpads when
  2934. * ACPI is handling them, otherwise it is plain impossible
  2935. * for userspace to do something even remotely sane */
  2936. hotkey_reserved_mask |= TP_ACPI_HKEY_BRGHTUP_MASK |
  2937. TP_ACPI_HKEY_BRGHTDWN_MASK;
  2938. }
  2939. #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
  2940. hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
  2941. & ~hotkey_all_mask
  2942. & ~hotkey_reserved_mask;
  2943. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2944. "hotkey source mask 0x%08x, polling freq %u\n",
  2945. hotkey_source_mask, hotkey_poll_freq);
  2946. #endif
  2947. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2948. "enabling firmware HKEY event interface...\n");
  2949. res = hotkey_status_set(true);
  2950. if (res) {
  2951. hotkey_exit();
  2952. return res;
  2953. }
  2954. mutex_lock(&hotkey_mutex);
  2955. res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
  2956. | hotkey_driver_mask)
  2957. & ~hotkey_source_mask);
  2958. mutex_unlock(&hotkey_mutex);
  2959. if (res < 0 && res != -ENXIO) {
  2960. hotkey_exit();
  2961. return res;
  2962. }
  2963. hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
  2964. & ~hotkey_reserved_mask;
  2965. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
  2966. "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
  2967. hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
  2968. tpacpi_inputdev->open = &hotkey_inputdev_open;
  2969. tpacpi_inputdev->close = &hotkey_inputdev_close;
  2970. hotkey_poll_setup_safe(true);
  2971. /* Enable doubletap by default */
  2972. tp_features.trackpoint_doubletap = 1;
  2973. return 0;
  2974. }
  2975. /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
  2976. * mode, Web conference mode, Function mode and Lay-flat mode.
  2977. * We support Home mode and Function mode currently.
  2978. *
  2979. * Will consider support rest of modes in future.
  2980. *
  2981. */
  2982. static const int adaptive_keyboard_modes[] = {
  2983. HOME_MODE,
  2984. /* WEB_BROWSER_MODE = 2,
  2985. WEB_CONFERENCE_MODE = 3, */
  2986. FUNCTION_MODE
  2987. };
  2988. /* press Fn key a while second, it will switch to Function Mode. Then
  2989. * release Fn key, previous mode be restored.
  2990. */
  2991. static bool adaptive_keyboard_mode_is_saved;
  2992. static int adaptive_keyboard_prev_mode;
  2993. static int adaptive_keyboard_get_mode(void)
  2994. {
  2995. int mode = 0;
  2996. if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
  2997. pr_err("Cannot read adaptive keyboard mode\n");
  2998. return -EIO;
  2999. }
  3000. return mode;
  3001. }
  3002. static int adaptive_keyboard_set_mode(int new_mode)
  3003. {
  3004. if (new_mode < 0 ||
  3005. new_mode > LAYFLAT_MODE)
  3006. return -EINVAL;
  3007. if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
  3008. pr_err("Cannot set adaptive keyboard mode\n");
  3009. return -EIO;
  3010. }
  3011. return 0;
  3012. }
  3013. static int adaptive_keyboard_get_next_mode(int mode)
  3014. {
  3015. size_t i;
  3016. size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
  3017. for (i = 0; i <= max_mode; i++) {
  3018. if (adaptive_keyboard_modes[i] == mode)
  3019. break;
  3020. }
  3021. if (i >= max_mode)
  3022. i = 0;
  3023. else
  3024. i++;
  3025. return adaptive_keyboard_modes[i];
  3026. }
  3027. static void adaptive_keyboard_change_row(void)
  3028. {
  3029. int mode;
  3030. if (adaptive_keyboard_mode_is_saved) {
  3031. mode = adaptive_keyboard_prev_mode;
  3032. adaptive_keyboard_mode_is_saved = false;
  3033. } else {
  3034. mode = adaptive_keyboard_get_mode();
  3035. if (mode < 0)
  3036. return;
  3037. mode = adaptive_keyboard_get_next_mode(mode);
  3038. }
  3039. adaptive_keyboard_set_mode(mode);
  3040. }
  3041. static void adaptive_keyboard_s_quickview_row(void)
  3042. {
  3043. int mode;
  3044. mode = adaptive_keyboard_get_mode();
  3045. if (mode < 0)
  3046. return;
  3047. adaptive_keyboard_prev_mode = mode;
  3048. adaptive_keyboard_mode_is_saved = true;
  3049. adaptive_keyboard_set_mode(FUNCTION_MODE);
  3050. }
  3051. /* 0x1000-0x1FFF: key presses */
  3052. static bool hotkey_notify_hotkey(const u32 hkey, bool *send_acpi_ev)
  3053. {
  3054. /* Never send ACPI netlink events for original hotkeys (hkey: 0x1001 - 0x1020) */
  3055. if (hkey >= TP_HKEY_EV_ORIG_KEY_START && hkey <= TP_HKEY_EV_ORIG_KEY_END) {
  3056. *send_acpi_ev = false;
  3057. /* Original hotkeys may be polled from NVRAM instead */
  3058. unsigned int scancode = hkey - TP_HKEY_EV_ORIG_KEY_START;
  3059. if (hotkey_source_mask & (1 << scancode))
  3060. return true;
  3061. }
  3062. return tpacpi_input_send_key(hkey, send_acpi_ev);
  3063. }
  3064. /* 0x2000-0x2FFF: Wakeup reason */
  3065. static bool hotkey_notify_wakeup(const u32 hkey, bool *send_acpi_ev)
  3066. {
  3067. switch (hkey) {
  3068. case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
  3069. case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
  3070. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  3071. *send_acpi_ev = false;
  3072. break;
  3073. case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
  3074. case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
  3075. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  3076. *send_acpi_ev = false;
  3077. break;
  3078. case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
  3079. case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
  3080. pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
  3081. /* how to auto-heal: */
  3082. /* 2313: woke up from S3, go to S4/S5 */
  3083. /* 2413: woke up from S4, go to S5 */
  3084. break;
  3085. default:
  3086. return false;
  3087. }
  3088. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  3089. pr_info("woke up due to a hot-unplug request...\n");
  3090. hotkey_wakeup_reason_notify_change();
  3091. }
  3092. return true;
  3093. }
  3094. /* 0x4000-0x4FFF: dock-related events */
  3095. static bool hotkey_notify_dockevent(const u32 hkey, bool *send_acpi_ev)
  3096. {
  3097. switch (hkey) {
  3098. case TP_HKEY_EV_UNDOCK_ACK:
  3099. /* ACPI undock operation completed after wakeup */
  3100. hotkey_autosleep_ack = 1;
  3101. pr_info("undocked\n");
  3102. hotkey_wakeup_hotunplug_complete_notify_change();
  3103. return true;
  3104. case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
  3105. pr_info("docked into hotplug port replicator\n");
  3106. return true;
  3107. case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
  3108. pr_info("undocked from hotplug port replicator\n");
  3109. return true;
  3110. /*
  3111. * Deliberately ignore attaching and detaching the keybord cover to avoid
  3112. * duplicates from intel-vbtn, which already emits SW_TABLET_MODE events
  3113. * to userspace.
  3114. *
  3115. * Please refer to the following thread for more information and a preliminary
  3116. * implementation using the GTOP ("Get Tablet OPtions") interface that could be
  3117. * extended to other attachment options of the ThinkPad X1 Tablet series, such as
  3118. * the Pico cartridge dock module:
  3119. * https://lore.kernel.org/platform-driver-x86/38cb8265-1e30-d547-9e12-b4ae290be737@a-kobel.de/
  3120. */
  3121. case TP_HKEY_EV_KBD_COVER_ATTACH:
  3122. case TP_HKEY_EV_KBD_COVER_DETACH:
  3123. *send_acpi_ev = false;
  3124. return true;
  3125. default:
  3126. return false;
  3127. }
  3128. }
  3129. /* 0x5000-0x5FFF: human interface helpers */
  3130. static bool hotkey_notify_usrevent(const u32 hkey, bool *send_acpi_ev)
  3131. {
  3132. switch (hkey) {
  3133. case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
  3134. case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
  3135. return true;
  3136. case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
  3137. case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
  3138. tpacpi_input_send_tabletsw();
  3139. hotkey_tablet_mode_notify_change();
  3140. *send_acpi_ev = false;
  3141. return true;
  3142. case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
  3143. case TP_HKEY_EV_LID_OPEN: /* Lid opened */
  3144. case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
  3145. /* do not propagate these events */
  3146. *send_acpi_ev = false;
  3147. return true;
  3148. default:
  3149. return false;
  3150. }
  3151. }
  3152. static void thermal_dump_all_sensors(void);
  3153. static void palmsensor_refresh(void);
  3154. /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
  3155. static bool hotkey_notify_6xxx(const u32 hkey, bool *send_acpi_ev)
  3156. {
  3157. switch (hkey) {
  3158. case TP_HKEY_EV_THM_TABLE_CHANGED:
  3159. pr_debug("EC reports: Thermal Table has changed\n");
  3160. /* recommended action: do nothing, we don't have
  3161. * Lenovo ATM information */
  3162. return true;
  3163. case TP_HKEY_EV_THM_CSM_COMPLETED:
  3164. pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
  3165. /* Thermal event - pass on to event handler */
  3166. tpacpi_driver_event(hkey);
  3167. return true;
  3168. case TP_HKEY_EV_THM_TRANSFM_CHANGED:
  3169. pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
  3170. /* recommended action: do nothing, we don't have
  3171. * Lenovo ATM information */
  3172. return true;
  3173. case TP_HKEY_EV_ALARM_BAT_HOT:
  3174. pr_crit("THERMAL ALARM: battery is too hot!\n");
  3175. /* recommended action: warn user through gui */
  3176. break;
  3177. case TP_HKEY_EV_ALARM_BAT_XHOT:
  3178. pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
  3179. /* recommended action: immediate sleep/hibernate */
  3180. break;
  3181. case TP_HKEY_EV_ALARM_BAT_LIM_CHANGE:
  3182. pr_debug("Battery Info: battery charge threshold changed\n");
  3183. /* User changed charging threshold. No action needed */
  3184. return true;
  3185. case TP_HKEY_EV_ALARM_SENSOR_HOT:
  3186. pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
  3187. /* recommended action: warn user through gui, that */
  3188. /* some internal component is too hot */
  3189. break;
  3190. case TP_HKEY_EV_ALARM_SENSOR_XHOT:
  3191. pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
  3192. /* recommended action: immediate sleep/hibernate */
  3193. break;
  3194. case TP_HKEY_EV_AC_CHANGED:
  3195. /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
  3196. * AC status changed; can be triggered by plugging or
  3197. * unplugging AC adapter, docking or undocking. */
  3198. fallthrough;
  3199. case TP_HKEY_EV_KEY_NUMLOCK:
  3200. case TP_HKEY_EV_KEY_FN:
  3201. /* key press events, we just ignore them as long as the EC
  3202. * is still reporting them in the normal keyboard stream */
  3203. *send_acpi_ev = false;
  3204. return true;
  3205. case TP_HKEY_EV_KEY_FN_ESC:
  3206. /* Get the media key status to force the status LED to update */
  3207. acpi_evalf(hkey_handle, NULL, "GMKS", "v");
  3208. *send_acpi_ev = false;
  3209. return true;
  3210. case TP_HKEY_EV_TABLET_CHANGED:
  3211. tpacpi_input_send_tabletsw();
  3212. hotkey_tablet_mode_notify_change();
  3213. *send_acpi_ev = false;
  3214. return true;
  3215. case TP_HKEY_EV_PALM_DETECTED:
  3216. case TP_HKEY_EV_PALM_UNDETECTED:
  3217. /* palm detected - pass on to event handler */
  3218. palmsensor_refresh();
  3219. return true;
  3220. default:
  3221. /* report simply as unknown, no sensor dump */
  3222. return false;
  3223. }
  3224. thermal_dump_all_sensors();
  3225. return true;
  3226. }
  3227. static bool hotkey_notify_8xxx(const u32 hkey, bool *send_acpi_ev)
  3228. {
  3229. switch (hkey) {
  3230. case TP_HKEY_EV_TRACK_DOUBLETAP:
  3231. if (tp_features.trackpoint_doubletap)
  3232. tpacpi_input_send_key(hkey, send_acpi_ev);
  3233. return true;
  3234. default:
  3235. return false;
  3236. }
  3237. }
  3238. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  3239. {
  3240. u32 hkey;
  3241. bool send_acpi_ev;
  3242. bool known_ev;
  3243. if (event != 0x80) {
  3244. pr_err("unknown HKEY notification event %d\n", event);
  3245. /* forward it to userspace, maybe it knows how to handle it */
  3246. acpi_bus_generate_netlink_event(
  3247. ibm->acpi->device->pnp.device_class,
  3248. dev_name(&ibm->acpi->device->dev),
  3249. event, 0);
  3250. return;
  3251. }
  3252. while (1) {
  3253. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  3254. pr_err("failed to retrieve HKEY event\n");
  3255. return;
  3256. }
  3257. if (hkey == 0) {
  3258. /* queue empty */
  3259. return;
  3260. }
  3261. send_acpi_ev = true;
  3262. known_ev = false;
  3263. switch (hkey >> 12) {
  3264. case 1:
  3265. /* 0x1000-0x1FFF: key presses */
  3266. known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev);
  3267. break;
  3268. case 2:
  3269. /* 0x2000-0x2FFF: Wakeup reason */
  3270. known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev);
  3271. break;
  3272. case 3:
  3273. /* 0x3000-0x3FFF: bay-related wakeups */
  3274. switch (hkey) {
  3275. case TP_HKEY_EV_BAYEJ_ACK:
  3276. hotkey_autosleep_ack = 1;
  3277. pr_info("bay ejected\n");
  3278. hotkey_wakeup_hotunplug_complete_notify_change();
  3279. known_ev = true;
  3280. break;
  3281. case TP_HKEY_EV_OPTDRV_EJ:
  3282. /* FIXME: kick libata if SATA link offline */
  3283. known_ev = true;
  3284. break;
  3285. }
  3286. break;
  3287. case 4:
  3288. /* 0x4000-0x4FFF: dock-related events */
  3289. known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev);
  3290. break;
  3291. case 5:
  3292. /* 0x5000-0x5FFF: human interface helpers */
  3293. known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev);
  3294. break;
  3295. case 6:
  3296. /* 0x6000-0x6FFF: thermal alarms/notices and
  3297. * keyboard events */
  3298. known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev);
  3299. break;
  3300. case 7:
  3301. /* 0x7000-0x7FFF: misc */
  3302. if (tp_features.hotkey_wlsw &&
  3303. hkey == TP_HKEY_EV_RFKILL_CHANGED) {
  3304. tpacpi_send_radiosw_update();
  3305. send_acpi_ev = false;
  3306. known_ev = true;
  3307. }
  3308. break;
  3309. case 8:
  3310. /* 0x8000-0x8FFF: misc2 */
  3311. known_ev = hotkey_notify_8xxx(hkey, &send_acpi_ev);
  3312. break;
  3313. }
  3314. if (!known_ev) {
  3315. pr_notice("unhandled HKEY event 0x%04x\n", hkey);
  3316. pr_notice("please report the conditions when this event happened to %s\n",
  3317. TPACPI_MAIL);
  3318. }
  3319. /* netlink events */
  3320. if (send_acpi_ev) {
  3321. acpi_bus_generate_netlink_event(
  3322. ibm->acpi->device->pnp.device_class,
  3323. dev_name(&ibm->acpi->device->dev),
  3324. event, hkey);
  3325. }
  3326. }
  3327. }
  3328. static void hotkey_suspend(void)
  3329. {
  3330. /* Do these on suspend, we get the events on early resume! */
  3331. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  3332. hotkey_autosleep_ack = 0;
  3333. /* save previous mode of adaptive keyboard of X1 Carbon */
  3334. if (tp_features.has_adaptive_kbd) {
  3335. if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
  3336. "GTRW", "dd", 0)) {
  3337. pr_err("Cannot read adaptive keyboard mode.\n");
  3338. }
  3339. }
  3340. }
  3341. static void hotkey_resume(void)
  3342. {
  3343. tpacpi_disable_brightness_delay();
  3344. mutex_lock(&hotkey_mutex);
  3345. if (hotkey_status_set(true) < 0 ||
  3346. hotkey_mask_set(hotkey_acpi_mask) < 0)
  3347. pr_err("error while attempting to reset the event firmware interface\n");
  3348. mutex_unlock(&hotkey_mutex);
  3349. tpacpi_send_radiosw_update();
  3350. tpacpi_input_send_tabletsw();
  3351. hotkey_tablet_mode_notify_change();
  3352. hotkey_wakeup_reason_notify_change();
  3353. hotkey_wakeup_hotunplug_complete_notify_change();
  3354. hotkey_poll_setup_safe(false);
  3355. /* restore previous mode of adapive keyboard of X1 Carbon */
  3356. if (tp_features.has_adaptive_kbd) {
  3357. if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
  3358. adaptive_keyboard_prev_mode)) {
  3359. pr_err("Cannot set adaptive keyboard mode.\n");
  3360. }
  3361. }
  3362. }
  3363. /* procfs -------------------------------------------------------------- */
  3364. static int hotkey_read(struct seq_file *m)
  3365. {
  3366. int res, status;
  3367. if (!tp_features.hotkey) {
  3368. seq_printf(m, "status:\t\tnot supported\n");
  3369. return 0;
  3370. }
  3371. if (mutex_lock_killable(&hotkey_mutex))
  3372. return -ERESTARTSYS;
  3373. res = hotkey_status_get(&status);
  3374. if (!res)
  3375. res = hotkey_mask_get();
  3376. mutex_unlock(&hotkey_mutex);
  3377. if (res)
  3378. return res;
  3379. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
  3380. if (hotkey_all_mask) {
  3381. seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
  3382. seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
  3383. } else {
  3384. seq_printf(m, "mask:\t\tnot supported\n");
  3385. seq_printf(m, "commands:\tenable, disable, reset\n");
  3386. }
  3387. return 0;
  3388. }
  3389. static void hotkey_enabledisable_warn(bool enable)
  3390. {
  3391. tpacpi_log_usertask("procfs hotkey enable/disable");
  3392. if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
  3393. pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
  3394. pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
  3395. }
  3396. static int hotkey_write(char *buf)
  3397. {
  3398. int res;
  3399. u32 mask;
  3400. char *cmd;
  3401. if (!tp_features.hotkey)
  3402. return -ENODEV;
  3403. if (mutex_lock_killable(&hotkey_mutex))
  3404. return -ERESTARTSYS;
  3405. mask = hotkey_user_mask;
  3406. res = 0;
  3407. while ((cmd = strsep(&buf, ","))) {
  3408. if (strstarts(cmd, "enable")) {
  3409. hotkey_enabledisable_warn(1);
  3410. } else if (strstarts(cmd, "disable")) {
  3411. hotkey_enabledisable_warn(0);
  3412. res = -EPERM;
  3413. } else if (strstarts(cmd, "reset")) {
  3414. mask = (hotkey_all_mask | hotkey_source_mask)
  3415. & ~hotkey_reserved_mask;
  3416. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  3417. /* mask set */
  3418. } else if (sscanf(cmd, "%x", &mask) == 1) {
  3419. /* mask set */
  3420. } else {
  3421. res = -EINVAL;
  3422. goto errexit;
  3423. }
  3424. }
  3425. if (!res) {
  3426. tpacpi_disclose_usertask("procfs hotkey",
  3427. "set mask to 0x%08x\n", mask);
  3428. res = hotkey_user_mask_set(mask);
  3429. }
  3430. errexit:
  3431. mutex_unlock(&hotkey_mutex);
  3432. return res;
  3433. }
  3434. static const struct acpi_device_id ibm_htk_device_ids[] = {
  3435. {TPACPI_ACPI_IBM_HKEY_HID, 0},
  3436. {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
  3437. {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
  3438. {"", 0},
  3439. };
  3440. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  3441. .hid = ibm_htk_device_ids,
  3442. .notify = hotkey_notify,
  3443. .handle = &hkey_handle,
  3444. .type = ACPI_DEVICE_NOTIFY,
  3445. };
  3446. static struct ibm_struct hotkey_driver_data = {
  3447. .name = "hotkey",
  3448. .read = hotkey_read,
  3449. .write = hotkey_write,
  3450. .exit = hotkey_exit,
  3451. .resume = hotkey_resume,
  3452. .suspend = hotkey_suspend,
  3453. .acpi = &ibm_hotkey_acpidriver,
  3454. };
  3455. /*************************************************************************
  3456. * Bluetooth subdriver
  3457. */
  3458. enum {
  3459. /* ACPI GBDC/SBDC bits */
  3460. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  3461. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  3462. TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
  3463. 0 = disable, 1 = enable */
  3464. };
  3465. enum {
  3466. /* ACPI \BLTH commands */
  3467. TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
  3468. TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
  3469. TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
  3470. TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
  3471. TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
  3472. };
  3473. #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
  3474. static int bluetooth_get_status(void)
  3475. {
  3476. int status;
  3477. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3478. if (dbg_bluetoothemul)
  3479. return (tpacpi_bluetooth_emulstate) ?
  3480. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3481. #endif
  3482. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  3483. return -EIO;
  3484. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
  3485. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3486. }
  3487. static int bluetooth_set_status(enum tpacpi_rfkill_state state)
  3488. {
  3489. int status;
  3490. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s bluetooth\n",
  3491. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  3492. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3493. if (dbg_bluetoothemul) {
  3494. tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3495. return 0;
  3496. }
  3497. #endif
  3498. if (state == TPACPI_RFK_RADIO_ON)
  3499. status = TP_ACPI_BLUETOOTH_RADIOSSW
  3500. | TP_ACPI_BLUETOOTH_RESUMECTRL;
  3501. else
  3502. status = 0;
  3503. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  3504. return -EIO;
  3505. return 0;
  3506. }
  3507. /* sysfs bluetooth enable ---------------------------------------------- */
  3508. static ssize_t bluetooth_enable_show(struct device *dev,
  3509. struct device_attribute *attr,
  3510. char *buf)
  3511. {
  3512. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
  3513. attr, buf);
  3514. }
  3515. static ssize_t bluetooth_enable_store(struct device *dev,
  3516. struct device_attribute *attr,
  3517. const char *buf, size_t count)
  3518. {
  3519. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
  3520. attr, buf, count);
  3521. }
  3522. static DEVICE_ATTR_RW(bluetooth_enable);
  3523. /* --------------------------------------------------------------------- */
  3524. static struct attribute *bluetooth_attributes[] = {
  3525. &dev_attr_bluetooth_enable.attr,
  3526. NULL
  3527. };
  3528. static umode_t bluetooth_attr_is_visible(struct kobject *kobj,
  3529. struct attribute *attr, int n)
  3530. {
  3531. return tp_features.bluetooth ? attr->mode : 0;
  3532. }
  3533. static const struct attribute_group bluetooth_attr_group = {
  3534. .is_visible = bluetooth_attr_is_visible,
  3535. .attrs = bluetooth_attributes,
  3536. };
  3537. static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
  3538. .get_status = bluetooth_get_status,
  3539. .set_status = bluetooth_set_status,
  3540. };
  3541. static void bluetooth_shutdown(void)
  3542. {
  3543. /* Order firmware to save current state to NVRAM */
  3544. if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
  3545. TP_ACPI_BLTH_SAVE_STATE))
  3546. pr_notice("failed to save bluetooth state to NVRAM\n");
  3547. else
  3548. vdbg_printk(TPACPI_DBG_RFKILL,
  3549. "bluetooth state saved to NVRAM\n");
  3550. }
  3551. static void bluetooth_exit(void)
  3552. {
  3553. tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
  3554. bluetooth_shutdown();
  3555. }
  3556. static const struct dmi_system_id fwbug_list[] __initconst = {
  3557. {
  3558. .ident = "ThinkPad E485",
  3559. .driver_data = &quirk_btusb_bug,
  3560. .matches = {
  3561. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3562. DMI_MATCH(DMI_BOARD_NAME, "20KU"),
  3563. },
  3564. },
  3565. {
  3566. .ident = "ThinkPad E585",
  3567. .driver_data = &quirk_btusb_bug,
  3568. .matches = {
  3569. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3570. DMI_MATCH(DMI_BOARD_NAME, "20KV"),
  3571. },
  3572. },
  3573. {
  3574. .ident = "ThinkPad A285 - 20MW",
  3575. .driver_data = &quirk_btusb_bug,
  3576. .matches = {
  3577. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3578. DMI_MATCH(DMI_BOARD_NAME, "20MW"),
  3579. },
  3580. },
  3581. {
  3582. .ident = "ThinkPad A285 - 20MX",
  3583. .driver_data = &quirk_btusb_bug,
  3584. .matches = {
  3585. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3586. DMI_MATCH(DMI_BOARD_NAME, "20MX"),
  3587. },
  3588. },
  3589. {
  3590. .ident = "ThinkPad A485 - 20MU",
  3591. .driver_data = &quirk_btusb_bug,
  3592. .matches = {
  3593. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3594. DMI_MATCH(DMI_BOARD_NAME, "20MU"),
  3595. },
  3596. },
  3597. {
  3598. .ident = "ThinkPad A485 - 20MV",
  3599. .driver_data = &quirk_btusb_bug,
  3600. .matches = {
  3601. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  3602. DMI_MATCH(DMI_BOARD_NAME, "20MV"),
  3603. },
  3604. },
  3605. {}
  3606. };
  3607. static const struct pci_device_id fwbug_cards_ids[] __initconst = {
  3608. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
  3609. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
  3610. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
  3611. {}
  3612. };
  3613. static int __init have_bt_fwbug(void)
  3614. {
  3615. /*
  3616. * Some AMD based ThinkPads have a firmware bug that calling
  3617. * "GBDC" will cause bluetooth on Intel wireless cards blocked
  3618. */
  3619. if (tp_features.quirks && tp_features.quirks->btusb_bug &&
  3620. pci_dev_present(fwbug_cards_ids)) {
  3621. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3622. FW_BUG "disable bluetooth subdriver for Intel cards\n");
  3623. return 1;
  3624. } else
  3625. return 0;
  3626. }
  3627. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  3628. {
  3629. int res;
  3630. int status = 0;
  3631. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3632. "initializing bluetooth subdriver\n");
  3633. TPACPI_ACPIHANDLE_INIT(hkey);
  3634. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3635. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  3636. tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
  3637. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  3638. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3639. "bluetooth is %s, status 0x%02x\n",
  3640. str_supported(tp_features.bluetooth),
  3641. status);
  3642. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3643. if (dbg_bluetoothemul) {
  3644. tp_features.bluetooth = 1;
  3645. pr_info("bluetooth switch emulation enabled\n");
  3646. } else
  3647. #endif
  3648. if (tp_features.bluetooth &&
  3649. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  3650. /* no bluetooth hardware present in system */
  3651. tp_features.bluetooth = 0;
  3652. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3653. "bluetooth hardware not installed\n");
  3654. }
  3655. if (!tp_features.bluetooth)
  3656. return -ENODEV;
  3657. res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
  3658. &bluetooth_tprfk_ops,
  3659. RFKILL_TYPE_BLUETOOTH,
  3660. TPACPI_RFK_BLUETOOTH_SW_NAME,
  3661. true);
  3662. return res;
  3663. }
  3664. /* procfs -------------------------------------------------------------- */
  3665. static int bluetooth_read(struct seq_file *m)
  3666. {
  3667. return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
  3668. }
  3669. static int bluetooth_write(char *buf)
  3670. {
  3671. return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
  3672. }
  3673. static struct ibm_struct bluetooth_driver_data = {
  3674. .name = "bluetooth",
  3675. .read = bluetooth_read,
  3676. .write = bluetooth_write,
  3677. .exit = bluetooth_exit,
  3678. .shutdown = bluetooth_shutdown,
  3679. };
  3680. /*************************************************************************
  3681. * Wan subdriver
  3682. */
  3683. enum {
  3684. /* ACPI GWAN/SWAN bits */
  3685. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  3686. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  3687. TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
  3688. 0 = disable, 1 = enable */
  3689. };
  3690. #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
  3691. static int wan_get_status(void)
  3692. {
  3693. int status;
  3694. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3695. if (dbg_wwanemul)
  3696. return (tpacpi_wwan_emulstate) ?
  3697. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3698. #endif
  3699. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  3700. return -EIO;
  3701. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
  3702. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3703. }
  3704. static int wan_set_status(enum tpacpi_rfkill_state state)
  3705. {
  3706. int status;
  3707. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n",
  3708. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  3709. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3710. if (dbg_wwanemul) {
  3711. tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3712. return 0;
  3713. }
  3714. #endif
  3715. if (state == TPACPI_RFK_RADIO_ON)
  3716. status = TP_ACPI_WANCARD_RADIOSSW
  3717. | TP_ACPI_WANCARD_RESUMECTRL;
  3718. else
  3719. status = 0;
  3720. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  3721. return -EIO;
  3722. return 0;
  3723. }
  3724. /* sysfs wan enable ---------------------------------------------------- */
  3725. static ssize_t wan_enable_show(struct device *dev,
  3726. struct device_attribute *attr,
  3727. char *buf)
  3728. {
  3729. return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
  3730. attr, buf);
  3731. }
  3732. static ssize_t wan_enable_store(struct device *dev,
  3733. struct device_attribute *attr,
  3734. const char *buf, size_t count)
  3735. {
  3736. return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
  3737. attr, buf, count);
  3738. }
  3739. static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  3740. wan_enable_show, wan_enable_store);
  3741. /* --------------------------------------------------------------------- */
  3742. static struct attribute *wan_attributes[] = {
  3743. &dev_attr_wwan_enable.attr,
  3744. NULL
  3745. };
  3746. static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  3747. int n)
  3748. {
  3749. return tp_features.wan ? attr->mode : 0;
  3750. }
  3751. static const struct attribute_group wan_attr_group = {
  3752. .is_visible = wan_attr_is_visible,
  3753. .attrs = wan_attributes,
  3754. };
  3755. static const struct tpacpi_rfk_ops wan_tprfk_ops = {
  3756. .get_status = wan_get_status,
  3757. .set_status = wan_set_status,
  3758. };
  3759. static void wan_shutdown(void)
  3760. {
  3761. /* Order firmware to save current state to NVRAM */
  3762. if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
  3763. TP_ACPI_WGSV_SAVE_STATE))
  3764. pr_notice("failed to save WWAN state to NVRAM\n");
  3765. else
  3766. vdbg_printk(TPACPI_DBG_RFKILL,
  3767. "WWAN state saved to NVRAM\n");
  3768. }
  3769. static void wan_exit(void)
  3770. {
  3771. tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
  3772. wan_shutdown();
  3773. }
  3774. static int __init wan_init(struct ibm_init_struct *iibm)
  3775. {
  3776. int res;
  3777. int status = 0;
  3778. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3779. "initializing wan subdriver\n");
  3780. TPACPI_ACPIHANDLE_INIT(hkey);
  3781. tp_features.wan = hkey_handle &&
  3782. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  3783. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3784. "wan is %s, status 0x%02x\n",
  3785. str_supported(tp_features.wan),
  3786. status);
  3787. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3788. if (dbg_wwanemul) {
  3789. tp_features.wan = 1;
  3790. pr_info("wwan switch emulation enabled\n");
  3791. } else
  3792. #endif
  3793. if (tp_features.wan &&
  3794. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  3795. /* no wan hardware present in system */
  3796. tp_features.wan = 0;
  3797. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3798. "wan hardware not installed\n");
  3799. }
  3800. if (!tp_features.wan)
  3801. return -ENODEV;
  3802. res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
  3803. &wan_tprfk_ops,
  3804. RFKILL_TYPE_WWAN,
  3805. TPACPI_RFK_WWAN_SW_NAME,
  3806. true);
  3807. return res;
  3808. }
  3809. /* procfs -------------------------------------------------------------- */
  3810. static int wan_read(struct seq_file *m)
  3811. {
  3812. return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
  3813. }
  3814. static int wan_write(char *buf)
  3815. {
  3816. return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
  3817. }
  3818. static struct ibm_struct wan_driver_data = {
  3819. .name = "wan",
  3820. .read = wan_read,
  3821. .write = wan_write,
  3822. .exit = wan_exit,
  3823. .shutdown = wan_shutdown,
  3824. };
  3825. /*************************************************************************
  3826. * UWB subdriver
  3827. */
  3828. enum {
  3829. /* ACPI GUWB/SUWB bits */
  3830. TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
  3831. TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
  3832. };
  3833. #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
  3834. static int uwb_get_status(void)
  3835. {
  3836. int status;
  3837. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3838. if (dbg_uwbemul)
  3839. return (tpacpi_uwb_emulstate) ?
  3840. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3841. #endif
  3842. if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
  3843. return -EIO;
  3844. return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
  3845. TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
  3846. }
  3847. static int uwb_set_status(enum tpacpi_rfkill_state state)
  3848. {
  3849. int status;
  3850. vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n",
  3851. str_enable_disable(state == TPACPI_RFK_RADIO_ON));
  3852. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3853. if (dbg_uwbemul) {
  3854. tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
  3855. return 0;
  3856. }
  3857. #endif
  3858. if (state == TPACPI_RFK_RADIO_ON)
  3859. status = TP_ACPI_UWB_RADIOSSW;
  3860. else
  3861. status = 0;
  3862. if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
  3863. return -EIO;
  3864. return 0;
  3865. }
  3866. /* --------------------------------------------------------------------- */
  3867. static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
  3868. .get_status = uwb_get_status,
  3869. .set_status = uwb_set_status,
  3870. };
  3871. static void uwb_exit(void)
  3872. {
  3873. tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
  3874. }
  3875. static int __init uwb_init(struct ibm_init_struct *iibm)
  3876. {
  3877. int res;
  3878. int status = 0;
  3879. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3880. "initializing uwb subdriver\n");
  3881. TPACPI_ACPIHANDLE_INIT(hkey);
  3882. tp_features.uwb = hkey_handle &&
  3883. acpi_evalf(hkey_handle, &status, "GUWB", "qd");
  3884. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
  3885. "uwb is %s, status 0x%02x\n",
  3886. str_supported(tp_features.uwb),
  3887. status);
  3888. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  3889. if (dbg_uwbemul) {
  3890. tp_features.uwb = 1;
  3891. pr_info("uwb switch emulation enabled\n");
  3892. } else
  3893. #endif
  3894. if (tp_features.uwb &&
  3895. !(status & TP_ACPI_UWB_HWPRESENT)) {
  3896. /* no uwb hardware present in system */
  3897. tp_features.uwb = 0;
  3898. dbg_printk(TPACPI_DBG_INIT,
  3899. "uwb hardware not installed\n");
  3900. }
  3901. if (!tp_features.uwb)
  3902. return -ENODEV;
  3903. res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
  3904. &uwb_tprfk_ops,
  3905. RFKILL_TYPE_UWB,
  3906. TPACPI_RFK_UWB_SW_NAME,
  3907. false);
  3908. return res;
  3909. }
  3910. static struct ibm_struct uwb_driver_data = {
  3911. .name = "uwb",
  3912. .exit = uwb_exit,
  3913. .flags.experimental = 1,
  3914. };
  3915. /*************************************************************************
  3916. * Video subdriver
  3917. */
  3918. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  3919. enum video_access_mode {
  3920. TPACPI_VIDEO_NONE = 0,
  3921. TPACPI_VIDEO_570, /* 570 */
  3922. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  3923. TPACPI_VIDEO_NEW, /* all others */
  3924. };
  3925. enum { /* video status flags, based on VIDEO_570 */
  3926. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  3927. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  3928. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  3929. };
  3930. enum { /* TPACPI_VIDEO_570 constants */
  3931. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  3932. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  3933. * video_status_flags */
  3934. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  3935. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  3936. };
  3937. static enum video_access_mode video_supported;
  3938. static int video_orig_autosw;
  3939. static int video_autosw_get(void);
  3940. static int video_autosw_set(int enable);
  3941. TPACPI_HANDLE(vid, root,
  3942. "\\_SB.PCI.AGP.VGA", /* 570 */
  3943. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  3944. "\\_SB.PCI0.VID0", /* 770e */
  3945. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  3946. "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
  3947. "\\_SB.PCI0.AGP.VID", /* all others */
  3948. ); /* R30, R31 */
  3949. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  3950. static int __init video_init(struct ibm_init_struct *iibm)
  3951. {
  3952. int ivga;
  3953. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  3954. TPACPI_ACPIHANDLE_INIT(vid);
  3955. if (tpacpi_is_ibm())
  3956. TPACPI_ACPIHANDLE_INIT(vid2);
  3957. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  3958. /* G41, assume IVGA doesn't change */
  3959. vid_handle = vid2_handle;
  3960. if (!vid_handle)
  3961. /* video switching not supported on R30, R31 */
  3962. video_supported = TPACPI_VIDEO_NONE;
  3963. else if (tpacpi_is_ibm() &&
  3964. acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  3965. /* 570 */
  3966. video_supported = TPACPI_VIDEO_570;
  3967. else if (tpacpi_is_ibm() &&
  3968. acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  3969. /* 600e/x, 770e, 770x */
  3970. video_supported = TPACPI_VIDEO_770;
  3971. else
  3972. /* all others */
  3973. video_supported = TPACPI_VIDEO_NEW;
  3974. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  3975. str_supported(video_supported != TPACPI_VIDEO_NONE),
  3976. video_supported);
  3977. return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV;
  3978. }
  3979. static void video_exit(void)
  3980. {
  3981. dbg_printk(TPACPI_DBG_EXIT,
  3982. "restoring original video autoswitch mode\n");
  3983. if (video_autosw_set(video_orig_autosw))
  3984. pr_err("error while trying to restore original video autoswitch mode\n");
  3985. }
  3986. static int video_outputsw_get(void)
  3987. {
  3988. int status = 0;
  3989. int i;
  3990. switch (video_supported) {
  3991. case TPACPI_VIDEO_570:
  3992. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  3993. TP_ACPI_VIDEO_570_PHSCMD))
  3994. return -EIO;
  3995. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  3996. break;
  3997. case TPACPI_VIDEO_770:
  3998. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  3999. return -EIO;
  4000. if (i)
  4001. status |= TP_ACPI_VIDEO_S_LCD;
  4002. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  4003. return -EIO;
  4004. if (i)
  4005. status |= TP_ACPI_VIDEO_S_CRT;
  4006. break;
  4007. case TPACPI_VIDEO_NEW:
  4008. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  4009. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  4010. return -EIO;
  4011. if (i)
  4012. status |= TP_ACPI_VIDEO_S_CRT;
  4013. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  4014. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  4015. return -EIO;
  4016. if (i)
  4017. status |= TP_ACPI_VIDEO_S_LCD;
  4018. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  4019. return -EIO;
  4020. if (i)
  4021. status |= TP_ACPI_VIDEO_S_DVI;
  4022. break;
  4023. default:
  4024. return -ENOSYS;
  4025. }
  4026. return status;
  4027. }
  4028. static int video_outputsw_set(int status)
  4029. {
  4030. int autosw;
  4031. int res = 0;
  4032. switch (video_supported) {
  4033. case TPACPI_VIDEO_570:
  4034. res = acpi_evalf(NULL, NULL,
  4035. "\\_SB.PHS2", "vdd",
  4036. TP_ACPI_VIDEO_570_PHS2CMD,
  4037. status | TP_ACPI_VIDEO_570_PHS2SET);
  4038. break;
  4039. case TPACPI_VIDEO_770:
  4040. autosw = video_autosw_get();
  4041. if (autosw < 0)
  4042. return autosw;
  4043. res = video_autosw_set(1);
  4044. if (res)
  4045. return res;
  4046. res = acpi_evalf(vid_handle, NULL,
  4047. "ASWT", "vdd", status * 0x100, 0);
  4048. if (!autosw && video_autosw_set(autosw)) {
  4049. pr_err("video auto-switch left enabled due to error\n");
  4050. return -EIO;
  4051. }
  4052. break;
  4053. case TPACPI_VIDEO_NEW:
  4054. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  4055. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  4056. break;
  4057. default:
  4058. return -ENOSYS;
  4059. }
  4060. return (res) ? 0 : -EIO;
  4061. }
  4062. static int video_autosw_get(void)
  4063. {
  4064. int autosw = 0;
  4065. switch (video_supported) {
  4066. case TPACPI_VIDEO_570:
  4067. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  4068. return -EIO;
  4069. break;
  4070. case TPACPI_VIDEO_770:
  4071. case TPACPI_VIDEO_NEW:
  4072. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  4073. return -EIO;
  4074. break;
  4075. default:
  4076. return -ENOSYS;
  4077. }
  4078. return autosw & 1;
  4079. }
  4080. static int video_autosw_set(int enable)
  4081. {
  4082. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
  4083. return -EIO;
  4084. return 0;
  4085. }
  4086. static int video_outputsw_cycle(void)
  4087. {
  4088. int autosw = video_autosw_get();
  4089. int res;
  4090. if (autosw < 0)
  4091. return autosw;
  4092. switch (video_supported) {
  4093. case TPACPI_VIDEO_570:
  4094. res = video_autosw_set(1);
  4095. if (res)
  4096. return res;
  4097. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  4098. break;
  4099. case TPACPI_VIDEO_770:
  4100. case TPACPI_VIDEO_NEW:
  4101. res = video_autosw_set(1);
  4102. if (res)
  4103. return res;
  4104. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  4105. break;
  4106. default:
  4107. return -ENOSYS;
  4108. }
  4109. if (!autosw && video_autosw_set(autosw)) {
  4110. pr_err("video auto-switch left enabled due to error\n");
  4111. return -EIO;
  4112. }
  4113. return (res) ? 0 : -EIO;
  4114. }
  4115. static int video_expand_toggle(void)
  4116. {
  4117. switch (video_supported) {
  4118. case TPACPI_VIDEO_570:
  4119. return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
  4120. 0 : -EIO;
  4121. case TPACPI_VIDEO_770:
  4122. return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
  4123. 0 : -EIO;
  4124. case TPACPI_VIDEO_NEW:
  4125. return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
  4126. 0 : -EIO;
  4127. default:
  4128. return -ENOSYS;
  4129. }
  4130. /* not reached */
  4131. }
  4132. static int video_read(struct seq_file *m)
  4133. {
  4134. int status, autosw;
  4135. if (video_supported == TPACPI_VIDEO_NONE) {
  4136. seq_printf(m, "status:\t\tnot supported\n");
  4137. return 0;
  4138. }
  4139. /* Even reads can crash X.org, so... */
  4140. if (!capable(CAP_SYS_ADMIN))
  4141. return -EPERM;
  4142. status = video_outputsw_get();
  4143. if (status < 0)
  4144. return status;
  4145. autosw = video_autosw_get();
  4146. if (autosw < 0)
  4147. return autosw;
  4148. seq_printf(m, "status:\t\tsupported\n");
  4149. seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0)));
  4150. seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1)));
  4151. if (video_supported == TPACPI_VIDEO_NEW)
  4152. seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3)));
  4153. seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0)));
  4154. seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
  4155. seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
  4156. if (video_supported == TPACPI_VIDEO_NEW)
  4157. seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
  4158. seq_printf(m, "commands:\tauto_enable, auto_disable\n");
  4159. seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
  4160. return 0;
  4161. }
  4162. static int video_write(char *buf)
  4163. {
  4164. char *cmd;
  4165. int enable, disable, status;
  4166. int res;
  4167. if (video_supported == TPACPI_VIDEO_NONE)
  4168. return -ENODEV;
  4169. /* Even reads can crash X.org, let alone writes... */
  4170. if (!capable(CAP_SYS_ADMIN))
  4171. return -EPERM;
  4172. enable = 0;
  4173. disable = 0;
  4174. while ((cmd = strsep(&buf, ","))) {
  4175. if (strstarts(cmd, "lcd_enable")) {
  4176. enable |= TP_ACPI_VIDEO_S_LCD;
  4177. } else if (strstarts(cmd, "lcd_disable")) {
  4178. disable |= TP_ACPI_VIDEO_S_LCD;
  4179. } else if (strstarts(cmd, "crt_enable")) {
  4180. enable |= TP_ACPI_VIDEO_S_CRT;
  4181. } else if (strstarts(cmd, "crt_disable")) {
  4182. disable |= TP_ACPI_VIDEO_S_CRT;
  4183. } else if (video_supported == TPACPI_VIDEO_NEW &&
  4184. strstarts(cmd, "dvi_enable")) {
  4185. enable |= TP_ACPI_VIDEO_S_DVI;
  4186. } else if (video_supported == TPACPI_VIDEO_NEW &&
  4187. strstarts(cmd, "dvi_disable")) {
  4188. disable |= TP_ACPI_VIDEO_S_DVI;
  4189. } else if (strstarts(cmd, "auto_enable")) {
  4190. res = video_autosw_set(1);
  4191. if (res)
  4192. return res;
  4193. } else if (strstarts(cmd, "auto_disable")) {
  4194. res = video_autosw_set(0);
  4195. if (res)
  4196. return res;
  4197. } else if (strstarts(cmd, "video_switch")) {
  4198. res = video_outputsw_cycle();
  4199. if (res)
  4200. return res;
  4201. } else if (strstarts(cmd, "expand_toggle")) {
  4202. res = video_expand_toggle();
  4203. if (res)
  4204. return res;
  4205. } else
  4206. return -EINVAL;
  4207. }
  4208. if (enable || disable) {
  4209. status = video_outputsw_get();
  4210. if (status < 0)
  4211. return status;
  4212. res = video_outputsw_set((status & ~disable) | enable);
  4213. if (res)
  4214. return res;
  4215. }
  4216. return 0;
  4217. }
  4218. static struct ibm_struct video_driver_data = {
  4219. .name = "video",
  4220. .read = video_read,
  4221. .write = video_write,
  4222. .exit = video_exit,
  4223. };
  4224. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  4225. /*************************************************************************
  4226. * Keyboard backlight subdriver
  4227. */
  4228. static enum led_brightness kbdlight_brightness;
  4229. static DEFINE_MUTEX(kbdlight_mutex);
  4230. static int kbdlight_set_level(int level)
  4231. {
  4232. int ret = 0;
  4233. if (!hkey_handle)
  4234. return -ENXIO;
  4235. mutex_lock(&kbdlight_mutex);
  4236. if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
  4237. ret = -EIO;
  4238. else
  4239. kbdlight_brightness = level;
  4240. mutex_unlock(&kbdlight_mutex);
  4241. return ret;
  4242. }
  4243. static int kbdlight_get_level(void)
  4244. {
  4245. int status = 0;
  4246. if (!hkey_handle)
  4247. return -ENXIO;
  4248. if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
  4249. return -EIO;
  4250. if (status < 0)
  4251. return status;
  4252. return status & 0x3;
  4253. }
  4254. static bool kbdlight_is_supported(void)
  4255. {
  4256. int status = 0;
  4257. if (!hkey_handle)
  4258. return false;
  4259. if (!acpi_has_method(hkey_handle, "MLCG")) {
  4260. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
  4261. return false;
  4262. }
  4263. if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
  4264. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
  4265. return false;
  4266. }
  4267. if (status < 0) {
  4268. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
  4269. return false;
  4270. }
  4271. vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
  4272. /*
  4273. * Guessed test for keyboard backlight:
  4274. *
  4275. * Machines with backlight keyboard return:
  4276. * b010100000010000000XX - ThinkPad X1 Carbon 3rd
  4277. * b110100010010000000XX - ThinkPad x230
  4278. * b010100000010000000XX - ThinkPad x240
  4279. * b010100000010000000XX - ThinkPad W541
  4280. * (XX is current backlight level)
  4281. *
  4282. * Machines without backlight keyboard return:
  4283. * b10100001000000000000 - ThinkPad x230
  4284. * b10110001000000000000 - ThinkPad E430
  4285. * b00000000000000000000 - ThinkPad E450
  4286. *
  4287. * Candidate BITs for detection test (XOR):
  4288. * b01000000001000000000
  4289. * ^
  4290. */
  4291. return status & BIT(9);
  4292. }
  4293. static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
  4294. enum led_brightness brightness)
  4295. {
  4296. return kbdlight_set_level(brightness);
  4297. }
  4298. static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
  4299. {
  4300. int level;
  4301. level = kbdlight_get_level();
  4302. if (level < 0)
  4303. return 0;
  4304. return level;
  4305. }
  4306. static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
  4307. .led_classdev = {
  4308. .name = "tpacpi::kbd_backlight",
  4309. .max_brightness = 2,
  4310. .flags = LED_BRIGHT_HW_CHANGED,
  4311. .brightness_set_blocking = &kbdlight_sysfs_set,
  4312. .brightness_get = &kbdlight_sysfs_get,
  4313. }
  4314. };
  4315. static int __init kbdlight_init(struct ibm_init_struct *iibm)
  4316. {
  4317. int rc;
  4318. vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
  4319. TPACPI_ACPIHANDLE_INIT(hkey);
  4320. if (!kbdlight_is_supported()) {
  4321. tp_features.kbdlight = 0;
  4322. vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
  4323. return -ENODEV;
  4324. }
  4325. kbdlight_brightness = kbdlight_sysfs_get(NULL);
  4326. tp_features.kbdlight = 1;
  4327. rc = led_classdev_register(&tpacpi_pdev->dev,
  4328. &tpacpi_led_kbdlight.led_classdev);
  4329. if (rc < 0) {
  4330. tp_features.kbdlight = 0;
  4331. return rc;
  4332. }
  4333. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
  4334. TP_ACPI_HKEY_KBD_LIGHT_MASK);
  4335. return 0;
  4336. }
  4337. static void kbdlight_exit(void)
  4338. {
  4339. led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
  4340. }
  4341. static int kbdlight_set_level_and_update(int level)
  4342. {
  4343. int ret;
  4344. struct led_classdev *led_cdev;
  4345. ret = kbdlight_set_level(level);
  4346. led_cdev = &tpacpi_led_kbdlight.led_classdev;
  4347. if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
  4348. led_cdev->brightness = level;
  4349. return ret;
  4350. }
  4351. static int kbdlight_read(struct seq_file *m)
  4352. {
  4353. int level;
  4354. if (!tp_features.kbdlight) {
  4355. seq_printf(m, "status:\t\tnot supported\n");
  4356. } else {
  4357. level = kbdlight_get_level();
  4358. if (level < 0)
  4359. seq_printf(m, "status:\t\terror %d\n", level);
  4360. else
  4361. seq_printf(m, "status:\t\t%d\n", level);
  4362. seq_printf(m, "commands:\t0, 1, 2\n");
  4363. }
  4364. return 0;
  4365. }
  4366. static int kbdlight_write(char *buf)
  4367. {
  4368. char *cmd;
  4369. int res, level = -EINVAL;
  4370. if (!tp_features.kbdlight)
  4371. return -ENODEV;
  4372. while ((cmd = strsep(&buf, ","))) {
  4373. res = kstrtoint(cmd, 10, &level);
  4374. if (res < 0)
  4375. return res;
  4376. }
  4377. if (level >= 3 || level < 0)
  4378. return -EINVAL;
  4379. return kbdlight_set_level_and_update(level);
  4380. }
  4381. static void kbdlight_suspend(void)
  4382. {
  4383. struct led_classdev *led_cdev;
  4384. if (!tp_features.kbdlight)
  4385. return;
  4386. led_cdev = &tpacpi_led_kbdlight.led_classdev;
  4387. led_update_brightness(led_cdev);
  4388. led_classdev_suspend(led_cdev);
  4389. }
  4390. static void kbdlight_resume(void)
  4391. {
  4392. if (!tp_features.kbdlight)
  4393. return;
  4394. led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
  4395. }
  4396. static struct ibm_struct kbdlight_driver_data = {
  4397. .name = "kbdlight",
  4398. .read = kbdlight_read,
  4399. .write = kbdlight_write,
  4400. .suspend = kbdlight_suspend,
  4401. .resume = kbdlight_resume,
  4402. .exit = kbdlight_exit,
  4403. };
  4404. /*************************************************************************
  4405. * Light (thinklight) subdriver
  4406. */
  4407. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  4408. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  4409. static int light_get_status(void)
  4410. {
  4411. int status = 0;
  4412. if (tp_features.light_status) {
  4413. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  4414. return -EIO;
  4415. return (!!status);
  4416. }
  4417. return -ENXIO;
  4418. }
  4419. static int light_set_status(int status)
  4420. {
  4421. int rc;
  4422. if (tp_features.light) {
  4423. if (cmos_handle) {
  4424. rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
  4425. (status) ?
  4426. TP_CMOS_THINKLIGHT_ON :
  4427. TP_CMOS_THINKLIGHT_OFF);
  4428. } else {
  4429. rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
  4430. (status) ? 1 : 0);
  4431. }
  4432. return (rc) ? 0 : -EIO;
  4433. }
  4434. return -ENXIO;
  4435. }
  4436. static int light_sysfs_set(struct led_classdev *led_cdev,
  4437. enum led_brightness brightness)
  4438. {
  4439. return light_set_status((brightness != LED_OFF) ?
  4440. TPACPI_LED_ON : TPACPI_LED_OFF);
  4441. }
  4442. static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
  4443. {
  4444. return (light_get_status() == 1) ? LED_ON : LED_OFF;
  4445. }
  4446. static struct tpacpi_led_classdev tpacpi_led_thinklight = {
  4447. .led_classdev = {
  4448. .name = "tpacpi::thinklight",
  4449. .max_brightness = 1,
  4450. .brightness_set_blocking = &light_sysfs_set,
  4451. .brightness_get = &light_sysfs_get,
  4452. }
  4453. };
  4454. static int __init light_init(struct ibm_init_struct *iibm)
  4455. {
  4456. int rc;
  4457. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  4458. if (tpacpi_is_ibm()) {
  4459. TPACPI_ACPIHANDLE_INIT(ledb);
  4460. TPACPI_ACPIHANDLE_INIT(lght);
  4461. }
  4462. TPACPI_ACPIHANDLE_INIT(cmos);
  4463. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  4464. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  4465. if (tp_features.light)
  4466. /* light status not supported on
  4467. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  4468. tp_features.light_status =
  4469. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  4470. vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
  4471. str_supported(tp_features.light),
  4472. str_supported(tp_features.light_status));
  4473. if (!tp_features.light)
  4474. return -ENODEV;
  4475. rc = led_classdev_register(&tpacpi_pdev->dev,
  4476. &tpacpi_led_thinklight.led_classdev);
  4477. if (rc < 0) {
  4478. tp_features.light = 0;
  4479. tp_features.light_status = 0;
  4480. } else {
  4481. rc = 0;
  4482. }
  4483. return rc;
  4484. }
  4485. static void light_exit(void)
  4486. {
  4487. led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
  4488. }
  4489. static int light_read(struct seq_file *m)
  4490. {
  4491. int status;
  4492. if (!tp_features.light) {
  4493. seq_printf(m, "status:\t\tnot supported\n");
  4494. } else if (!tp_features.light_status) {
  4495. seq_printf(m, "status:\t\tunknown\n");
  4496. seq_printf(m, "commands:\ton, off\n");
  4497. } else {
  4498. status = light_get_status();
  4499. if (status < 0)
  4500. return status;
  4501. seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0)));
  4502. seq_printf(m, "commands:\ton, off\n");
  4503. }
  4504. return 0;
  4505. }
  4506. static int light_write(char *buf)
  4507. {
  4508. char *cmd;
  4509. int newstatus = 0;
  4510. if (!tp_features.light)
  4511. return -ENODEV;
  4512. while ((cmd = strsep(&buf, ","))) {
  4513. if (strstarts(cmd, "on")) {
  4514. newstatus = 1;
  4515. } else if (strstarts(cmd, "off")) {
  4516. newstatus = 0;
  4517. } else
  4518. return -EINVAL;
  4519. }
  4520. return light_set_status(newstatus);
  4521. }
  4522. static struct ibm_struct light_driver_data = {
  4523. .name = "light",
  4524. .read = light_read,
  4525. .write = light_write,
  4526. .exit = light_exit,
  4527. };
  4528. /*************************************************************************
  4529. * CMOS subdriver
  4530. */
  4531. /* sysfs cmos_command -------------------------------------------------- */
  4532. static ssize_t cmos_command_store(struct device *dev,
  4533. struct device_attribute *attr,
  4534. const char *buf, size_t count)
  4535. {
  4536. unsigned long cmos_cmd;
  4537. int res;
  4538. if (parse_strtoul(buf, 21, &cmos_cmd))
  4539. return -EINVAL;
  4540. res = issue_thinkpad_cmos_command(cmos_cmd);
  4541. return (res) ? res : count;
  4542. }
  4543. static DEVICE_ATTR_WO(cmos_command);
  4544. static struct attribute *cmos_attributes[] = {
  4545. &dev_attr_cmos_command.attr,
  4546. NULL
  4547. };
  4548. static umode_t cmos_attr_is_visible(struct kobject *kobj,
  4549. struct attribute *attr, int n)
  4550. {
  4551. return cmos_handle ? attr->mode : 0;
  4552. }
  4553. static const struct attribute_group cmos_attr_group = {
  4554. .is_visible = cmos_attr_is_visible,
  4555. .attrs = cmos_attributes,
  4556. };
  4557. /* --------------------------------------------------------------------- */
  4558. static int __init cmos_init(struct ibm_init_struct *iibm)
  4559. {
  4560. vdbg_printk(TPACPI_DBG_INIT,
  4561. "initializing cmos commands subdriver\n");
  4562. TPACPI_ACPIHANDLE_INIT(cmos);
  4563. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  4564. str_supported(cmos_handle != NULL));
  4565. return cmos_handle ? 0 : -ENODEV;
  4566. }
  4567. static int cmos_read(struct seq_file *m)
  4568. {
  4569. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  4570. R30, R31, T20-22, X20-21 */
  4571. if (!cmos_handle)
  4572. seq_printf(m, "status:\t\tnot supported\n");
  4573. else {
  4574. seq_printf(m, "status:\t\tsupported\n");
  4575. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
  4576. }
  4577. return 0;
  4578. }
  4579. static int cmos_write(char *buf)
  4580. {
  4581. char *cmd;
  4582. int cmos_cmd, res;
  4583. while ((cmd = strsep(&buf, ","))) {
  4584. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  4585. cmos_cmd >= 0 && cmos_cmd <= 21) {
  4586. /* cmos_cmd set */
  4587. } else
  4588. return -EINVAL;
  4589. res = issue_thinkpad_cmos_command(cmos_cmd);
  4590. if (res)
  4591. return res;
  4592. }
  4593. return 0;
  4594. }
  4595. static struct ibm_struct cmos_driver_data = {
  4596. .name = "cmos",
  4597. .read = cmos_read,
  4598. .write = cmos_write,
  4599. };
  4600. /*************************************************************************
  4601. * LED subdriver
  4602. */
  4603. enum led_access_mode {
  4604. TPACPI_LED_NONE = 0,
  4605. TPACPI_LED_570, /* 570 */
  4606. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4607. TPACPI_LED_NEW, /* all others */
  4608. };
  4609. enum { /* For TPACPI_LED_OLD */
  4610. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  4611. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  4612. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  4613. };
  4614. static enum led_access_mode led_supported;
  4615. static acpi_handle led_handle;
  4616. #define TPACPI_LED_NUMLEDS 16
  4617. static struct tpacpi_led_classdev *tpacpi_leds;
  4618. static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
  4619. static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
  4620. /* there's a limit of 19 chars + NULL before 2.6.26 */
  4621. "tpacpi::power",
  4622. "tpacpi:orange:batt",
  4623. "tpacpi:green:batt",
  4624. "tpacpi::dock_active",
  4625. "tpacpi::bay_active",
  4626. "tpacpi::dock_batt",
  4627. "tpacpi::unknown_led",
  4628. "tpacpi::standby",
  4629. "tpacpi::dock_status1",
  4630. "tpacpi::dock_status2",
  4631. "tpacpi::lid_logo_dot",
  4632. "tpacpi::unknown_led3",
  4633. "tpacpi::thinkvantage",
  4634. };
  4635. #define TPACPI_SAFE_LEDS 0x1481U
  4636. static inline bool tpacpi_is_led_restricted(const unsigned int led)
  4637. {
  4638. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4639. return false;
  4640. #else
  4641. return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
  4642. #endif
  4643. }
  4644. static int led_get_status(const unsigned int led)
  4645. {
  4646. int status;
  4647. enum led_status_t led_s;
  4648. switch (led_supported) {
  4649. case TPACPI_LED_570:
  4650. if (!acpi_evalf(ec_handle,
  4651. &status, "GLED", "dd", 1 << led))
  4652. return -EIO;
  4653. led_s = (status == 0) ?
  4654. TPACPI_LED_OFF :
  4655. ((status == 1) ?
  4656. TPACPI_LED_ON :
  4657. TPACPI_LED_BLINK);
  4658. tpacpi_led_state_cache[led] = led_s;
  4659. return led_s;
  4660. default:
  4661. return -ENXIO;
  4662. }
  4663. /* not reached */
  4664. }
  4665. static int led_set_status(const unsigned int led,
  4666. const enum led_status_t ledstatus)
  4667. {
  4668. /* off, on, blink. Index is led_status_t */
  4669. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  4670. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  4671. int rc = 0;
  4672. switch (led_supported) {
  4673. case TPACPI_LED_570:
  4674. /* 570 */
  4675. if (unlikely(led > 7))
  4676. return -EINVAL;
  4677. if (unlikely(tpacpi_is_led_restricted(led)))
  4678. return -EPERM;
  4679. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4680. (1 << led), led_sled_arg1[ledstatus]))
  4681. return -EIO;
  4682. break;
  4683. case TPACPI_LED_OLD:
  4684. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  4685. if (unlikely(led > 7))
  4686. return -EINVAL;
  4687. if (unlikely(tpacpi_is_led_restricted(led)))
  4688. return -EPERM;
  4689. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  4690. if (rc >= 0)
  4691. rc = ec_write(TPACPI_LED_EC_HLBL,
  4692. (ledstatus == TPACPI_LED_BLINK) << led);
  4693. if (rc >= 0)
  4694. rc = ec_write(TPACPI_LED_EC_HLCL,
  4695. (ledstatus != TPACPI_LED_OFF) << led);
  4696. break;
  4697. case TPACPI_LED_NEW:
  4698. /* all others */
  4699. if (unlikely(led >= TPACPI_LED_NUMLEDS))
  4700. return -EINVAL;
  4701. if (unlikely(tpacpi_is_led_restricted(led)))
  4702. return -EPERM;
  4703. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  4704. led, led_led_arg1[ledstatus]))
  4705. return -EIO;
  4706. break;
  4707. default:
  4708. return -ENXIO;
  4709. }
  4710. if (!rc)
  4711. tpacpi_led_state_cache[led] = ledstatus;
  4712. return rc;
  4713. }
  4714. static int led_sysfs_set(struct led_classdev *led_cdev,
  4715. enum led_brightness brightness)
  4716. {
  4717. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4718. struct tpacpi_led_classdev, led_classdev);
  4719. enum led_status_t new_state;
  4720. if (brightness == LED_OFF)
  4721. new_state = TPACPI_LED_OFF;
  4722. else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
  4723. new_state = TPACPI_LED_ON;
  4724. else
  4725. new_state = TPACPI_LED_BLINK;
  4726. return led_set_status(data->led, new_state);
  4727. }
  4728. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  4729. unsigned long *delay_on, unsigned long *delay_off)
  4730. {
  4731. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4732. struct tpacpi_led_classdev, led_classdev);
  4733. /* Can we choose the flash rate? */
  4734. if (*delay_on == 0 && *delay_off == 0) {
  4735. /* yes. set them to the hardware blink rate (1 Hz) */
  4736. *delay_on = 500; /* ms */
  4737. *delay_off = 500; /* ms */
  4738. } else if ((*delay_on != 500) || (*delay_off != 500))
  4739. return -EINVAL;
  4740. return led_set_status(data->led, TPACPI_LED_BLINK);
  4741. }
  4742. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  4743. {
  4744. int rc;
  4745. struct tpacpi_led_classdev *data = container_of(led_cdev,
  4746. struct tpacpi_led_classdev, led_classdev);
  4747. rc = led_get_status(data->led);
  4748. if (rc == TPACPI_LED_OFF || rc < 0)
  4749. rc = LED_OFF; /* no error handling in led class :( */
  4750. else
  4751. rc = LED_FULL;
  4752. return rc;
  4753. }
  4754. static void led_exit(void)
  4755. {
  4756. unsigned int i;
  4757. for (i = 0; i < TPACPI_LED_NUMLEDS; i++)
  4758. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  4759. kfree(tpacpi_leds);
  4760. }
  4761. static int __init tpacpi_init_led(unsigned int led)
  4762. {
  4763. /* LEDs with no name don't get registered */
  4764. if (!tpacpi_led_names[led])
  4765. return 0;
  4766. tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
  4767. tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
  4768. if (led_supported == TPACPI_LED_570)
  4769. tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get;
  4770. tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
  4771. tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN;
  4772. tpacpi_leds[led].led = led;
  4773. return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev);
  4774. }
  4775. static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
  4776. TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
  4777. TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
  4778. TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
  4779. TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
  4780. TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
  4781. TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
  4782. TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
  4783. TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
  4784. TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
  4785. TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
  4786. TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
  4787. TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
  4788. TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
  4789. TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
  4790. TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
  4791. TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
  4792. TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
  4793. TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
  4794. TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
  4795. TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
  4796. /* (1) - may have excess leds enabled on MSB */
  4797. /* Defaults (order matters, keep last, don't reorder!) */
  4798. { /* Lenovo */
  4799. .vendor = PCI_VENDOR_ID_LENOVO,
  4800. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4801. .quirks = 0x1fffU,
  4802. },
  4803. { /* IBM ThinkPads with no EC version string */
  4804. .vendor = PCI_VENDOR_ID_IBM,
  4805. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
  4806. .quirks = 0x00ffU,
  4807. },
  4808. { /* IBM ThinkPads with EC version string */
  4809. .vendor = PCI_VENDOR_ID_IBM,
  4810. .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
  4811. .quirks = 0x00bfU,
  4812. },
  4813. };
  4814. static enum led_access_mode __init led_init_detect_mode(void)
  4815. {
  4816. acpi_status status;
  4817. if (tpacpi_is_ibm()) {
  4818. /* 570 */
  4819. status = acpi_get_handle(ec_handle, "SLED", &led_handle);
  4820. if (ACPI_SUCCESS(status))
  4821. return TPACPI_LED_570;
  4822. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  4823. status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
  4824. if (ACPI_SUCCESS(status))
  4825. return TPACPI_LED_OLD;
  4826. }
  4827. /* most others */
  4828. status = acpi_get_handle(ec_handle, "LED", &led_handle);
  4829. if (ACPI_SUCCESS(status))
  4830. return TPACPI_LED_NEW;
  4831. /* R30, R31, and unknown firmwares */
  4832. led_handle = NULL;
  4833. return TPACPI_LED_NONE;
  4834. }
  4835. static int __init led_init(struct ibm_init_struct *iibm)
  4836. {
  4837. unsigned int i;
  4838. int rc;
  4839. unsigned long useful_leds;
  4840. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  4841. led_supported = led_init_detect_mode();
  4842. if (led_supported != TPACPI_LED_NONE) {
  4843. useful_leds = tpacpi_check_quirks(led_useful_qtable,
  4844. ARRAY_SIZE(led_useful_qtable));
  4845. if (!useful_leds) {
  4846. led_handle = NULL;
  4847. led_supported = TPACPI_LED_NONE;
  4848. }
  4849. }
  4850. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  4851. str_supported(led_supported), led_supported);
  4852. if (led_supported == TPACPI_LED_NONE)
  4853. return -ENODEV;
  4854. tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
  4855. GFP_KERNEL);
  4856. if (!tpacpi_leds) {
  4857. pr_err("Out of memory for LED data\n");
  4858. return -ENOMEM;
  4859. }
  4860. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  4861. tpacpi_leds[i].led = -1;
  4862. if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) {
  4863. rc = tpacpi_init_led(i);
  4864. if (rc < 0) {
  4865. led_exit();
  4866. return rc;
  4867. }
  4868. }
  4869. }
  4870. #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
  4871. pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
  4872. #endif
  4873. return 0;
  4874. }
  4875. #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s))
  4876. static int led_read(struct seq_file *m)
  4877. {
  4878. if (!led_supported) {
  4879. seq_printf(m, "status:\t\tnot supported\n");
  4880. return 0;
  4881. }
  4882. seq_printf(m, "status:\t\tsupported\n");
  4883. if (led_supported == TPACPI_LED_570) {
  4884. /* 570 */
  4885. int i, status;
  4886. for (i = 0; i < 8; i++) {
  4887. status = led_get_status(i);
  4888. if (status < 0)
  4889. return -EIO;
  4890. seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status));
  4891. }
  4892. }
  4893. seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
  4894. return 0;
  4895. }
  4896. static int led_write(char *buf)
  4897. {
  4898. char *cmd;
  4899. int led, rc;
  4900. enum led_status_t s;
  4901. if (!led_supported)
  4902. return -ENODEV;
  4903. while ((cmd = strsep(&buf, ","))) {
  4904. if (sscanf(cmd, "%d", &led) != 1)
  4905. return -EINVAL;
  4906. if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1))
  4907. return -ENODEV;
  4908. if (tpacpi_leds[led].led < 0)
  4909. return -ENODEV;
  4910. if (strstr(cmd, "off")) {
  4911. s = TPACPI_LED_OFF;
  4912. } else if (strstr(cmd, "on")) {
  4913. s = TPACPI_LED_ON;
  4914. } else if (strstr(cmd, "blink")) {
  4915. s = TPACPI_LED_BLINK;
  4916. } else {
  4917. return -EINVAL;
  4918. }
  4919. rc = led_set_status(led, s);
  4920. if (rc < 0)
  4921. return rc;
  4922. }
  4923. return 0;
  4924. }
  4925. static struct ibm_struct led_driver_data = {
  4926. .name = "led",
  4927. .read = led_read,
  4928. .write = led_write,
  4929. .exit = led_exit,
  4930. };
  4931. /*************************************************************************
  4932. * Beep subdriver
  4933. */
  4934. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  4935. #define TPACPI_BEEP_Q1 0x0001
  4936. static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
  4937. TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
  4938. TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
  4939. };
  4940. static int __init beep_init(struct ibm_init_struct *iibm)
  4941. {
  4942. unsigned long quirks;
  4943. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  4944. TPACPI_ACPIHANDLE_INIT(beep);
  4945. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  4946. str_supported(beep_handle != NULL));
  4947. quirks = tpacpi_check_quirks(beep_quirk_table,
  4948. ARRAY_SIZE(beep_quirk_table));
  4949. tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
  4950. return (beep_handle) ? 0 : -ENODEV;
  4951. }
  4952. static int beep_read(struct seq_file *m)
  4953. {
  4954. if (!beep_handle)
  4955. seq_printf(m, "status:\t\tnot supported\n");
  4956. else {
  4957. seq_printf(m, "status:\t\tsupported\n");
  4958. seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
  4959. }
  4960. return 0;
  4961. }
  4962. static int beep_write(char *buf)
  4963. {
  4964. char *cmd;
  4965. int beep_cmd;
  4966. if (!beep_handle)
  4967. return -ENODEV;
  4968. while ((cmd = strsep(&buf, ","))) {
  4969. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  4970. beep_cmd >= 0 && beep_cmd <= 17) {
  4971. /* beep_cmd set */
  4972. } else
  4973. return -EINVAL;
  4974. if (tp_features.beep_needs_two_args) {
  4975. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
  4976. beep_cmd, 0))
  4977. return -EIO;
  4978. } else {
  4979. if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
  4980. beep_cmd))
  4981. return -EIO;
  4982. }
  4983. }
  4984. return 0;
  4985. }
  4986. static struct ibm_struct beep_driver_data = {
  4987. .name = "beep",
  4988. .read = beep_read,
  4989. .write = beep_write,
  4990. };
  4991. /*************************************************************************
  4992. * Thermal subdriver
  4993. */
  4994. enum thermal_access_mode {
  4995. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  4996. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  4997. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  4998. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  4999. TPACPI_THERMAL_TPEC_12, /* Use ACPI EC regs, 12 sensors */
  5000. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  5001. };
  5002. enum { /* TPACPI_THERMAL_TPEC_* */
  5003. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  5004. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  5005. TP_EC_THERMAL_TMP0_NS = 0xA8, /* ACPI EC Non-Standard regs TMP 0..7 */
  5006. TP_EC_THERMAL_TMP8_NS = 0xB8, /* ACPI EC Non-standard regs TMP 8..11 */
  5007. TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
  5008. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  5009. TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
  5010. };
  5011. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  5012. struct ibm_thermal_sensors_struct {
  5013. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  5014. };
  5015. static const struct tpacpi_quirk thermal_quirk_table[] __initconst = {
  5016. /* Non-standard address for thermal registers on some ThinkPads */
  5017. TPACPI_Q_LNV3('R', '1', 'F', true), /* L13 Yoga Gen 2 */
  5018. TPACPI_Q_LNV3('N', '2', 'U', true), /* X13 Yoga Gen 2*/
  5019. TPACPI_Q_LNV3('R', '0', 'R', true), /* L380 */
  5020. TPACPI_Q_LNV3('R', '1', '5', true), /* L13 Yoga Gen 1*/
  5021. TPACPI_Q_LNV3('R', '1', '0', true), /* L390 */
  5022. TPACPI_Q_LNV3('N', '2', 'L', true), /* X13 Yoga Gen 1*/
  5023. TPACPI_Q_LNV3('R', '0', 'T', true), /* 11e Gen5 GL*/
  5024. TPACPI_Q_LNV3('R', '1', 'D', true), /* 11e Gen5 GL-R*/
  5025. TPACPI_Q_LNV3('R', '0', 'V', true), /* 11e Gen5 KL-Y*/
  5026. };
  5027. static enum thermal_access_mode thermal_read_mode;
  5028. static bool thermal_use_labels;
  5029. static bool thermal_with_ns_address; /* Non-standard thermal reg address */
  5030. /* Function to check thermal read mode */
  5031. static enum thermal_access_mode __init thermal_read_mode_check(void)
  5032. {
  5033. u8 t, ta1, ta2, ver = 0;
  5034. int i;
  5035. int acpi_tmp7;
  5036. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  5037. if (thinkpad_id.ec_model) {
  5038. /*
  5039. * Direct EC access mode: sensors at registers 0x78-0x7F,
  5040. * 0xC0-0xC7. Registers return 0x00 for non-implemented,
  5041. * thermal sensors return 0x80 when not available.
  5042. *
  5043. * In some special cases (when Power Supply ID is 0xC2)
  5044. * above rule causes thermal control issues. Offset 0xEF
  5045. * determines EC version. 0xC0-0xC7 are not thermal registers
  5046. * in Ver 3.
  5047. */
  5048. if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
  5049. pr_warn("Thinkpad ACPI EC unable to access EC version\n");
  5050. /* Quirks to check non-standard EC */
  5051. thermal_with_ns_address = tpacpi_check_quirks(thermal_quirk_table,
  5052. ARRAY_SIZE(thermal_quirk_table));
  5053. /* Support for Thinkpads with non-standard address */
  5054. if (thermal_with_ns_address) {
  5055. pr_info("ECFW with non-standard thermal registers found\n");
  5056. return TPACPI_THERMAL_TPEC_12;
  5057. }
  5058. ta1 = ta2 = 0;
  5059. for (i = 0; i < 8; i++) {
  5060. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  5061. ta1 |= t;
  5062. } else {
  5063. ta1 = 0;
  5064. break;
  5065. }
  5066. if (ver < 3) {
  5067. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  5068. ta2 |= t;
  5069. } else {
  5070. ta1 = 0;
  5071. break;
  5072. }
  5073. }
  5074. }
  5075. if (ta1 == 0) {
  5076. /* This is sheer paranoia, but we handle it anyway */
  5077. if (acpi_tmp7) {
  5078. pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
  5079. return TPACPI_THERMAL_ACPI_TMP07;
  5080. }
  5081. pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
  5082. return TPACPI_THERMAL_NONE;
  5083. }
  5084. if (ver >= 3) {
  5085. thermal_use_labels = true;
  5086. return TPACPI_THERMAL_TPEC_8;
  5087. }
  5088. return (ta2 != 0) ? TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  5089. }
  5090. if (acpi_tmp7) {
  5091. if (tpacpi_is_ibm() && acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  5092. /* 600e/x, 770e, 770x */
  5093. return TPACPI_THERMAL_ACPI_UPDT;
  5094. }
  5095. /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
  5096. return TPACPI_THERMAL_ACPI_TMP07;
  5097. }
  5098. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  5099. return TPACPI_THERMAL_NONE;
  5100. }
  5101. /* idx is zero-based */
  5102. static int thermal_get_sensor(int idx, s32 *value)
  5103. {
  5104. int t;
  5105. s8 tmp;
  5106. char tmpi[5];
  5107. t = TP_EC_THERMAL_TMP0;
  5108. switch (thermal_read_mode) {
  5109. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  5110. case TPACPI_THERMAL_TPEC_16:
  5111. if (idx >= 8 && idx <= 15) {
  5112. t = TP_EC_THERMAL_TMP8;
  5113. idx -= 8;
  5114. }
  5115. #endif
  5116. fallthrough;
  5117. case TPACPI_THERMAL_TPEC_8:
  5118. if (idx <= 7) {
  5119. if (!acpi_ec_read(t + idx, &tmp))
  5120. return -EIO;
  5121. *value = tmp * 1000;
  5122. return 0;
  5123. }
  5124. break;
  5125. /* The Non-standard EC uses 12 Thermal areas */
  5126. case TPACPI_THERMAL_TPEC_12:
  5127. if (idx >= 12)
  5128. return -EINVAL;
  5129. t = idx < 8 ? TP_EC_THERMAL_TMP0_NS + idx :
  5130. TP_EC_THERMAL_TMP8_NS + (idx - 8);
  5131. if (!acpi_ec_read(t, &tmp))
  5132. return -EIO;
  5133. *value = tmp * MILLIDEGREE_PER_DEGREE;
  5134. return 0;
  5135. case TPACPI_THERMAL_ACPI_UPDT:
  5136. if (idx <= 7) {
  5137. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  5138. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  5139. return -EIO;
  5140. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  5141. return -EIO;
  5142. *value = (t - 2732) * 100;
  5143. return 0;
  5144. }
  5145. break;
  5146. case TPACPI_THERMAL_ACPI_TMP07:
  5147. if (idx <= 7) {
  5148. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  5149. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  5150. return -EIO;
  5151. if (t > 127 || t < -127)
  5152. t = TP_EC_THERMAL_TMP_NA;
  5153. *value = t * 1000;
  5154. return 0;
  5155. }
  5156. break;
  5157. case TPACPI_THERMAL_NONE:
  5158. default:
  5159. return -ENOSYS;
  5160. }
  5161. return -EINVAL;
  5162. }
  5163. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  5164. {
  5165. int res, i, n;
  5166. if (!s)
  5167. return -EINVAL;
  5168. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  5169. n = 16;
  5170. else if (thermal_read_mode == TPACPI_THERMAL_TPEC_12)
  5171. n = 12;
  5172. else
  5173. n = 8;
  5174. for (i = 0 ; i < n; i++) {
  5175. res = thermal_get_sensor(i, &s->temp[i]);
  5176. if (res)
  5177. return res;
  5178. }
  5179. return n;
  5180. }
  5181. static void thermal_dump_all_sensors(void)
  5182. {
  5183. int n, i;
  5184. struct ibm_thermal_sensors_struct t;
  5185. n = thermal_get_sensors(&t);
  5186. if (n <= 0)
  5187. return;
  5188. pr_notice("temperatures (Celsius):");
  5189. for (i = 0; i < n; i++) {
  5190. if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
  5191. pr_cont(" %d", (int)(t.temp[i] / 1000));
  5192. else
  5193. pr_cont(" N/A");
  5194. }
  5195. pr_cont("\n");
  5196. }
  5197. /* sysfs temp##_input -------------------------------------------------- */
  5198. static ssize_t thermal_temp_input_show(struct device *dev,
  5199. struct device_attribute *attr,
  5200. char *buf)
  5201. {
  5202. struct sensor_device_attribute *sensor_attr =
  5203. to_sensor_dev_attr(attr);
  5204. int idx = sensor_attr->index;
  5205. s32 value;
  5206. int res;
  5207. res = thermal_get_sensor(idx, &value);
  5208. if (res)
  5209. return res;
  5210. if (value == TPACPI_THERMAL_SENSOR_NA)
  5211. return -ENXIO;
  5212. return sysfs_emit(buf, "%d\n", value);
  5213. }
  5214. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  5215. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  5216. thermal_temp_input_show, NULL, _idxB)
  5217. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  5218. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  5219. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  5220. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  5221. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  5222. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  5223. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  5224. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  5225. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  5226. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  5227. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  5228. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  5229. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  5230. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  5231. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  5232. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  5233. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  5234. };
  5235. #define THERMAL_ATTRS(X) \
  5236. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  5237. static struct attribute *thermal_temp_input_attr[] = {
  5238. THERMAL_ATTRS(0),
  5239. THERMAL_ATTRS(1),
  5240. THERMAL_ATTRS(2),
  5241. THERMAL_ATTRS(3),
  5242. THERMAL_ATTRS(4),
  5243. THERMAL_ATTRS(5),
  5244. THERMAL_ATTRS(6),
  5245. THERMAL_ATTRS(7),
  5246. THERMAL_ATTRS(8),
  5247. THERMAL_ATTRS(9),
  5248. THERMAL_ATTRS(10),
  5249. THERMAL_ATTRS(11),
  5250. THERMAL_ATTRS(12),
  5251. THERMAL_ATTRS(13),
  5252. THERMAL_ATTRS(14),
  5253. THERMAL_ATTRS(15),
  5254. NULL
  5255. };
  5256. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  5257. static umode_t thermal_attr_is_visible(struct kobject *kobj,
  5258. struct attribute *attr, int n)
  5259. {
  5260. struct device_attribute *dev_attr = to_dev_attr(attr);
  5261. struct sensor_device_attribute *sensor_attr =
  5262. to_sensor_dev_attr(dev_attr);
  5263. int idx = sensor_attr->index;
  5264. switch (thermal_read_mode) {
  5265. case TPACPI_THERMAL_NONE:
  5266. return 0;
  5267. case TPACPI_THERMAL_ACPI_TMP07:
  5268. case TPACPI_THERMAL_ACPI_UPDT:
  5269. case TPACPI_THERMAL_TPEC_8:
  5270. if (idx >= 8)
  5271. return 0;
  5272. break;
  5273. case TPACPI_THERMAL_TPEC_12:
  5274. if (idx >= 12)
  5275. return 0;
  5276. break;
  5277. default:
  5278. break;
  5279. }
  5280. return attr->mode;
  5281. }
  5282. static const struct attribute_group thermal_attr_group = {
  5283. .is_visible = thermal_attr_is_visible,
  5284. .attrs = thermal_temp_input_attr,
  5285. };
  5286. #undef THERMAL_SENSOR_ATTR_TEMP
  5287. #undef THERMAL_ATTRS
  5288. static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf)
  5289. {
  5290. return sysfs_emit(buf, "CPU\n");
  5291. }
  5292. static DEVICE_ATTR_RO(temp1_label);
  5293. static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf)
  5294. {
  5295. return sysfs_emit(buf, "GPU\n");
  5296. }
  5297. static DEVICE_ATTR_RO(temp2_label);
  5298. static struct attribute *temp_label_attributes[] = {
  5299. &dev_attr_temp1_label.attr,
  5300. &dev_attr_temp2_label.attr,
  5301. NULL
  5302. };
  5303. static umode_t temp_label_attr_is_visible(struct kobject *kobj,
  5304. struct attribute *attr, int n)
  5305. {
  5306. return thermal_use_labels ? attr->mode : 0;
  5307. }
  5308. static const struct attribute_group temp_label_attr_group = {
  5309. .is_visible = temp_label_attr_is_visible,
  5310. .attrs = temp_label_attributes,
  5311. };
  5312. /* --------------------------------------------------------------------- */
  5313. static int __init thermal_init(struct ibm_init_struct *iibm)
  5314. {
  5315. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  5316. thermal_read_mode = thermal_read_mode_check();
  5317. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  5318. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  5319. thermal_read_mode);
  5320. return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV;
  5321. }
  5322. static int thermal_read(struct seq_file *m)
  5323. {
  5324. int n, i;
  5325. struct ibm_thermal_sensors_struct t;
  5326. n = thermal_get_sensors(&t);
  5327. if (unlikely(n < 0))
  5328. return n;
  5329. seq_printf(m, "temperatures:\t");
  5330. if (n > 0) {
  5331. for (i = 0; i < (n - 1); i++)
  5332. seq_printf(m, "%d ", t.temp[i] / 1000);
  5333. seq_printf(m, "%d\n", t.temp[i] / 1000);
  5334. } else
  5335. seq_printf(m, "not supported\n");
  5336. return 0;
  5337. }
  5338. static struct ibm_struct thermal_driver_data = {
  5339. .name = "thermal",
  5340. .read = thermal_read,
  5341. };
  5342. /*************************************************************************
  5343. * Backlight/brightness subdriver
  5344. */
  5345. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  5346. /*
  5347. * ThinkPads can read brightness from two places: EC HBRV (0x31), or
  5348. * CMOS NVRAM byte 0x5E, bits 0-3.
  5349. *
  5350. * EC HBRV (0x31) has the following layout
  5351. * Bit 7: unknown function
  5352. * Bit 6: unknown function
  5353. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  5354. * Bit 4: must be set to zero to avoid problems
  5355. * Bit 3-0: backlight brightness level
  5356. *
  5357. * brightness_get_raw returns status data in the HBRV layout
  5358. *
  5359. * WARNING: The X61 has been verified to use HBRV for something else, so
  5360. * this should be used _only_ on IBM ThinkPads, and maybe with some careful
  5361. * testing on the very early *60 Lenovo models...
  5362. */
  5363. enum {
  5364. TP_EC_BACKLIGHT = 0x31,
  5365. /* TP_EC_BACKLIGHT bitmasks */
  5366. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  5367. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  5368. TP_EC_BACKLIGHT_MAPSW = 0x20,
  5369. };
  5370. enum tpacpi_brightness_access_mode {
  5371. TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
  5372. TPACPI_BRGHT_MODE_EC, /* EC control */
  5373. TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
  5374. TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  5375. TPACPI_BRGHT_MODE_MAX
  5376. };
  5377. static struct backlight_device *ibm_backlight_device;
  5378. static enum tpacpi_brightness_access_mode brightness_mode =
  5379. TPACPI_BRGHT_MODE_MAX;
  5380. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  5381. static struct mutex brightness_mutex;
  5382. /* NVRAM brightness access */
  5383. static unsigned int tpacpi_brightness_nvram_get(void)
  5384. {
  5385. u8 lnvram;
  5386. lockdep_assert_held(&brightness_mutex);
  5387. lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  5388. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  5389. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  5390. lnvram &= bright_maxlvl;
  5391. return lnvram;
  5392. }
  5393. static void tpacpi_brightness_checkpoint_nvram(void)
  5394. {
  5395. u8 lec = 0;
  5396. u8 b_nvram;
  5397. if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
  5398. return;
  5399. vdbg_printk(TPACPI_DBG_BRGHT,
  5400. "trying to checkpoint backlight level to NVRAM...\n");
  5401. if (mutex_lock_killable(&brightness_mutex) < 0)
  5402. return;
  5403. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5404. goto unlock;
  5405. lec &= TP_EC_BACKLIGHT_LVLMSK;
  5406. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
  5407. if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  5408. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
  5409. /* NVRAM needs update */
  5410. b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
  5411. TP_NVRAM_POS_LEVEL_BRIGHTNESS);
  5412. b_nvram |= lec;
  5413. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
  5414. dbg_printk(TPACPI_DBG_BRGHT,
  5415. "updated NVRAM backlight level to %u (0x%02x)\n",
  5416. (unsigned int) lec, (unsigned int) b_nvram);
  5417. } else
  5418. vdbg_printk(TPACPI_DBG_BRGHT,
  5419. "NVRAM backlight level already is %u (0x%02x)\n",
  5420. (unsigned int) lec, (unsigned int) b_nvram);
  5421. unlock:
  5422. mutex_unlock(&brightness_mutex);
  5423. }
  5424. static int tpacpi_brightness_get_raw(int *status)
  5425. {
  5426. u8 lec = 0;
  5427. lockdep_assert_held(&brightness_mutex);
  5428. switch (brightness_mode) {
  5429. case TPACPI_BRGHT_MODE_UCMS_STEP:
  5430. *status = tpacpi_brightness_nvram_get();
  5431. return 0;
  5432. case TPACPI_BRGHT_MODE_EC:
  5433. case TPACPI_BRGHT_MODE_ECNVRAM:
  5434. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5435. return -EIO;
  5436. *status = lec;
  5437. return 0;
  5438. default:
  5439. return -ENXIO;
  5440. }
  5441. }
  5442. /* do NOT call with illegal backlight level value */
  5443. static int tpacpi_brightness_set_ec(unsigned int value)
  5444. {
  5445. u8 lec = 0;
  5446. lockdep_assert_held(&brightness_mutex);
  5447. if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
  5448. return -EIO;
  5449. if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
  5450. (lec & TP_EC_BACKLIGHT_CMDMSK) |
  5451. (value & TP_EC_BACKLIGHT_LVLMSK))))
  5452. return -EIO;
  5453. return 0;
  5454. }
  5455. static int tpacpi_brightness_set_ucmsstep(unsigned int value)
  5456. {
  5457. int cmos_cmd, inc;
  5458. unsigned int current_value, i;
  5459. lockdep_assert_held(&brightness_mutex);
  5460. current_value = tpacpi_brightness_nvram_get();
  5461. if (value == current_value)
  5462. return 0;
  5463. cmos_cmd = (value > current_value) ?
  5464. TP_CMOS_BRIGHTNESS_UP :
  5465. TP_CMOS_BRIGHTNESS_DOWN;
  5466. inc = (value > current_value) ? 1 : -1;
  5467. for (i = current_value; i != value; i += inc)
  5468. if (issue_thinkpad_cmos_command(cmos_cmd))
  5469. return -EIO;
  5470. return 0;
  5471. }
  5472. /* May return EINTR which can always be mapped to ERESTARTSYS */
  5473. static int brightness_set(unsigned int value)
  5474. {
  5475. int res;
  5476. if (value > bright_maxlvl)
  5477. return -EINVAL;
  5478. vdbg_printk(TPACPI_DBG_BRGHT,
  5479. "set backlight level to %d\n", value);
  5480. res = mutex_lock_killable(&brightness_mutex);
  5481. if (res < 0)
  5482. return res;
  5483. switch (brightness_mode) {
  5484. case TPACPI_BRGHT_MODE_EC:
  5485. case TPACPI_BRGHT_MODE_ECNVRAM:
  5486. res = tpacpi_brightness_set_ec(value);
  5487. break;
  5488. case TPACPI_BRGHT_MODE_UCMS_STEP:
  5489. res = tpacpi_brightness_set_ucmsstep(value);
  5490. break;
  5491. default:
  5492. res = -ENXIO;
  5493. }
  5494. mutex_unlock(&brightness_mutex);
  5495. return res;
  5496. }
  5497. /* sysfs backlight class ----------------------------------------------- */
  5498. static int brightness_update_status(struct backlight_device *bd)
  5499. {
  5500. int level = backlight_get_brightness(bd);
  5501. dbg_printk(TPACPI_DBG_BRGHT,
  5502. "backlight: attempt to set level to %d\n",
  5503. level);
  5504. /* it is the backlight class's job (caller) to handle
  5505. * EINTR and other errors properly */
  5506. return brightness_set(level);
  5507. }
  5508. static int brightness_get(struct backlight_device *bd)
  5509. {
  5510. int status, res;
  5511. res = mutex_lock_killable(&brightness_mutex);
  5512. if (res < 0)
  5513. return 0;
  5514. res = tpacpi_brightness_get_raw(&status);
  5515. mutex_unlock(&brightness_mutex);
  5516. if (res < 0)
  5517. return 0;
  5518. return status & TP_EC_BACKLIGHT_LVLMSK;
  5519. }
  5520. static void tpacpi_brightness_notify_change(void)
  5521. {
  5522. backlight_force_update(ibm_backlight_device,
  5523. BACKLIGHT_UPDATE_HOTKEY);
  5524. }
  5525. static const struct backlight_ops ibm_backlight_data = {
  5526. .get_brightness = brightness_get,
  5527. .update_status = brightness_update_status,
  5528. };
  5529. /* --------------------------------------------------------------------- */
  5530. static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used)
  5531. {
  5532. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  5533. union acpi_object *obj;
  5534. acpi_status status;
  5535. int rc;
  5536. status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer);
  5537. if (ACPI_FAILURE(status))
  5538. return 0;
  5539. obj = buffer.pointer;
  5540. if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
  5541. acpi_handle_info(adev->handle,
  5542. "Unknown _BCL data, please report this to %s\n",
  5543. TPACPI_MAIL);
  5544. rc = 0;
  5545. } else {
  5546. rc = obj->package.count;
  5547. }
  5548. kfree(obj);
  5549. return rc;
  5550. }
  5551. /*
  5552. * Call _BCL method of video device. On some ThinkPads this will
  5553. * switch the firmware to the ACPI brightness control mode.
  5554. */
  5555. static int __init tpacpi_query_bcl_levels(acpi_handle handle)
  5556. {
  5557. struct acpi_device *device;
  5558. device = acpi_fetch_acpi_dev(handle);
  5559. if (!device)
  5560. return 0;
  5561. return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL);
  5562. }
  5563. /*
  5564. * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
  5565. */
  5566. static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
  5567. {
  5568. acpi_handle video_device;
  5569. int bcl_levels = 0;
  5570. tpacpi_acpi_handle_locate("video", NULL, &video_device);
  5571. if (video_device)
  5572. bcl_levels = tpacpi_query_bcl_levels(video_device);
  5573. tp_features.bright_acpimode = (bcl_levels > 0);
  5574. return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
  5575. }
  5576. /*
  5577. * These are only useful for models that have only one possibility
  5578. * of GPU. If the BIOS model handles both ATI and Intel, don't use
  5579. * these quirks.
  5580. */
  5581. #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
  5582. #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
  5583. #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
  5584. static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
  5585. /* Models with ATI GPUs known to require ECNVRAM mode */
  5586. TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
  5587. /* Models with ATI GPUs that can use ECNVRAM */
  5588. TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
  5589. TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5590. TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
  5591. TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5592. /* Models with Intel Extreme Graphics 2 */
  5593. TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
  5594. TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5595. TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
  5596. /* Models with Intel GMA900 */
  5597. TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
  5598. TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
  5599. TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
  5600. };
  5601. /*
  5602. * Returns < 0 for error, otherwise sets tp_features.bright_*
  5603. * and bright_maxlvl.
  5604. */
  5605. static void __init tpacpi_detect_brightness_capabilities(void)
  5606. {
  5607. unsigned int b;
  5608. vdbg_printk(TPACPI_DBG_INIT,
  5609. "detecting firmware brightness interface capabilities\n");
  5610. /* we could run a quirks check here (same table used by
  5611. * brightness_init) if needed */
  5612. /*
  5613. * We always attempt to detect acpi support, so as to switch
  5614. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  5615. * going to publish a backlight interface
  5616. */
  5617. b = tpacpi_check_std_acpi_brightness_support();
  5618. switch (b) {
  5619. case 16:
  5620. bright_maxlvl = 15;
  5621. break;
  5622. case 8:
  5623. case 0:
  5624. bright_maxlvl = 7;
  5625. break;
  5626. default:
  5627. tp_features.bright_unkfw = 1;
  5628. bright_maxlvl = b - 1;
  5629. }
  5630. pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
  5631. }
  5632. static int __init brightness_init(struct ibm_init_struct *iibm)
  5633. {
  5634. struct backlight_properties props;
  5635. int b;
  5636. unsigned long quirks;
  5637. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  5638. mutex_init(&brightness_mutex);
  5639. quirks = tpacpi_check_quirks(brightness_quirk_table,
  5640. ARRAY_SIZE(brightness_quirk_table));
  5641. /* tpacpi_detect_brightness_capabilities() must have run already */
  5642. /* if it is unknown, we don't handle it: it wouldn't be safe */
  5643. if (tp_features.bright_unkfw)
  5644. return -ENODEV;
  5645. if (!brightness_enable) {
  5646. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5647. "brightness support disabled by module parameter\n");
  5648. return -ENODEV;
  5649. }
  5650. if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
  5651. if (brightness_enable > 1) {
  5652. pr_info("Standard ACPI backlight interface available, not loading native one\n");
  5653. return -ENODEV;
  5654. } else if (brightness_enable == 1) {
  5655. pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
  5656. return -ENODEV;
  5657. }
  5658. } else if (!tp_features.bright_acpimode) {
  5659. pr_notice("ACPI backlight interface not available\n");
  5660. return -ENODEV;
  5661. }
  5662. pr_notice("ACPI native brightness control enabled\n");
  5663. /*
  5664. * Check for module parameter bogosity, note that we
  5665. * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
  5666. * able to detect "unspecified"
  5667. */
  5668. if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
  5669. return -EINVAL;
  5670. /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
  5671. if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
  5672. brightness_mode == TPACPI_BRGHT_MODE_MAX) {
  5673. if (quirks & TPACPI_BRGHT_Q_EC)
  5674. brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
  5675. else
  5676. brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
  5677. dbg_printk(TPACPI_DBG_BRGHT,
  5678. "driver auto-selected brightness_mode=%d\n",
  5679. brightness_mode);
  5680. }
  5681. /* Safety */
  5682. if (!tpacpi_is_ibm() &&
  5683. (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
  5684. brightness_mode == TPACPI_BRGHT_MODE_EC))
  5685. return -EINVAL;
  5686. if (tpacpi_brightness_get_raw(&b) < 0)
  5687. return -ENODEV;
  5688. memset(&props, 0, sizeof(struct backlight_properties));
  5689. props.type = BACKLIGHT_PLATFORM;
  5690. props.max_brightness = bright_maxlvl;
  5691. props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  5692. ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
  5693. NULL, NULL,
  5694. &ibm_backlight_data,
  5695. &props);
  5696. if (IS_ERR(ibm_backlight_device)) {
  5697. int rc = PTR_ERR(ibm_backlight_device);
  5698. ibm_backlight_device = NULL;
  5699. pr_err("Could not register backlight device\n");
  5700. return rc;
  5701. }
  5702. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5703. "brightness is supported\n");
  5704. if (quirks & TPACPI_BRGHT_Q_ASK) {
  5705. pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
  5706. brightness_mode);
  5707. pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
  5708. TPACPI_MAIL);
  5709. }
  5710. /* Added by mistake in early 2007. Probably useless, but it could
  5711. * be working around some unknown firmware problem where the value
  5712. * read at startup doesn't match the real hardware state... so leave
  5713. * it in place just in case */
  5714. backlight_update_status(ibm_backlight_device);
  5715. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
  5716. "brightness: registering brightness hotkeys as change notification\n");
  5717. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  5718. | TP_ACPI_HKEY_BRGHTUP_MASK
  5719. | TP_ACPI_HKEY_BRGHTDWN_MASK);
  5720. return 0;
  5721. }
  5722. static void brightness_suspend(void)
  5723. {
  5724. tpacpi_brightness_checkpoint_nvram();
  5725. }
  5726. static void brightness_shutdown(void)
  5727. {
  5728. tpacpi_brightness_checkpoint_nvram();
  5729. }
  5730. static void brightness_exit(void)
  5731. {
  5732. if (ibm_backlight_device) {
  5733. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
  5734. "calling backlight_device_unregister()\n");
  5735. backlight_device_unregister(ibm_backlight_device);
  5736. }
  5737. tpacpi_brightness_checkpoint_nvram();
  5738. }
  5739. static int brightness_read(struct seq_file *m)
  5740. {
  5741. int level;
  5742. level = brightness_get(NULL);
  5743. if (level < 0) {
  5744. seq_printf(m, "level:\t\tunreadable\n");
  5745. } else {
  5746. seq_printf(m, "level:\t\t%d\n", level);
  5747. seq_printf(m, "commands:\tup, down\n");
  5748. seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
  5749. bright_maxlvl);
  5750. }
  5751. return 0;
  5752. }
  5753. static int brightness_write(char *buf)
  5754. {
  5755. int level;
  5756. int rc;
  5757. char *cmd;
  5758. level = brightness_get(NULL);
  5759. if (level < 0)
  5760. return level;
  5761. while ((cmd = strsep(&buf, ","))) {
  5762. if (strstarts(cmd, "up")) {
  5763. if (level < bright_maxlvl)
  5764. level++;
  5765. } else if (strstarts(cmd, "down")) {
  5766. if (level > 0)
  5767. level--;
  5768. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  5769. level >= 0 && level <= bright_maxlvl) {
  5770. /* new level set */
  5771. } else
  5772. return -EINVAL;
  5773. }
  5774. tpacpi_disclose_usertask("procfs brightness",
  5775. "set level to %d\n", level);
  5776. /*
  5777. * Now we know what the final level should be, so we try to set it.
  5778. * Doing it this way makes the syscall restartable in case of EINTR
  5779. */
  5780. rc = brightness_set(level);
  5781. if (!rc && ibm_backlight_device)
  5782. backlight_force_update(ibm_backlight_device,
  5783. BACKLIGHT_UPDATE_SYSFS);
  5784. return (rc == -EINTR) ? -ERESTARTSYS : rc;
  5785. }
  5786. static struct ibm_struct brightness_driver_data = {
  5787. .name = "brightness",
  5788. .read = brightness_read,
  5789. .write = brightness_write,
  5790. .exit = brightness_exit,
  5791. .suspend = brightness_suspend,
  5792. .shutdown = brightness_shutdown,
  5793. };
  5794. /*************************************************************************
  5795. * Volume subdriver
  5796. */
  5797. /*
  5798. * IBM ThinkPads have a simple volume controller with MUTE gating.
  5799. * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
  5800. *
  5801. * Since the *61 series (and probably also the later *60 series), Lenovo
  5802. * ThinkPads only implement the MUTE gate.
  5803. *
  5804. * EC register 0x30
  5805. * Bit 6: MUTE (1 mutes sound)
  5806. * Bit 3-0: Volume
  5807. * Other bits should be zero as far as we know.
  5808. *
  5809. * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
  5810. * bits 3-0 (volume). Other bits in NVRAM may have other functions,
  5811. * such as bit 7 which is used to detect repeated presses of MUTE,
  5812. * and we leave them unchanged.
  5813. *
  5814. * On newer Lenovo ThinkPads, the EC can automatically change the volume
  5815. * in response to user input. Unfortunately, this rarely works well.
  5816. * The laptop changes the state of its internal MUTE gate and, on some
  5817. * models, sends KEY_MUTE, causing any user code that responds to the
  5818. * mute button to get confused. The hardware MUTE gate is also
  5819. * unnecessary, since user code can handle the mute button without
  5820. * kernel or EC help.
  5821. *
  5822. * To avoid confusing userspace, we simply disable all EC-based mute
  5823. * and volume controls when possible.
  5824. */
  5825. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  5826. #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
  5827. #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
  5828. #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
  5829. #if SNDRV_CARDS <= 32
  5830. #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
  5831. #else
  5832. #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
  5833. #endif
  5834. static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
  5835. static char *alsa_id = "ThinkPadEC";
  5836. static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
  5837. struct tpacpi_alsa_data {
  5838. struct snd_card *card;
  5839. struct snd_ctl_elem_id *ctl_mute_id;
  5840. struct snd_ctl_elem_id *ctl_vol_id;
  5841. };
  5842. static struct snd_card *alsa_card;
  5843. enum {
  5844. TP_EC_AUDIO = 0x30,
  5845. /* TP_EC_AUDIO bits */
  5846. TP_EC_AUDIO_MUTESW = 6,
  5847. /* TP_EC_AUDIO bitmasks */
  5848. TP_EC_AUDIO_LVL_MSK = 0x0F,
  5849. TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
  5850. /* Maximum volume */
  5851. TP_EC_VOLUME_MAX = 14,
  5852. };
  5853. enum tpacpi_volume_access_mode {
  5854. TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
  5855. TPACPI_VOL_MODE_EC, /* Pure EC control */
  5856. TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
  5857. TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
  5858. TPACPI_VOL_MODE_MAX
  5859. };
  5860. enum tpacpi_volume_capabilities {
  5861. TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
  5862. TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
  5863. TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
  5864. TPACPI_VOL_CAP_MAX
  5865. };
  5866. enum tpacpi_mute_btn_mode {
  5867. TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
  5868. /* We don't know what mode 1 is. */
  5869. TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
  5870. TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
  5871. };
  5872. static enum tpacpi_volume_access_mode volume_mode =
  5873. TPACPI_VOL_MODE_MAX;
  5874. static enum tpacpi_volume_capabilities volume_capabilities;
  5875. static bool volume_control_allowed;
  5876. static bool software_mute_requested = true;
  5877. static bool software_mute_active;
  5878. static int software_mute_orig_mode;
  5879. /*
  5880. * Used to syncronize writers to TP_EC_AUDIO and
  5881. * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
  5882. */
  5883. static struct mutex volume_mutex;
  5884. static void tpacpi_volume_checkpoint_nvram(void)
  5885. {
  5886. u8 lec = 0;
  5887. u8 b_nvram;
  5888. u8 ec_mask;
  5889. if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
  5890. return;
  5891. if (!volume_control_allowed)
  5892. return;
  5893. if (software_mute_active)
  5894. return;
  5895. vdbg_printk(TPACPI_DBG_MIXER,
  5896. "trying to checkpoint mixer state to NVRAM...\n");
  5897. if (tp_features.mixer_no_level_control)
  5898. ec_mask = TP_EC_AUDIO_MUTESW_MSK;
  5899. else
  5900. ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
  5901. if (mutex_lock_killable(&volume_mutex) < 0)
  5902. return;
  5903. if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
  5904. goto unlock;
  5905. lec &= ec_mask;
  5906. b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
  5907. if (lec != (b_nvram & ec_mask)) {
  5908. /* NVRAM needs update */
  5909. b_nvram &= ~ec_mask;
  5910. b_nvram |= lec;
  5911. nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
  5912. dbg_printk(TPACPI_DBG_MIXER,
  5913. "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
  5914. (unsigned int) lec, (unsigned int) b_nvram);
  5915. } else {
  5916. vdbg_printk(TPACPI_DBG_MIXER,
  5917. "NVRAM mixer status already is 0x%02x (0x%02x)\n",
  5918. (unsigned int) lec, (unsigned int) b_nvram);
  5919. }
  5920. unlock:
  5921. mutex_unlock(&volume_mutex);
  5922. }
  5923. static int volume_get_status_ec(u8 *status)
  5924. {
  5925. u8 s;
  5926. if (!acpi_ec_read(TP_EC_AUDIO, &s))
  5927. return -EIO;
  5928. *status = s;
  5929. dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
  5930. return 0;
  5931. }
  5932. static int volume_get_status(u8 *status)
  5933. {
  5934. return volume_get_status_ec(status);
  5935. }
  5936. static int volume_set_status_ec(const u8 status)
  5937. {
  5938. if (!acpi_ec_write(TP_EC_AUDIO, status))
  5939. return -EIO;
  5940. dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
  5941. /*
  5942. * On X200s, and possibly on others, it can take a while for
  5943. * reads to become correct.
  5944. */
  5945. msleep(1);
  5946. return 0;
  5947. }
  5948. static int volume_set_status(const u8 status)
  5949. {
  5950. return volume_set_status_ec(status);
  5951. }
  5952. /* returns < 0 on error, 0 on no change, 1 on change */
  5953. static int __volume_set_mute_ec(const bool mute)
  5954. {
  5955. int rc;
  5956. u8 s, n;
  5957. if (mutex_lock_killable(&volume_mutex) < 0)
  5958. return -EINTR;
  5959. rc = volume_get_status_ec(&s);
  5960. if (rc)
  5961. goto unlock;
  5962. n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
  5963. s & ~TP_EC_AUDIO_MUTESW_MSK;
  5964. if (n != s) {
  5965. rc = volume_set_status_ec(n);
  5966. if (!rc)
  5967. rc = 1;
  5968. }
  5969. unlock:
  5970. mutex_unlock(&volume_mutex);
  5971. return rc;
  5972. }
  5973. static int volume_alsa_set_mute(const bool mute)
  5974. {
  5975. dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
  5976. (mute) ? "" : "un");
  5977. return __volume_set_mute_ec(mute);
  5978. }
  5979. static int volume_set_mute(const bool mute)
  5980. {
  5981. int rc;
  5982. dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
  5983. (mute) ? "" : "un");
  5984. rc = __volume_set_mute_ec(mute);
  5985. return (rc < 0) ? rc : 0;
  5986. }
  5987. /* returns < 0 on error, 0 on no change, 1 on change */
  5988. static int __volume_set_volume_ec(const u8 vol)
  5989. {
  5990. int rc;
  5991. u8 s, n;
  5992. if (vol > TP_EC_VOLUME_MAX)
  5993. return -EINVAL;
  5994. if (mutex_lock_killable(&volume_mutex) < 0)
  5995. return -EINTR;
  5996. rc = volume_get_status_ec(&s);
  5997. if (rc)
  5998. goto unlock;
  5999. n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
  6000. if (n != s) {
  6001. rc = volume_set_status_ec(n);
  6002. if (!rc)
  6003. rc = 1;
  6004. }
  6005. unlock:
  6006. mutex_unlock(&volume_mutex);
  6007. return rc;
  6008. }
  6009. static int volume_set_software_mute(bool startup)
  6010. {
  6011. int result;
  6012. if (!tpacpi_is_lenovo())
  6013. return -ENODEV;
  6014. if (startup) {
  6015. if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
  6016. "HAUM", "qd"))
  6017. return -EIO;
  6018. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6019. "Initial HAUM setting was %d\n",
  6020. software_mute_orig_mode);
  6021. }
  6022. if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
  6023. (int)TP_EC_MUTE_BTN_NONE))
  6024. return -EIO;
  6025. if (result != TP_EC_MUTE_BTN_NONE)
  6026. pr_warn("Unexpected SAUM result %d\n",
  6027. result);
  6028. /*
  6029. * In software mute mode, the standard codec controls take
  6030. * precendence, so we unmute the ThinkPad HW switch at
  6031. * startup. Just on case there are SAUM-capable ThinkPads
  6032. * with level controls, set max HW volume as well.
  6033. */
  6034. if (tp_features.mixer_no_level_control)
  6035. result = volume_set_mute(false);
  6036. else
  6037. result = volume_set_status(TP_EC_VOLUME_MAX);
  6038. if (result != 0)
  6039. pr_warn("Failed to unmute the HW mute switch\n");
  6040. return 0;
  6041. }
  6042. static void volume_exit_software_mute(void)
  6043. {
  6044. int r;
  6045. if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
  6046. || r != software_mute_orig_mode)
  6047. pr_warn("Failed to restore mute mode\n");
  6048. }
  6049. static int volume_alsa_set_volume(const u8 vol)
  6050. {
  6051. dbg_printk(TPACPI_DBG_MIXER,
  6052. "ALSA: trying to set volume level to %hu\n", vol);
  6053. return __volume_set_volume_ec(vol);
  6054. }
  6055. static void volume_alsa_notify_change(void)
  6056. {
  6057. struct tpacpi_alsa_data *d;
  6058. if (alsa_card && alsa_card->private_data) {
  6059. d = alsa_card->private_data;
  6060. if (d->ctl_mute_id)
  6061. snd_ctl_notify(alsa_card,
  6062. SNDRV_CTL_EVENT_MASK_VALUE,
  6063. d->ctl_mute_id);
  6064. if (d->ctl_vol_id)
  6065. snd_ctl_notify(alsa_card,
  6066. SNDRV_CTL_EVENT_MASK_VALUE,
  6067. d->ctl_vol_id);
  6068. }
  6069. }
  6070. static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
  6071. struct snd_ctl_elem_info *uinfo)
  6072. {
  6073. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  6074. uinfo->count = 1;
  6075. uinfo->value.integer.min = 0;
  6076. uinfo->value.integer.max = TP_EC_VOLUME_MAX;
  6077. return 0;
  6078. }
  6079. static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
  6080. struct snd_ctl_elem_value *ucontrol)
  6081. {
  6082. u8 s;
  6083. int rc;
  6084. rc = volume_get_status(&s);
  6085. if (rc < 0)
  6086. return rc;
  6087. ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
  6088. return 0;
  6089. }
  6090. static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
  6091. struct snd_ctl_elem_value *ucontrol)
  6092. {
  6093. tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
  6094. ucontrol->value.integer.value[0]);
  6095. return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
  6096. }
  6097. #define volume_alsa_mute_info snd_ctl_boolean_mono_info
  6098. static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
  6099. struct snd_ctl_elem_value *ucontrol)
  6100. {
  6101. u8 s;
  6102. int rc;
  6103. rc = volume_get_status(&s);
  6104. if (rc < 0)
  6105. return rc;
  6106. ucontrol->value.integer.value[0] =
  6107. (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
  6108. return 0;
  6109. }
  6110. static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
  6111. struct snd_ctl_elem_value *ucontrol)
  6112. {
  6113. tpacpi_disclose_usertask("ALSA", "%smute\n",
  6114. ucontrol->value.integer.value[0] ?
  6115. "un" : "");
  6116. return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
  6117. }
  6118. static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
  6119. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6120. .name = "Console Playback Volume",
  6121. .index = 0,
  6122. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  6123. .info = volume_alsa_vol_info,
  6124. .get = volume_alsa_vol_get,
  6125. };
  6126. static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
  6127. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6128. .name = "Console Playback Switch",
  6129. .index = 0,
  6130. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  6131. .info = volume_alsa_mute_info,
  6132. .get = volume_alsa_mute_get,
  6133. };
  6134. static void volume_suspend(void)
  6135. {
  6136. tpacpi_volume_checkpoint_nvram();
  6137. }
  6138. static void volume_resume(void)
  6139. {
  6140. if (software_mute_active) {
  6141. if (volume_set_software_mute(false) < 0)
  6142. pr_warn("Failed to restore software mute\n");
  6143. } else {
  6144. volume_alsa_notify_change();
  6145. }
  6146. }
  6147. static void volume_shutdown(void)
  6148. {
  6149. tpacpi_volume_checkpoint_nvram();
  6150. }
  6151. static void volume_exit(void)
  6152. {
  6153. if (alsa_card) {
  6154. snd_card_free(alsa_card);
  6155. alsa_card = NULL;
  6156. }
  6157. tpacpi_volume_checkpoint_nvram();
  6158. if (software_mute_active)
  6159. volume_exit_software_mute();
  6160. }
  6161. static int __init volume_create_alsa_mixer(void)
  6162. {
  6163. struct snd_card *card;
  6164. struct tpacpi_alsa_data *data;
  6165. struct snd_kcontrol *ctl_vol;
  6166. struct snd_kcontrol *ctl_mute;
  6167. int rc;
  6168. rc = snd_card_new(&tpacpi_pdev->dev,
  6169. alsa_index, alsa_id, THIS_MODULE,
  6170. sizeof(struct tpacpi_alsa_data), &card);
  6171. if (rc < 0 || !card) {
  6172. pr_err("Failed to create ALSA card structures: %d\n", rc);
  6173. return -ENODEV;
  6174. }
  6175. BUG_ON(!card->private_data);
  6176. data = card->private_data;
  6177. data->card = card;
  6178. strscpy(card->driver, TPACPI_ALSA_DRVNAME);
  6179. strscpy(card->shortname, TPACPI_ALSA_SHRTNAME);
  6180. snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
  6181. (thinkpad_id.ec_version_str) ?
  6182. thinkpad_id.ec_version_str : "(unknown)");
  6183. snprintf(card->longname, sizeof(card->longname),
  6184. "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
  6185. (thinkpad_id.ec_version_str) ?
  6186. thinkpad_id.ec_version_str : "unknown");
  6187. if (volume_control_allowed) {
  6188. volume_alsa_control_vol.put = volume_alsa_vol_put;
  6189. volume_alsa_control_vol.access =
  6190. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  6191. volume_alsa_control_mute.put = volume_alsa_mute_put;
  6192. volume_alsa_control_mute.access =
  6193. SNDRV_CTL_ELEM_ACCESS_READWRITE;
  6194. }
  6195. if (!tp_features.mixer_no_level_control) {
  6196. ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
  6197. rc = snd_ctl_add(card, ctl_vol);
  6198. if (rc < 0) {
  6199. pr_err("Failed to create ALSA volume control: %d\n",
  6200. rc);
  6201. goto err_exit;
  6202. }
  6203. data->ctl_vol_id = &ctl_vol->id;
  6204. }
  6205. ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
  6206. rc = snd_ctl_add(card, ctl_mute);
  6207. if (rc < 0) {
  6208. pr_err("Failed to create ALSA mute control: %d\n", rc);
  6209. goto err_exit;
  6210. }
  6211. data->ctl_mute_id = &ctl_mute->id;
  6212. rc = snd_card_register(card);
  6213. if (rc < 0) {
  6214. pr_err("Failed to register ALSA card: %d\n", rc);
  6215. goto err_exit;
  6216. }
  6217. alsa_card = card;
  6218. return 0;
  6219. err_exit:
  6220. snd_card_free(card);
  6221. return -ENODEV;
  6222. }
  6223. #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
  6224. #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
  6225. static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
  6226. /* Whitelist volume level on all IBM by default */
  6227. { .vendor = PCI_VENDOR_ID_IBM,
  6228. .bios = TPACPI_MATCH_ANY,
  6229. .ec = TPACPI_MATCH_ANY,
  6230. .quirks = TPACPI_VOL_Q_LEVEL },
  6231. /* Lenovo models with volume control (needs confirmation) */
  6232. TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
  6233. TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
  6234. TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
  6235. TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
  6236. TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
  6237. TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
  6238. TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
  6239. /* Whitelist mute-only on all Lenovo by default */
  6240. { .vendor = PCI_VENDOR_ID_LENOVO,
  6241. .bios = TPACPI_MATCH_ANY,
  6242. .ec = TPACPI_MATCH_ANY,
  6243. .quirks = TPACPI_VOL_Q_MUTEONLY }
  6244. };
  6245. static int __init volume_init(struct ibm_init_struct *iibm)
  6246. {
  6247. unsigned long quirks;
  6248. int rc;
  6249. vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
  6250. mutex_init(&volume_mutex);
  6251. /*
  6252. * Check for module parameter bogosity, note that we
  6253. * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
  6254. * able to detect "unspecified"
  6255. */
  6256. if (volume_mode > TPACPI_VOL_MODE_MAX)
  6257. return -EINVAL;
  6258. if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
  6259. pr_err("UCMS step volume mode not implemented, please contact %s\n",
  6260. TPACPI_MAIL);
  6261. return -ENODEV;
  6262. }
  6263. if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
  6264. return -EINVAL;
  6265. /*
  6266. * The ALSA mixer is our primary interface.
  6267. * When disabled, don't install the subdriver at all
  6268. */
  6269. if (!alsa_enable) {
  6270. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6271. "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
  6272. return -ENODEV;
  6273. }
  6274. quirks = tpacpi_check_quirks(volume_quirk_table,
  6275. ARRAY_SIZE(volume_quirk_table));
  6276. switch (volume_capabilities) {
  6277. case TPACPI_VOL_CAP_AUTO:
  6278. if (quirks & TPACPI_VOL_Q_MUTEONLY)
  6279. tp_features.mixer_no_level_control = 1;
  6280. else if (quirks & TPACPI_VOL_Q_LEVEL)
  6281. tp_features.mixer_no_level_control = 0;
  6282. else
  6283. return -ENODEV; /* no mixer */
  6284. break;
  6285. case TPACPI_VOL_CAP_VOLMUTE:
  6286. tp_features.mixer_no_level_control = 0;
  6287. break;
  6288. case TPACPI_VOL_CAP_MUTEONLY:
  6289. tp_features.mixer_no_level_control = 1;
  6290. break;
  6291. default:
  6292. return -ENODEV;
  6293. }
  6294. if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
  6295. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6296. "using user-supplied volume_capabilities=%d\n",
  6297. volume_capabilities);
  6298. if (volume_mode == TPACPI_VOL_MODE_AUTO ||
  6299. volume_mode == TPACPI_VOL_MODE_MAX) {
  6300. volume_mode = TPACPI_VOL_MODE_ECNVRAM;
  6301. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6302. "driver auto-selected volume_mode=%d\n",
  6303. volume_mode);
  6304. } else {
  6305. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6306. "using user-supplied volume_mode=%d\n",
  6307. volume_mode);
  6308. }
  6309. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6310. "mute is supported, volume control is %s\n",
  6311. str_supported(!tp_features.mixer_no_level_control));
  6312. if (software_mute_requested && volume_set_software_mute(true) == 0) {
  6313. software_mute_active = true;
  6314. } else {
  6315. rc = volume_create_alsa_mixer();
  6316. if (rc) {
  6317. pr_err("Could not create the ALSA mixer interface\n");
  6318. return rc;
  6319. }
  6320. pr_info("Console audio control enabled, mode: %s\n",
  6321. (volume_control_allowed) ?
  6322. "override (read/write)" :
  6323. "monitor (read only)");
  6324. }
  6325. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
  6326. "registering volume hotkeys as change notification\n");
  6327. tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
  6328. | TP_ACPI_HKEY_VOLUP_MASK
  6329. | TP_ACPI_HKEY_VOLDWN_MASK
  6330. | TP_ACPI_HKEY_MUTE_MASK);
  6331. return 0;
  6332. }
  6333. static int volume_read(struct seq_file *m)
  6334. {
  6335. u8 status;
  6336. if (volume_get_status(&status) < 0) {
  6337. seq_printf(m, "level:\t\tunreadable\n");
  6338. } else {
  6339. if (tp_features.mixer_no_level_control)
  6340. seq_printf(m, "level:\t\tunsupported\n");
  6341. else
  6342. seq_printf(m, "level:\t\t%d\n",
  6343. status & TP_EC_AUDIO_LVL_MSK);
  6344. seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW)));
  6345. if (volume_control_allowed) {
  6346. seq_printf(m, "commands:\tunmute, mute\n");
  6347. if (!tp_features.mixer_no_level_control) {
  6348. seq_printf(m, "commands:\tup, down\n");
  6349. seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
  6350. TP_EC_VOLUME_MAX);
  6351. }
  6352. }
  6353. }
  6354. return 0;
  6355. }
  6356. static int volume_write(char *buf)
  6357. {
  6358. u8 s;
  6359. u8 new_level, new_mute;
  6360. int l;
  6361. char *cmd;
  6362. int rc;
  6363. /*
  6364. * We do allow volume control at driver startup, so that the
  6365. * user can set initial state through the volume=... parameter hack.
  6366. */
  6367. if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
  6368. if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
  6369. tp_warned.volume_ctrl_forbidden = 1;
  6370. pr_notice("Console audio control in monitor mode, changes are not allowed\n");
  6371. pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
  6372. }
  6373. return -EPERM;
  6374. }
  6375. rc = volume_get_status(&s);
  6376. if (rc < 0)
  6377. return rc;
  6378. new_level = s & TP_EC_AUDIO_LVL_MSK;
  6379. new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
  6380. while ((cmd = strsep(&buf, ","))) {
  6381. if (!tp_features.mixer_no_level_control) {
  6382. if (strstarts(cmd, "up")) {
  6383. if (new_mute)
  6384. new_mute = 0;
  6385. else if (new_level < TP_EC_VOLUME_MAX)
  6386. new_level++;
  6387. continue;
  6388. } else if (strstarts(cmd, "down")) {
  6389. if (new_mute)
  6390. new_mute = 0;
  6391. else if (new_level > 0)
  6392. new_level--;
  6393. continue;
  6394. } else if (sscanf(cmd, "level %u", &l) == 1 &&
  6395. l >= 0 && l <= TP_EC_VOLUME_MAX) {
  6396. new_level = l;
  6397. continue;
  6398. }
  6399. }
  6400. if (strstarts(cmd, "mute"))
  6401. new_mute = TP_EC_AUDIO_MUTESW_MSK;
  6402. else if (strstarts(cmd, "unmute"))
  6403. new_mute = 0;
  6404. else
  6405. return -EINVAL;
  6406. }
  6407. if (tp_features.mixer_no_level_control) {
  6408. tpacpi_disclose_usertask("procfs volume", "%smute\n",
  6409. new_mute ? "" : "un");
  6410. rc = volume_set_mute(!!new_mute);
  6411. } else {
  6412. tpacpi_disclose_usertask("procfs volume",
  6413. "%smute and set level to %d\n",
  6414. new_mute ? "" : "un", new_level);
  6415. rc = volume_set_status(new_mute | new_level);
  6416. }
  6417. volume_alsa_notify_change();
  6418. return (rc == -EINTR) ? -ERESTARTSYS : rc;
  6419. }
  6420. static struct ibm_struct volume_driver_data = {
  6421. .name = "volume",
  6422. .read = volume_read,
  6423. .write = volume_write,
  6424. .exit = volume_exit,
  6425. .suspend = volume_suspend,
  6426. .resume = volume_resume,
  6427. .shutdown = volume_shutdown,
  6428. };
  6429. #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  6430. #define alsa_card NULL
  6431. static inline void volume_alsa_notify_change(void)
  6432. {
  6433. }
  6434. static int __init volume_init(struct ibm_init_struct *iibm)
  6435. {
  6436. pr_info("volume: disabled as there is no ALSA support in this kernel\n");
  6437. return -ENODEV;
  6438. }
  6439. static struct ibm_struct volume_driver_data = {
  6440. .name = "volume",
  6441. };
  6442. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  6443. /*************************************************************************
  6444. * Fan subdriver
  6445. */
  6446. /*
  6447. * FAN ACCESS MODES
  6448. *
  6449. * TPACPI_FAN_RD_ACPI_GFAN:
  6450. * ACPI GFAN method: returns fan level
  6451. *
  6452. * see TPACPI_FAN_WR_ACPI_SFAN
  6453. * EC 0x2f (HFSP) not available if GFAN exists
  6454. *
  6455. * TPACPI_FAN_WR_ACPI_SFAN:
  6456. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  6457. *
  6458. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  6459. * it for writing.
  6460. *
  6461. * TPACPI_FAN_RD_ACPI_FANG:
  6462. * ACPI FANG method: returns fan control register
  6463. *
  6464. * Takes one parameter which is 0x8100 plus the offset to EC memory
  6465. * address 0xf500 and returns the byte at this address.
  6466. *
  6467. * 0xf500:
  6468. * When the value is less than 9 automatic mode is enabled
  6469. * 0xf502:
  6470. * Contains the current fan speed from 0-100%
  6471. * 0xf506:
  6472. * Bit 7 has to be set in order to enable manual control by
  6473. * writing a value >= 9 to 0xf500
  6474. *
  6475. * TPACPI_FAN_WR_ACPI_FANW:
  6476. * ACPI FANW method: sets fan control registers
  6477. *
  6478. * Takes 0x8100 plus the offset to EC memory address 0xf500 and the
  6479. * value to be written there as parameters.
  6480. *
  6481. * see TPACPI_FAN_RD_ACPI_FANG
  6482. *
  6483. * TPACPI_FAN_WR_TPEC:
  6484. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  6485. * Supported on almost all ThinkPads
  6486. *
  6487. * Fan speed changes of any sort (including those caused by the
  6488. * disengaged mode) are usually done slowly by the firmware as the
  6489. * maximum amount of fan duty cycle change per second seems to be
  6490. * limited.
  6491. *
  6492. * Reading is not available if GFAN exists.
  6493. * Writing is not available if SFAN exists.
  6494. *
  6495. * Bits
  6496. * 7 automatic mode engaged;
  6497. * (default operation mode of the ThinkPad)
  6498. * fan level is ignored in this mode.
  6499. * 6 full speed mode (takes precedence over bit 7);
  6500. * not available on all thinkpads. May disable
  6501. * the tachometer while the fan controller ramps up
  6502. * the speed (which can take up to a few *minutes*).
  6503. * Speeds up fan to 100% duty-cycle, which is far above
  6504. * the standard RPM levels. It is not impossible that
  6505. * it could cause hardware damage.
  6506. * 5-3 unused in some models. Extra bits for fan level
  6507. * in others, but still useless as all values above
  6508. * 7 map to the same speed as level 7 in these models.
  6509. * 2-0 fan level (0..7 usually)
  6510. * 0x00 = stop
  6511. * 0x07 = max (set when temperatures critical)
  6512. * Some ThinkPads may have other levels, see
  6513. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  6514. *
  6515. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  6516. * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
  6517. * does so, its initial value is meaningless (0x07).
  6518. *
  6519. * For firmware bugs, refer to:
  6520. * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  6521. *
  6522. * ----
  6523. *
  6524. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  6525. * Main fan tachometer reading (in RPM)
  6526. *
  6527. * This register is present on all ThinkPads with a new-style EC, and
  6528. * it is known not to be present on the A21m/e, and T22, as there is
  6529. * something else in offset 0x84 according to the ACPI DSDT. Other
  6530. * ThinkPads from this same time period (and earlier) probably lack the
  6531. * tachometer as well.
  6532. *
  6533. * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
  6534. * was never fixed by IBM to report the EC firmware version string
  6535. * probably support the tachometer (like the early X models), so
  6536. * detecting it is quite hard. We need more data to know for sure.
  6537. *
  6538. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  6539. * might result.
  6540. *
  6541. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  6542. * mode.
  6543. *
  6544. * For firmware bugs, refer to:
  6545. * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  6546. *
  6547. * ----
  6548. *
  6549. * ThinkPad EC register 0x31 bit 0 (only on select models)
  6550. *
  6551. * When bit 0 of EC register 0x31 is zero, the tachometer registers
  6552. * show the speed of the main fan. When bit 0 of EC register 0x31
  6553. * is one, the tachometer registers show the speed of the auxiliary
  6554. * fan.
  6555. *
  6556. * Fan control seems to affect both fans, regardless of the state
  6557. * of this bit.
  6558. *
  6559. * So far, only the firmware for the X60/X61 non-tablet versions
  6560. * seem to support this (firmware TP-7M).
  6561. *
  6562. * TPACPI_FAN_WR_ACPI_FANS:
  6563. * ThinkPad X31, X40, X41. Not available in the X60.
  6564. *
  6565. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  6566. * high speed. ACPI DSDT seems to map these three speeds to levels
  6567. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  6568. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  6569. *
  6570. * The speeds are stored on handles
  6571. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  6572. *
  6573. * There are three default speed sets, accessible as handles:
  6574. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  6575. *
  6576. * ACPI DSDT switches which set is in use depending on various
  6577. * factors.
  6578. *
  6579. * TPACPI_FAN_WR_TPEC is also available and should be used to
  6580. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  6581. * but the ACPI tables just mention level 7.
  6582. *
  6583. * TPACPI_FAN_RD_TPEC_NS:
  6584. * This mode is used for a few ThinkPads (L13 Yoga Gen2, X13 Yoga Gen2 etc.)
  6585. * that are using non-standard EC locations for reporting fan speeds.
  6586. * Currently these platforms only provide fan rpm reporting.
  6587. *
  6588. */
  6589. #define FAN_RPM_CAL_CONST 491520 /* FAN RPM calculation offset for some non-standard ECFW */
  6590. #define FAN_NS_CTRL_STATUS BIT(2) /* Bit which determines control is enabled or not */
  6591. #define FAN_NS_CTRL BIT(4) /* Bit which determines control is by host or EC */
  6592. #define FAN_CLOCK_TPM (22500*60) /* Ticks per minute for a 22.5 kHz clock */
  6593. enum { /* Fan control constants */
  6594. fan_status_offset = 0x2f, /* EC register 0x2f */
  6595. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  6596. * 0x84 must be read before 0x85 */
  6597. fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
  6598. bit 0 selects which fan is active */
  6599. fan_status_offset_ns = 0x93, /* Special status/control offset for non-standard EC Fan1 */
  6600. fan2_status_offset_ns = 0x96, /* Special status/control offset for non-standard EC Fan2 */
  6601. fan_rpm_status_ns = 0x95, /* Special offset for Fan1 RPM status for non-standard EC */
  6602. fan2_rpm_status_ns = 0x98, /* Special offset for Fan2 RPM status for non-standard EC */
  6603. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  6604. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  6605. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  6606. };
  6607. enum fan_status_access_mode {
  6608. TPACPI_FAN_NONE = 0, /* No fan status or control */
  6609. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  6610. TPACPI_FAN_RD_ACPI_FANG, /* Use ACPI FANG */
  6611. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  6612. TPACPI_FAN_RD_TPEC_NS, /* Use non-standard ACPI EC regs (eg: L13 Yoga gen2 etc.) */
  6613. };
  6614. enum fan_control_access_mode {
  6615. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  6616. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  6617. TPACPI_FAN_WR_ACPI_FANW, /* Use ACPI FANW */
  6618. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  6619. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  6620. };
  6621. enum fan_control_commands {
  6622. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  6623. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  6624. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  6625. * and also watchdog cmd */
  6626. };
  6627. static bool fan_control_allowed;
  6628. static enum fan_status_access_mode fan_status_access_mode;
  6629. static enum fan_control_access_mode fan_control_access_mode;
  6630. static enum fan_control_commands fan_control_commands;
  6631. static u8 fan_control_initial_status;
  6632. static u8 fan_control_desired_level;
  6633. static u8 fan_control_resume_level;
  6634. static int fan_watchdog_maxinterval;
  6635. static bool fan_with_ns_addr;
  6636. static bool ecfw_with_fan_dec_rpm;
  6637. static bool fan_speed_in_tpr;
  6638. static struct mutex fan_mutex;
  6639. static void fan_watchdog_fire(struct work_struct *ignored);
  6640. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  6641. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  6642. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  6643. "\\FSPD", /* 600e/x, 770e, 770x */
  6644. ); /* all others */
  6645. TPACPI_HANDLE(fang, ec, "FANG", /* E531 */
  6646. ); /* all others */
  6647. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  6648. "JFNS", /* 770x-JL */
  6649. ); /* all others */
  6650. TPACPI_HANDLE(fanw, ec, "FANW", /* E531 */
  6651. ); /* all others */
  6652. /*
  6653. * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
  6654. * HFSP register at boot, so it contains 0x07 but the Thinkpad could
  6655. * be in auto mode (0x80).
  6656. *
  6657. * This is corrected by any write to HFSP either by the driver, or
  6658. * by the firmware.
  6659. *
  6660. * We assume 0x07 really means auto mode while this quirk is active,
  6661. * as this is far more likely than the ThinkPad being in level 7,
  6662. * which is only used by the firmware during thermal emergencies.
  6663. *
  6664. * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
  6665. * TP-70 (T43, R52), which are known to be buggy.
  6666. */
  6667. static void fan_quirk1_setup(void)
  6668. {
  6669. if (fan_control_initial_status == 0x07) {
  6670. pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
  6671. tp_features.fan_ctrl_status_undef = 1;
  6672. }
  6673. }
  6674. static void fan_quirk1_handle(u8 *fan_status)
  6675. {
  6676. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  6677. if (*fan_status != fan_control_initial_status) {
  6678. /* something changed the HFSP regisnter since
  6679. * driver init time, so it is not undefined
  6680. * anymore */
  6681. tp_features.fan_ctrl_status_undef = 0;
  6682. } else {
  6683. /* Return most likely status. In fact, it
  6684. * might be the only possible status */
  6685. *fan_status = TP_EC_FAN_AUTO;
  6686. }
  6687. }
  6688. }
  6689. /* Select main fan on X60/X61, NOOP on others */
  6690. static bool fan_select_fan1(void)
  6691. {
  6692. if (tp_features.second_fan) {
  6693. u8 val;
  6694. if (ec_read(fan_select_offset, &val) < 0)
  6695. return false;
  6696. val &= 0xFEU;
  6697. if (ec_write(fan_select_offset, val) < 0)
  6698. return false;
  6699. }
  6700. return true;
  6701. }
  6702. /* Select secondary fan on X60/X61 */
  6703. static bool fan_select_fan2(void)
  6704. {
  6705. u8 val;
  6706. if (!tp_features.second_fan)
  6707. return false;
  6708. if (ec_read(fan_select_offset, &val) < 0)
  6709. return false;
  6710. val |= 0x01U;
  6711. if (ec_write(fan_select_offset, val) < 0)
  6712. return false;
  6713. return true;
  6714. }
  6715. static void fan_update_desired_level(u8 status)
  6716. {
  6717. lockdep_assert_held(&fan_mutex);
  6718. if ((status &
  6719. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  6720. if (status > 7)
  6721. fan_control_desired_level = 7;
  6722. else
  6723. fan_control_desired_level = status;
  6724. }
  6725. }
  6726. static int fan_get_status(u8 *status)
  6727. {
  6728. u8 s;
  6729. /* TODO:
  6730. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  6731. switch (fan_status_access_mode) {
  6732. case TPACPI_FAN_RD_ACPI_GFAN: {
  6733. /* 570, 600e/x, 770e, 770x */
  6734. int res;
  6735. if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
  6736. return -EIO;
  6737. if (likely(status))
  6738. *status = res & 0x07;
  6739. break;
  6740. }
  6741. case TPACPI_FAN_RD_ACPI_FANG: {
  6742. /* E531 */
  6743. int mode, speed;
  6744. if (unlikely(!acpi_evalf(fang_handle, &mode, NULL, "dd", 0x8100)))
  6745. return -EIO;
  6746. if (unlikely(!acpi_evalf(fang_handle, &speed, NULL, "dd", 0x8102)))
  6747. return -EIO;
  6748. if (likely(status)) {
  6749. *status = speed * 7 / 100;
  6750. if (mode < 9)
  6751. *status |= TP_EC_FAN_AUTO;
  6752. }
  6753. break;
  6754. }
  6755. case TPACPI_FAN_RD_TPEC:
  6756. /* all except 570, 600e/x, 770e, 770x */
  6757. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  6758. return -EIO;
  6759. if (likely(status)) {
  6760. *status = s;
  6761. fan_quirk1_handle(status);
  6762. }
  6763. break;
  6764. case TPACPI_FAN_RD_TPEC_NS:
  6765. /* Default mode is AUTO which means controlled by EC */
  6766. if (!acpi_ec_read(fan_status_offset_ns, &s))
  6767. return -EIO;
  6768. if (status)
  6769. *status = s;
  6770. break;
  6771. default:
  6772. return -ENXIO;
  6773. }
  6774. return 0;
  6775. }
  6776. static int fan_get_status_safe(u8 *status)
  6777. {
  6778. int rc;
  6779. u8 s;
  6780. if (mutex_lock_killable(&fan_mutex))
  6781. return -ERESTARTSYS;
  6782. rc = fan_get_status(&s);
  6783. /* NS EC doesn't have register with level settings */
  6784. if (!rc && !fan_with_ns_addr)
  6785. fan_update_desired_level(s);
  6786. mutex_unlock(&fan_mutex);
  6787. if (rc)
  6788. return rc;
  6789. if (status)
  6790. *status = s;
  6791. return 0;
  6792. }
  6793. static int fan_get_speed(unsigned int *speed)
  6794. {
  6795. u8 hi, lo;
  6796. switch (fan_status_access_mode) {
  6797. case TPACPI_FAN_RD_TPEC:
  6798. /* all except 570, 600e/x, 770e, 770x */
  6799. if (unlikely(!fan_select_fan1()))
  6800. return -EIO;
  6801. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  6802. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  6803. return -EIO;
  6804. if (likely(speed)) {
  6805. *speed = (hi << 8) | lo;
  6806. if (fan_speed_in_tpr && *speed != 0)
  6807. *speed = FAN_CLOCK_TPM / *speed;
  6808. }
  6809. break;
  6810. case TPACPI_FAN_RD_TPEC_NS:
  6811. if (!acpi_ec_read(fan_rpm_status_ns, &lo))
  6812. return -EIO;
  6813. if (speed)
  6814. *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
  6815. break;
  6816. default:
  6817. return -ENXIO;
  6818. }
  6819. return 0;
  6820. }
  6821. static int fan2_get_speed(unsigned int *speed)
  6822. {
  6823. u8 hi, lo, status;
  6824. bool rc;
  6825. switch (fan_status_access_mode) {
  6826. case TPACPI_FAN_RD_TPEC:
  6827. /* all except 570, 600e/x, 770e, 770x */
  6828. if (unlikely(!fan_select_fan2()))
  6829. return -EIO;
  6830. rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
  6831. !acpi_ec_read(fan_rpm_offset + 1, &hi);
  6832. fan_select_fan1(); /* play it safe */
  6833. if (rc)
  6834. return -EIO;
  6835. if (likely(speed)) {
  6836. *speed = (hi << 8) | lo;
  6837. if (fan_speed_in_tpr && *speed != 0)
  6838. *speed = FAN_CLOCK_TPM / *speed;
  6839. }
  6840. break;
  6841. case TPACPI_FAN_RD_TPEC_NS:
  6842. rc = !acpi_ec_read(fan2_status_offset_ns, &status);
  6843. if (rc)
  6844. return -EIO;
  6845. if (!(status & FAN_NS_CTRL_STATUS)) {
  6846. pr_info("secondary fan control not supported\n");
  6847. return -EIO;
  6848. }
  6849. rc = !acpi_ec_read(fan2_rpm_status_ns, &lo);
  6850. if (rc)
  6851. return -EIO;
  6852. if (speed)
  6853. *speed = lo ? FAN_RPM_CAL_CONST / lo : 0;
  6854. break;
  6855. case TPACPI_FAN_RD_ACPI_FANG: {
  6856. /* E531 */
  6857. int speed_tmp;
  6858. if (unlikely(!acpi_evalf(fang_handle, &speed_tmp, NULL, "dd", 0x8102)))
  6859. return -EIO;
  6860. if (likely(speed))
  6861. *speed = speed_tmp * 65535 / 100;
  6862. break;
  6863. }
  6864. default:
  6865. return -ENXIO;
  6866. }
  6867. return 0;
  6868. }
  6869. static int fan_set_level(int level)
  6870. {
  6871. if (!fan_control_allowed)
  6872. return -EPERM;
  6873. switch (fan_control_access_mode) {
  6874. case TPACPI_FAN_WR_ACPI_SFAN:
  6875. if ((level < 0) || (level > 7))
  6876. return -EINVAL;
  6877. if (tp_features.second_fan_ctl) {
  6878. if (!fan_select_fan2() ||
  6879. !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) {
  6880. pr_warn("Couldn't set 2nd fan level, disabling support\n");
  6881. tp_features.second_fan_ctl = 0;
  6882. }
  6883. fan_select_fan1();
  6884. }
  6885. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  6886. return -EIO;
  6887. break;
  6888. case TPACPI_FAN_WR_ACPI_FANS:
  6889. case TPACPI_FAN_WR_TPEC:
  6890. if (!(level & TP_EC_FAN_AUTO) &&
  6891. !(level & TP_EC_FAN_FULLSPEED) &&
  6892. ((level < 0) || (level > 7)))
  6893. return -EINVAL;
  6894. /* safety net should the EC not support AUTO
  6895. * or FULLSPEED mode bits and just ignore them */
  6896. if (level & TP_EC_FAN_FULLSPEED)
  6897. level |= 7; /* safety min speed 7 */
  6898. else if (level & TP_EC_FAN_AUTO)
  6899. level |= 4; /* safety min speed 4 */
  6900. if (tp_features.second_fan_ctl) {
  6901. if (!fan_select_fan2() ||
  6902. !acpi_ec_write(fan_status_offset, level)) {
  6903. pr_warn("Couldn't set 2nd fan level, disabling support\n");
  6904. tp_features.second_fan_ctl = 0;
  6905. }
  6906. fan_select_fan1();
  6907. }
  6908. if (!acpi_ec_write(fan_status_offset, level))
  6909. return -EIO;
  6910. else
  6911. tp_features.fan_ctrl_status_undef = 0;
  6912. break;
  6913. case TPACPI_FAN_WR_ACPI_FANW:
  6914. if (!(level & TP_EC_FAN_AUTO) && (level < 0 || level > 7))
  6915. return -EINVAL;
  6916. if (level & TP_EC_FAN_FULLSPEED)
  6917. return -EINVAL;
  6918. if (level & TP_EC_FAN_AUTO) {
  6919. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x05)) {
  6920. return -EIO;
  6921. }
  6922. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0x00)) {
  6923. return -EIO;
  6924. }
  6925. } else {
  6926. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
  6927. return -EIO;
  6928. }
  6929. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
  6930. return -EIO;
  6931. }
  6932. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8102, level * 100 / 7)) {
  6933. return -EIO;
  6934. }
  6935. }
  6936. break;
  6937. default:
  6938. return -ENXIO;
  6939. }
  6940. vdbg_printk(TPACPI_DBG_FAN,
  6941. "fan control: set fan control register to 0x%02x\n", level);
  6942. return 0;
  6943. }
  6944. static int fan_set_level_safe(int level)
  6945. {
  6946. int rc;
  6947. if (!fan_control_allowed)
  6948. return -EPERM;
  6949. if (mutex_lock_killable(&fan_mutex))
  6950. return -ERESTARTSYS;
  6951. if (level == TPACPI_FAN_LAST_LEVEL)
  6952. level = fan_control_desired_level;
  6953. rc = fan_set_level(level);
  6954. if (!rc)
  6955. fan_update_desired_level(level);
  6956. mutex_unlock(&fan_mutex);
  6957. return rc;
  6958. }
  6959. static int fan_set_enable(void)
  6960. {
  6961. u8 s = 0;
  6962. int rc;
  6963. if (!fan_control_allowed)
  6964. return -EPERM;
  6965. if (mutex_lock_killable(&fan_mutex))
  6966. return -ERESTARTSYS;
  6967. switch (fan_control_access_mode) {
  6968. case TPACPI_FAN_WR_ACPI_FANS:
  6969. case TPACPI_FAN_WR_TPEC:
  6970. rc = fan_get_status(&s);
  6971. if (rc)
  6972. break;
  6973. /* Don't go out of emergency fan mode */
  6974. if (s != 7) {
  6975. s &= 0x07;
  6976. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  6977. }
  6978. if (!acpi_ec_write(fan_status_offset, s))
  6979. rc = -EIO;
  6980. else {
  6981. tp_features.fan_ctrl_status_undef = 0;
  6982. rc = 0;
  6983. }
  6984. break;
  6985. case TPACPI_FAN_WR_ACPI_SFAN:
  6986. rc = fan_get_status(&s);
  6987. if (rc)
  6988. break;
  6989. s &= 0x07;
  6990. /* Set fan to at least level 4 */
  6991. s |= 4;
  6992. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  6993. rc = -EIO;
  6994. else
  6995. rc = 0;
  6996. break;
  6997. case TPACPI_FAN_WR_ACPI_FANW:
  6998. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x05)) {
  6999. rc = -EIO;
  7000. break;
  7001. }
  7002. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0x00)) {
  7003. rc = -EIO;
  7004. break;
  7005. }
  7006. rc = 0;
  7007. break;
  7008. default:
  7009. rc = -ENXIO;
  7010. }
  7011. mutex_unlock(&fan_mutex);
  7012. if (!rc)
  7013. vdbg_printk(TPACPI_DBG_FAN,
  7014. "fan control: set fan control register to 0x%02x\n",
  7015. s);
  7016. return rc;
  7017. }
  7018. static int fan_set_disable(void)
  7019. {
  7020. int rc;
  7021. if (!fan_control_allowed)
  7022. return -EPERM;
  7023. if (mutex_lock_killable(&fan_mutex))
  7024. return -ERESTARTSYS;
  7025. rc = 0;
  7026. switch (fan_control_access_mode) {
  7027. case TPACPI_FAN_WR_ACPI_FANS:
  7028. case TPACPI_FAN_WR_TPEC:
  7029. if (!acpi_ec_write(fan_status_offset, 0x00))
  7030. rc = -EIO;
  7031. else {
  7032. fan_control_desired_level = 0;
  7033. tp_features.fan_ctrl_status_undef = 0;
  7034. }
  7035. break;
  7036. case TPACPI_FAN_WR_ACPI_SFAN:
  7037. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  7038. rc = -EIO;
  7039. else
  7040. fan_control_desired_level = 0;
  7041. break;
  7042. case TPACPI_FAN_WR_ACPI_FANW:
  7043. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
  7044. rc = -EIO;
  7045. break;
  7046. }
  7047. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
  7048. rc = -EIO;
  7049. break;
  7050. }
  7051. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8102, 0x00)) {
  7052. rc = -EIO;
  7053. break;
  7054. }
  7055. rc = 0;
  7056. break;
  7057. default:
  7058. rc = -ENXIO;
  7059. }
  7060. if (!rc)
  7061. vdbg_printk(TPACPI_DBG_FAN,
  7062. "fan control: set fan control register to 0\n");
  7063. mutex_unlock(&fan_mutex);
  7064. return rc;
  7065. }
  7066. static int fan_set_speed(int speed)
  7067. {
  7068. int rc;
  7069. if (!fan_control_allowed)
  7070. return -EPERM;
  7071. if (mutex_lock_killable(&fan_mutex))
  7072. return -ERESTARTSYS;
  7073. rc = 0;
  7074. switch (fan_control_access_mode) {
  7075. case TPACPI_FAN_WR_ACPI_FANS:
  7076. if (speed >= 0 && speed <= 65535) {
  7077. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  7078. speed, speed, speed))
  7079. rc = -EIO;
  7080. } else
  7081. rc = -EINVAL;
  7082. break;
  7083. case TPACPI_FAN_WR_ACPI_FANW:
  7084. if (speed >= 0 && speed <= 65535) {
  7085. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8106, 0x45)) {
  7086. rc = -EIO;
  7087. break;
  7088. }
  7089. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd", 0x8100, 0xff)) {
  7090. rc = -EIO;
  7091. break;
  7092. }
  7093. if (!acpi_evalf(fanw_handle, NULL, NULL, "vdd",
  7094. 0x8102, speed * 100 / 65535))
  7095. rc = -EIO;
  7096. } else
  7097. rc = -EINVAL;
  7098. break;
  7099. default:
  7100. rc = -ENXIO;
  7101. }
  7102. mutex_unlock(&fan_mutex);
  7103. return rc;
  7104. }
  7105. static void fan_watchdog_reset(void)
  7106. {
  7107. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7108. return;
  7109. if (fan_watchdog_maxinterval > 0 &&
  7110. tpacpi_lifecycle != TPACPI_LIFE_EXITING)
  7111. mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
  7112. msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
  7113. else
  7114. cancel_delayed_work(&fan_watchdog_task);
  7115. }
  7116. static void fan_watchdog_fire(struct work_struct *ignored)
  7117. {
  7118. int rc;
  7119. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  7120. return;
  7121. pr_notice("fan watchdog: enabling fan\n");
  7122. rc = fan_set_enable();
  7123. if (rc < 0) {
  7124. pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
  7125. rc);
  7126. /* reschedule for later */
  7127. fan_watchdog_reset();
  7128. }
  7129. }
  7130. /*
  7131. * SYSFS fan layout: hwmon compatible (device)
  7132. *
  7133. * pwm*_enable:
  7134. * 0: "disengaged" mode
  7135. * 1: manual mode
  7136. * 2: native EC "auto" mode (recommended, hardware default)
  7137. *
  7138. * pwm*: set speed in manual mode, ignored otherwise.
  7139. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  7140. * interpolation.
  7141. *
  7142. * fan*_input: tachometer reading, RPM
  7143. *
  7144. *
  7145. * SYSFS fan layout: extensions
  7146. *
  7147. * fan_watchdog (driver):
  7148. * fan watchdog interval in seconds, 0 disables (default), max 120
  7149. */
  7150. /* sysfs fan pwm1_enable ----------------------------------------------- */
  7151. static ssize_t fan_pwm1_enable_show(struct device *dev,
  7152. struct device_attribute *attr,
  7153. char *buf)
  7154. {
  7155. int res, mode;
  7156. u8 status;
  7157. res = fan_get_status_safe(&status);
  7158. if (res)
  7159. return res;
  7160. if (status & TP_EC_FAN_FULLSPEED) {
  7161. mode = 0;
  7162. } else if (status & TP_EC_FAN_AUTO) {
  7163. mode = 2;
  7164. } else
  7165. mode = 1;
  7166. return sysfs_emit(buf, "%d\n", mode);
  7167. }
  7168. static ssize_t fan_pwm1_enable_store(struct device *dev,
  7169. struct device_attribute *attr,
  7170. const char *buf, size_t count)
  7171. {
  7172. unsigned long t;
  7173. int res, level;
  7174. if (parse_strtoul(buf, 2, &t))
  7175. return -EINVAL;
  7176. tpacpi_disclose_usertask("hwmon pwm1_enable",
  7177. "set fan mode to %lu\n", t);
  7178. switch (t) {
  7179. case 0:
  7180. level = TP_EC_FAN_FULLSPEED;
  7181. break;
  7182. case 1:
  7183. level = TPACPI_FAN_LAST_LEVEL;
  7184. break;
  7185. case 2:
  7186. level = TP_EC_FAN_AUTO;
  7187. break;
  7188. case 3:
  7189. /* reserved for software-controlled auto mode */
  7190. return -ENOSYS;
  7191. default:
  7192. return -EINVAL;
  7193. }
  7194. res = fan_set_level_safe(level);
  7195. if (res == -ENXIO)
  7196. return -EINVAL;
  7197. else if (res < 0)
  7198. return res;
  7199. fan_watchdog_reset();
  7200. return count;
  7201. }
  7202. static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  7203. fan_pwm1_enable_show, fan_pwm1_enable_store);
  7204. /* sysfs fan pwm1 ------------------------------------------------------ */
  7205. static ssize_t fan_pwm1_show(struct device *dev,
  7206. struct device_attribute *attr,
  7207. char *buf)
  7208. {
  7209. int res;
  7210. u8 status;
  7211. res = fan_get_status_safe(&status);
  7212. if (res)
  7213. return res;
  7214. if ((status &
  7215. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  7216. status = fan_control_desired_level;
  7217. if (status > 7)
  7218. status = 7;
  7219. return sysfs_emit(buf, "%u\n", (status * 255) / 7);
  7220. }
  7221. static ssize_t fan_pwm1_store(struct device *dev,
  7222. struct device_attribute *attr,
  7223. const char *buf, size_t count)
  7224. {
  7225. unsigned long s;
  7226. int rc;
  7227. u8 status, newlevel;
  7228. if (parse_strtoul(buf, 255, &s))
  7229. return -EINVAL;
  7230. tpacpi_disclose_usertask("hwmon pwm1",
  7231. "set fan speed to %lu\n", s);
  7232. /* scale down from 0-255 to 0-7 */
  7233. newlevel = (s >> 5) & 0x07;
  7234. if (mutex_lock_killable(&fan_mutex))
  7235. return -ERESTARTSYS;
  7236. rc = fan_get_status(&status);
  7237. if (!rc && (status &
  7238. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  7239. rc = fan_set_level(newlevel);
  7240. if (rc == -ENXIO)
  7241. rc = -EINVAL;
  7242. else if (!rc) {
  7243. fan_update_desired_level(newlevel);
  7244. fan_watchdog_reset();
  7245. }
  7246. }
  7247. mutex_unlock(&fan_mutex);
  7248. return (rc) ? rc : count;
  7249. }
  7250. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
  7251. /* sysfs fan fan1_input ------------------------------------------------ */
  7252. static ssize_t fan_fan1_input_show(struct device *dev,
  7253. struct device_attribute *attr,
  7254. char *buf)
  7255. {
  7256. int res;
  7257. unsigned int speed;
  7258. res = fan_get_speed(&speed);
  7259. if (res < 0)
  7260. return res;
  7261. /* Check for fan speeds displayed in hexadecimal */
  7262. if (!ecfw_with_fan_dec_rpm)
  7263. return sysfs_emit(buf, "%u\n", speed);
  7264. else
  7265. return sysfs_emit(buf, "%x\n", speed);
  7266. }
  7267. static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
  7268. /* sysfs fan fan2_input ------------------------------------------------ */
  7269. static ssize_t fan_fan2_input_show(struct device *dev,
  7270. struct device_attribute *attr,
  7271. char *buf)
  7272. {
  7273. int res;
  7274. unsigned int speed;
  7275. res = fan2_get_speed(&speed);
  7276. if (res < 0)
  7277. return res;
  7278. /* Check for fan speeds displayed in hexadecimal */
  7279. if (!ecfw_with_fan_dec_rpm)
  7280. return sysfs_emit(buf, "%u\n", speed);
  7281. else
  7282. return sysfs_emit(buf, "%x\n", speed);
  7283. }
  7284. static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
  7285. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  7286. static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
  7287. {
  7288. return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval);
  7289. }
  7290. static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
  7291. size_t count)
  7292. {
  7293. unsigned long t;
  7294. if (parse_strtoul(buf, 120, &t))
  7295. return -EINVAL;
  7296. if (!fan_control_allowed)
  7297. return -EPERM;
  7298. fan_watchdog_maxinterval = t;
  7299. fan_watchdog_reset();
  7300. tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
  7301. return count;
  7302. }
  7303. static DRIVER_ATTR_RW(fan_watchdog);
  7304. /* --------------------------------------------------------------------- */
  7305. static struct attribute *fan_attributes[] = {
  7306. &dev_attr_pwm1_enable.attr,
  7307. &dev_attr_pwm1.attr,
  7308. &dev_attr_fan1_input.attr,
  7309. &dev_attr_fan2_input.attr,
  7310. NULL
  7311. };
  7312. static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  7313. int n)
  7314. {
  7315. if (fan_status_access_mode == TPACPI_FAN_NONE &&
  7316. fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7317. return 0;
  7318. if (attr == &dev_attr_fan2_input.attr) {
  7319. if (!tp_features.second_fan)
  7320. return 0;
  7321. }
  7322. return attr->mode;
  7323. }
  7324. static const struct attribute_group fan_attr_group = {
  7325. .is_visible = fan_attr_is_visible,
  7326. .attrs = fan_attributes,
  7327. };
  7328. static struct attribute *fan_driver_attributes[] = {
  7329. &driver_attr_fan_watchdog.attr,
  7330. NULL
  7331. };
  7332. static const struct attribute_group fan_driver_attr_group = {
  7333. .is_visible = fan_attr_is_visible,
  7334. .attrs = fan_driver_attributes,
  7335. };
  7336. #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */
  7337. #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
  7338. #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */
  7339. #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */
  7340. #define TPACPI_FAN_NS 0x0010 /* For EC with non-Standard register addresses */
  7341. #define TPACPI_FAN_DECRPM 0x0020 /* For ECFW's with RPM in register as decimal */
  7342. #define TPACPI_FAN_TPR 0x0040 /* Fan speed is in Ticks Per Revolution */
  7343. #define TPACPI_FAN_NOACPI 0x0080 /* Don't use ACPI methods even if detected */
  7344. static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
  7345. TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
  7346. TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
  7347. TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
  7348. TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
  7349. TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
  7350. TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
  7351. TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */
  7352. TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */
  7353. TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */
  7354. TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */
  7355. TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */
  7356. TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */
  7357. TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */
  7358. TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */
  7359. TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */
  7360. TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */
  7361. TPACPI_Q_LNV3('R', '1', 'F', TPACPI_FAN_NS), /* L13 Yoga Gen 2 */
  7362. TPACPI_Q_LNV3('N', '2', 'U', TPACPI_FAN_NS), /* X13 Yoga Gen 2*/
  7363. TPACPI_Q_LNV3('R', '0', 'R', TPACPI_FAN_NS), /* L380 */
  7364. TPACPI_Q_LNV3('R', '1', '5', TPACPI_FAN_NS), /* L13 Yoga Gen 1 */
  7365. TPACPI_Q_LNV3('R', '1', '0', TPACPI_FAN_NS), /* L390 */
  7366. TPACPI_Q_LNV3('N', '2', 'L', TPACPI_FAN_NS), /* X13 Yoga Gen 1 */
  7367. TPACPI_Q_LNV3('R', '0', 'T', TPACPI_FAN_NS), /* 11e Gen5 GL */
  7368. TPACPI_Q_LNV3('R', '1', 'D', TPACPI_FAN_NS), /* 11e Gen5 GL-R */
  7369. TPACPI_Q_LNV3('R', '0', 'V', TPACPI_FAN_NS), /* 11e Gen5 KL-Y */
  7370. TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */
  7371. TPACPI_Q_LNV3('R', '0', 'Q', TPACPI_FAN_DECRPM),/* L480 */
  7372. TPACPI_Q_LNV('8', 'F', TPACPI_FAN_TPR), /* ThinkPad x120e */
  7373. TPACPI_Q_LNV3('R', '0', '0', TPACPI_FAN_NOACPI),/* E560 */
  7374. TPACPI_Q_LNV3('R', '1', '2', TPACPI_FAN_NOACPI),/* T495 */
  7375. TPACPI_Q_LNV3('R', '1', '3', TPACPI_FAN_NOACPI),/* T495s */
  7376. };
  7377. static int __init fan_init(struct ibm_init_struct *iibm)
  7378. {
  7379. unsigned long quirks;
  7380. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7381. "initializing fan subdriver\n");
  7382. mutex_init(&fan_mutex);
  7383. fan_status_access_mode = TPACPI_FAN_NONE;
  7384. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  7385. fan_control_commands = 0;
  7386. fan_watchdog_maxinterval = 0;
  7387. tp_features.fan_ctrl_status_undef = 0;
  7388. tp_features.second_fan = 0;
  7389. tp_features.second_fan_ctl = 0;
  7390. fan_control_desired_level = 7;
  7391. if (tpacpi_is_ibm()) {
  7392. TPACPI_ACPIHANDLE_INIT(fans);
  7393. TPACPI_ACPIHANDLE_INIT(gfan);
  7394. TPACPI_ACPIHANDLE_INIT(sfan);
  7395. }
  7396. if (tpacpi_is_lenovo()) {
  7397. TPACPI_ACPIHANDLE_INIT(fang);
  7398. TPACPI_ACPIHANDLE_INIT(fanw);
  7399. }
  7400. quirks = tpacpi_check_quirks(fan_quirk_table,
  7401. ARRAY_SIZE(fan_quirk_table));
  7402. if (quirks & TPACPI_FAN_NOFAN) {
  7403. pr_info("No integrated ThinkPad fan available\n");
  7404. return -ENODEV;
  7405. }
  7406. if (quirks & TPACPI_FAN_NS) {
  7407. pr_info("ECFW with non-standard fan reg control found\n");
  7408. fan_with_ns_addr = 1;
  7409. /* Fan ctrl support from host is undefined for now */
  7410. tp_features.fan_ctrl_status_undef = 1;
  7411. }
  7412. /* Check for the EC/BIOS with RPM reported in decimal*/
  7413. if (quirks & TPACPI_FAN_DECRPM) {
  7414. pr_info("ECFW with fan RPM as decimal in EC register\n");
  7415. ecfw_with_fan_dec_rpm = 1;
  7416. tp_features.fan_ctrl_status_undef = 1;
  7417. }
  7418. if (quirks & TPACPI_FAN_NOACPI) {
  7419. /* E560, T495, T495s */
  7420. pr_info("Ignoring buggy ACPI fan access method\n");
  7421. fang_handle = NULL;
  7422. fanw_handle = NULL;
  7423. }
  7424. if (gfan_handle) {
  7425. /* 570, 600e/x, 770e, 770x */
  7426. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  7427. } else if (fang_handle) {
  7428. /* E531 */
  7429. fan_status_access_mode = TPACPI_FAN_RD_ACPI_FANG;
  7430. } else {
  7431. /* all other ThinkPads: note that even old-style
  7432. * ThinkPad ECs supports the fan control register */
  7433. if (fan_with_ns_addr ||
  7434. likely(acpi_ec_read(fan_status_offset, &fan_control_initial_status))) {
  7435. int res;
  7436. unsigned int speed;
  7437. fan_status_access_mode = fan_with_ns_addr ?
  7438. TPACPI_FAN_RD_TPEC_NS : TPACPI_FAN_RD_TPEC;
  7439. if (quirks & TPACPI_FAN_Q1)
  7440. fan_quirk1_setup();
  7441. if (quirks & TPACPI_FAN_TPR)
  7442. fan_speed_in_tpr = true;
  7443. /* Try and probe the 2nd fan */
  7444. tp_features.second_fan = 1; /* needed for get_speed to work */
  7445. res = fan2_get_speed(&speed);
  7446. if (res >= 0 && speed != FAN_NOT_PRESENT) {
  7447. /* It responded - so let's assume it's there */
  7448. tp_features.second_fan = 1;
  7449. /* fan control not currently available for ns ECFW */
  7450. tp_features.second_fan_ctl = !fan_with_ns_addr;
  7451. pr_info("secondary fan control detected & enabled\n");
  7452. } else {
  7453. /* Fan not auto-detected */
  7454. tp_features.second_fan = 0;
  7455. if (quirks & TPACPI_FAN_2FAN) {
  7456. tp_features.second_fan = 1;
  7457. pr_info("secondary fan support enabled\n");
  7458. }
  7459. if (quirks & TPACPI_FAN_2CTL) {
  7460. tp_features.second_fan = 1;
  7461. tp_features.second_fan_ctl = 1;
  7462. pr_info("secondary fan control enabled\n");
  7463. }
  7464. }
  7465. } else {
  7466. pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
  7467. return -ENODEV;
  7468. }
  7469. }
  7470. if (sfan_handle) {
  7471. /* 570, 770x-JL */
  7472. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  7473. fan_control_commands |=
  7474. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  7475. } else if (fanw_handle) {
  7476. /* E531 */
  7477. fan_control_access_mode = TPACPI_FAN_WR_ACPI_FANW;
  7478. fan_control_commands |=
  7479. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_SPEED | TPACPI_FAN_CMD_ENABLE;
  7480. } else {
  7481. if (!gfan_handle) {
  7482. /* gfan without sfan means no fan control */
  7483. /* all other models implement TP EC 0x2f control */
  7484. if (fans_handle) {
  7485. /* X31, X40, X41 */
  7486. fan_control_access_mode =
  7487. TPACPI_FAN_WR_ACPI_FANS;
  7488. fan_control_commands |=
  7489. TPACPI_FAN_CMD_SPEED |
  7490. TPACPI_FAN_CMD_LEVEL |
  7491. TPACPI_FAN_CMD_ENABLE;
  7492. } else {
  7493. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  7494. fan_control_commands |=
  7495. TPACPI_FAN_CMD_LEVEL |
  7496. TPACPI_FAN_CMD_ENABLE;
  7497. }
  7498. }
  7499. }
  7500. vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7501. "fan is %s, modes %d, %d\n",
  7502. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  7503. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  7504. fan_status_access_mode, fan_control_access_mode);
  7505. /* fan control master switch */
  7506. if (!fan_control_allowed) {
  7507. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  7508. fan_control_commands = 0;
  7509. dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
  7510. "fan control features disabled by parameter\n");
  7511. }
  7512. /* update fan_control_desired_level */
  7513. if (fan_status_access_mode != TPACPI_FAN_NONE)
  7514. fan_get_status_safe(NULL);
  7515. if (fan_status_access_mode == TPACPI_FAN_NONE &&
  7516. fan_control_access_mode == TPACPI_FAN_WR_NONE)
  7517. return -ENODEV;
  7518. return 0;
  7519. }
  7520. static void fan_exit(void)
  7521. {
  7522. vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
  7523. "cancelling any pending fan watchdog tasks\n");
  7524. cancel_delayed_work(&fan_watchdog_task);
  7525. flush_workqueue(tpacpi_wq);
  7526. }
  7527. static void fan_suspend(void)
  7528. {
  7529. int rc;
  7530. if (!fan_control_allowed)
  7531. return;
  7532. /* Store fan status in cache */
  7533. fan_control_resume_level = 0;
  7534. rc = fan_get_status_safe(&fan_control_resume_level);
  7535. if (rc)
  7536. pr_notice("failed to read fan level for later restore during resume: %d\n",
  7537. rc);
  7538. /* if it is undefined, don't attempt to restore it.
  7539. * KEEP THIS LAST */
  7540. if (tp_features.fan_ctrl_status_undef)
  7541. fan_control_resume_level = 0;
  7542. }
  7543. static void fan_resume(void)
  7544. {
  7545. u8 current_level = 7;
  7546. bool do_set = false;
  7547. int rc;
  7548. /* DSDT *always* updates status on resume */
  7549. tp_features.fan_ctrl_status_undef = 0;
  7550. if (!fan_control_allowed ||
  7551. !fan_control_resume_level ||
  7552. fan_get_status_safe(&current_level))
  7553. return;
  7554. switch (fan_control_access_mode) {
  7555. case TPACPI_FAN_WR_ACPI_SFAN:
  7556. /* never decrease fan level */
  7557. do_set = (fan_control_resume_level > current_level);
  7558. break;
  7559. case TPACPI_FAN_WR_ACPI_FANS:
  7560. case TPACPI_FAN_WR_TPEC:
  7561. /* never decrease fan level, scale is:
  7562. * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
  7563. *
  7564. * We expect the firmware to set either 7 or AUTO, but we
  7565. * handle FULLSPEED out of paranoia.
  7566. *
  7567. * So, we can safely only restore FULLSPEED or 7, anything
  7568. * else could slow the fan. Restoring AUTO is useless, at
  7569. * best that's exactly what the DSDT already set (it is the
  7570. * slower it uses).
  7571. *
  7572. * Always keep in mind that the DSDT *will* have set the
  7573. * fans to what the vendor supposes is the best level. We
  7574. * muck with it only to speed the fan up.
  7575. */
  7576. if (fan_control_resume_level != 7 &&
  7577. !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
  7578. return;
  7579. else
  7580. do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
  7581. (current_level != fan_control_resume_level);
  7582. break;
  7583. default:
  7584. return;
  7585. }
  7586. if (do_set) {
  7587. pr_notice("restoring fan level to 0x%02x\n",
  7588. fan_control_resume_level);
  7589. rc = fan_set_level_safe(fan_control_resume_level);
  7590. if (rc < 0)
  7591. pr_notice("failed to restore fan level: %d\n", rc);
  7592. }
  7593. }
  7594. static int fan_read(struct seq_file *m)
  7595. {
  7596. int rc;
  7597. u8 status;
  7598. unsigned int speed = 0;
  7599. switch (fan_status_access_mode) {
  7600. case TPACPI_FAN_RD_ACPI_GFAN:
  7601. /* 570, 600e/x, 770e, 770x */
  7602. rc = fan_get_status_safe(&status);
  7603. if (rc)
  7604. return rc;
  7605. seq_printf(m, "status:\t\t%s\n"
  7606. "level:\t\t%d\n",
  7607. str_enabled_disabled(status), status);
  7608. break;
  7609. case TPACPI_FAN_RD_TPEC_NS:
  7610. case TPACPI_FAN_RD_TPEC:
  7611. case TPACPI_FAN_RD_ACPI_FANG:
  7612. /* all except 570, 600e/x, 770e, 770x */
  7613. rc = fan_get_status_safe(&status);
  7614. if (rc)
  7615. return rc;
  7616. seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status));
  7617. rc = fan_get_speed(&speed);
  7618. if (rc < 0)
  7619. return rc;
  7620. /* Check for fan speeds displayed in hexadecimal */
  7621. if (!ecfw_with_fan_dec_rpm)
  7622. seq_printf(m, "speed:\t\t%d\n", speed);
  7623. else
  7624. seq_printf(m, "speed:\t\t%x\n", speed);
  7625. if (fan_status_access_mode == TPACPI_FAN_RD_TPEC_NS) {
  7626. /*
  7627. * No full speed bit in NS EC
  7628. * EC Auto mode is set by default.
  7629. * No other levels settings available
  7630. */
  7631. seq_printf(m, "level:\t\t%s\n", status & FAN_NS_CTRL ? "unknown" : "auto");
  7632. } else if (fan_status_access_mode == TPACPI_FAN_RD_TPEC) {
  7633. if (status & TP_EC_FAN_FULLSPEED)
  7634. /* Disengaged mode takes precedence */
  7635. seq_printf(m, "level:\t\tdisengaged\n");
  7636. else if (status & TP_EC_FAN_AUTO)
  7637. seq_printf(m, "level:\t\tauto\n");
  7638. else
  7639. seq_printf(m, "level:\t\t%d\n", status);
  7640. }
  7641. break;
  7642. case TPACPI_FAN_NONE:
  7643. default:
  7644. seq_printf(m, "status:\t\tnot supported\n");
  7645. }
  7646. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  7647. seq_printf(m, "commands:\tlevel <level>");
  7648. switch (fan_control_access_mode) {
  7649. case TPACPI_FAN_WR_ACPI_SFAN:
  7650. seq_printf(m, " (<level> is 0-7)\n");
  7651. break;
  7652. default:
  7653. seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
  7654. break;
  7655. }
  7656. }
  7657. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  7658. seq_printf(m, "commands:\tenable, disable\n"
  7659. "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
  7660. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  7661. seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
  7662. return 0;
  7663. }
  7664. static int fan_write_cmd_level(const char *cmd, int *rc)
  7665. {
  7666. int level;
  7667. if (strstarts(cmd, "level auto"))
  7668. level = TP_EC_FAN_AUTO;
  7669. else if (strstarts(cmd, "level disengaged") || strstarts(cmd, "level full-speed"))
  7670. level = TP_EC_FAN_FULLSPEED;
  7671. else if (sscanf(cmd, "level %d", &level) != 1)
  7672. return 0;
  7673. *rc = fan_set_level_safe(level);
  7674. if (*rc == -ENXIO)
  7675. pr_err("level command accepted for unsupported access mode %d\n",
  7676. fan_control_access_mode);
  7677. else if (!*rc)
  7678. tpacpi_disclose_usertask("procfs fan",
  7679. "set level to %d\n", level);
  7680. return 1;
  7681. }
  7682. static int fan_write_cmd_enable(const char *cmd, int *rc)
  7683. {
  7684. if (!strstarts(cmd, "enable"))
  7685. return 0;
  7686. *rc = fan_set_enable();
  7687. if (*rc == -ENXIO)
  7688. pr_err("enable command accepted for unsupported access mode %d\n",
  7689. fan_control_access_mode);
  7690. else if (!*rc)
  7691. tpacpi_disclose_usertask("procfs fan", "enable\n");
  7692. return 1;
  7693. }
  7694. static int fan_write_cmd_disable(const char *cmd, int *rc)
  7695. {
  7696. if (!strstarts(cmd, "disable"))
  7697. return 0;
  7698. *rc = fan_set_disable();
  7699. if (*rc == -ENXIO)
  7700. pr_err("disable command accepted for unsupported access mode %d\n",
  7701. fan_control_access_mode);
  7702. else if (!*rc)
  7703. tpacpi_disclose_usertask("procfs fan", "disable\n");
  7704. return 1;
  7705. }
  7706. static int fan_write_cmd_speed(const char *cmd, int *rc)
  7707. {
  7708. int speed;
  7709. /* TODO:
  7710. * Support speed <low> <medium> <high> ? */
  7711. if (sscanf(cmd, "speed %d", &speed) != 1)
  7712. return 0;
  7713. *rc = fan_set_speed(speed);
  7714. if (*rc == -ENXIO)
  7715. pr_err("speed command accepted for unsupported access mode %d\n",
  7716. fan_control_access_mode);
  7717. else if (!*rc)
  7718. tpacpi_disclose_usertask("procfs fan",
  7719. "set speed to %d\n", speed);
  7720. return 1;
  7721. }
  7722. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  7723. {
  7724. int interval;
  7725. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  7726. return 0;
  7727. if (interval < 0 || interval > 120)
  7728. *rc = -EINVAL;
  7729. else {
  7730. fan_watchdog_maxinterval = interval;
  7731. tpacpi_disclose_usertask("procfs fan",
  7732. "set watchdog timer to %d\n",
  7733. interval);
  7734. }
  7735. return 1;
  7736. }
  7737. static int fan_write(char *buf)
  7738. {
  7739. char *cmd;
  7740. int rc = 0;
  7741. while (!rc && (cmd = strsep(&buf, ","))) {
  7742. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  7743. fan_write_cmd_level(cmd, &rc)) &&
  7744. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  7745. (fan_write_cmd_enable(cmd, &rc) ||
  7746. fan_write_cmd_disable(cmd, &rc) ||
  7747. fan_write_cmd_watchdog(cmd, &rc))) &&
  7748. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  7749. fan_write_cmd_speed(cmd, &rc))
  7750. )
  7751. rc = -EINVAL;
  7752. else if (!rc)
  7753. fan_watchdog_reset();
  7754. }
  7755. return rc;
  7756. }
  7757. static struct ibm_struct fan_driver_data = {
  7758. .name = "fan",
  7759. .read = fan_read,
  7760. .write = fan_write,
  7761. .exit = fan_exit,
  7762. .suspend = fan_suspend,
  7763. .resume = fan_resume,
  7764. };
  7765. /*************************************************************************
  7766. * Mute LED subdriver
  7767. */
  7768. #define TPACPI_LED_MAX 2
  7769. struct tp_led_table {
  7770. acpi_string name;
  7771. int on_value;
  7772. int off_value;
  7773. int state;
  7774. };
  7775. static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
  7776. [LED_AUDIO_MUTE] = {
  7777. .name = "SSMS",
  7778. .on_value = 1,
  7779. .off_value = 0,
  7780. },
  7781. [LED_AUDIO_MICMUTE] = {
  7782. .name = "MMTS",
  7783. .on_value = 2,
  7784. .off_value = 0,
  7785. },
  7786. };
  7787. static int mute_led_on_off(struct tp_led_table *t, bool state)
  7788. {
  7789. acpi_handle temp;
  7790. int output;
  7791. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
  7792. pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
  7793. return -EIO;
  7794. }
  7795. if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
  7796. state ? t->on_value : t->off_value))
  7797. return -EIO;
  7798. t->state = state;
  7799. return state;
  7800. }
  7801. static int tpacpi_led_set(int whichled, bool on)
  7802. {
  7803. struct tp_led_table *t;
  7804. t = &led_tables[whichled];
  7805. if (t->state < 0 || t->state == on)
  7806. return t->state;
  7807. return mute_led_on_off(t, on);
  7808. }
  7809. static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
  7810. enum led_brightness brightness)
  7811. {
  7812. return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
  7813. }
  7814. static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
  7815. enum led_brightness brightness)
  7816. {
  7817. return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
  7818. }
  7819. static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
  7820. [LED_AUDIO_MUTE] = {
  7821. .name = "platform::mute",
  7822. .max_brightness = 1,
  7823. .brightness_set_blocking = tpacpi_led_mute_set,
  7824. .default_trigger = "audio-mute",
  7825. },
  7826. [LED_AUDIO_MICMUTE] = {
  7827. .name = "platform::micmute",
  7828. .max_brightness = 1,
  7829. .brightness_set_blocking = tpacpi_led_micmute_set,
  7830. .default_trigger = "audio-micmute",
  7831. },
  7832. };
  7833. static int mute_led_init(struct ibm_init_struct *iibm)
  7834. {
  7835. acpi_handle temp;
  7836. int i, err;
  7837. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7838. struct tp_led_table *t = &led_tables[i];
  7839. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
  7840. t->state = -ENODEV;
  7841. continue;
  7842. }
  7843. err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
  7844. if (err < 0) {
  7845. while (i--)
  7846. led_classdev_unregister(&mute_led_cdev[i]);
  7847. return err;
  7848. }
  7849. }
  7850. return 0;
  7851. }
  7852. static void mute_led_exit(void)
  7853. {
  7854. int i;
  7855. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7856. led_classdev_unregister(&mute_led_cdev[i]);
  7857. tpacpi_led_set(i, false);
  7858. }
  7859. }
  7860. static void mute_led_resume(void)
  7861. {
  7862. int i;
  7863. for (i = 0; i < TPACPI_LED_MAX; i++) {
  7864. struct tp_led_table *t = &led_tables[i];
  7865. if (t->state >= 0)
  7866. mute_led_on_off(t, t->state);
  7867. }
  7868. }
  7869. static struct ibm_struct mute_led_driver_data = {
  7870. .name = "mute_led",
  7871. .exit = mute_led_exit,
  7872. .resume = mute_led_resume,
  7873. };
  7874. /*
  7875. * Battery Wear Control Driver
  7876. * Contact: Ognjen Galic <smclt30p@gmail.com>
  7877. */
  7878. /* Metadata */
  7879. #define GET_START "BCTG"
  7880. #define SET_START "BCCS"
  7881. #define GET_STOP "BCSG"
  7882. #define SET_STOP "BCSS"
  7883. #define GET_DISCHARGE "BDSG"
  7884. #define SET_DISCHARGE "BDSS"
  7885. #define GET_INHIBIT "BICG"
  7886. #define SET_INHIBIT "BICS"
  7887. enum {
  7888. BAT_ANY = 0,
  7889. BAT_PRIMARY = 1,
  7890. BAT_SECONDARY = 2
  7891. };
  7892. enum {
  7893. /* Error condition bit */
  7894. METHOD_ERR = BIT(31),
  7895. };
  7896. enum {
  7897. /* This is used in the get/set helpers */
  7898. THRESHOLD_START,
  7899. THRESHOLD_STOP,
  7900. FORCE_DISCHARGE,
  7901. INHIBIT_CHARGE,
  7902. };
  7903. struct tpacpi_battery_data {
  7904. int charge_start;
  7905. int start_support;
  7906. int charge_stop;
  7907. int stop_support;
  7908. unsigned int charge_behaviours;
  7909. };
  7910. struct tpacpi_battery_driver_data {
  7911. struct tpacpi_battery_data batteries[3];
  7912. int individual_addressing;
  7913. };
  7914. static struct tpacpi_battery_driver_data battery_info;
  7915. /* ACPI helpers/functions/probes */
  7916. /*
  7917. * This evaluates a ACPI method call specific to the battery
  7918. * ACPI extension. The specifics are that an error is marked
  7919. * in the 32rd bit of the response, so we just check that here.
  7920. */
  7921. static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
  7922. {
  7923. int response;
  7924. if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
  7925. acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
  7926. return AE_ERROR;
  7927. }
  7928. if (response & METHOD_ERR) {
  7929. acpi_handle_err(hkey_handle,
  7930. "%s evaluated but flagged as error", method);
  7931. return AE_ERROR;
  7932. }
  7933. *ret = response;
  7934. return AE_OK;
  7935. }
  7936. static int tpacpi_battery_get(int what, int battery, int *ret)
  7937. {
  7938. switch (what) {
  7939. case THRESHOLD_START:
  7940. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
  7941. return -ENODEV;
  7942. /* The value is in the low 8 bits of the response */
  7943. *ret = *ret & 0xFF;
  7944. return 0;
  7945. case THRESHOLD_STOP:
  7946. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
  7947. return -ENODEV;
  7948. /* Value is in lower 8 bits */
  7949. *ret = *ret & 0xFF;
  7950. /*
  7951. * On the stop value, if we return 0 that
  7952. * does not make any sense. 0 means Default, which
  7953. * means that charging stops at 100%, so we return
  7954. * that.
  7955. */
  7956. if (*ret == 0)
  7957. *ret = 100;
  7958. return 0;
  7959. case FORCE_DISCHARGE:
  7960. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery))
  7961. return -ENODEV;
  7962. /* The force discharge status is in bit 0 */
  7963. *ret = *ret & 0x01;
  7964. return 0;
  7965. case INHIBIT_CHARGE:
  7966. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery))
  7967. return -ENODEV;
  7968. /* The inhibit charge status is in bit 0 */
  7969. *ret = *ret & 0x01;
  7970. return 0;
  7971. default:
  7972. pr_crit("wrong parameter: %d", what);
  7973. return -EINVAL;
  7974. }
  7975. }
  7976. static int tpacpi_battery_set(int what, int battery, int value)
  7977. {
  7978. int param, ret;
  7979. /* The first 8 bits are the value of the threshold */
  7980. param = value;
  7981. /* The battery ID is in bits 8-9, 2 bits */
  7982. param |= battery << 8;
  7983. switch (what) {
  7984. case THRESHOLD_START:
  7985. if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
  7986. pr_err("failed to set charge threshold on battery %d",
  7987. battery);
  7988. return -ENODEV;
  7989. }
  7990. return 0;
  7991. case THRESHOLD_STOP:
  7992. if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
  7993. pr_err("failed to set stop threshold: %d", battery);
  7994. return -ENODEV;
  7995. }
  7996. return 0;
  7997. case FORCE_DISCHARGE:
  7998. /* Force discharge is in bit 0,
  7999. * break on AC attach is in bit 1 (won't work on some ThinkPads),
  8000. * battery ID is in bits 8-9, 2 bits.
  8001. */
  8002. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) {
  8003. pr_err("failed to set force discharge on %d", battery);
  8004. return -ENODEV;
  8005. }
  8006. return 0;
  8007. case INHIBIT_CHARGE:
  8008. /* When setting inhibit charge, we set a default value of
  8009. * always breaking on AC detach and the effective time is set to
  8010. * be permanent.
  8011. * The battery ID is in bits 4-5, 2 bits,
  8012. * the effective time is in bits 8-23, 2 bytes.
  8013. * A time of FFFF indicates forever.
  8014. */
  8015. param = value;
  8016. param |= battery << 4;
  8017. param |= 0xFFFF << 8;
  8018. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) {
  8019. pr_err("failed to set inhibit charge on %d", battery);
  8020. return -ENODEV;
  8021. }
  8022. return 0;
  8023. default:
  8024. pr_crit("wrong parameter: %d", what);
  8025. return -EINVAL;
  8026. }
  8027. }
  8028. static int tpacpi_battery_set_validate(int what, int battery, int value)
  8029. {
  8030. int ret, v;
  8031. ret = tpacpi_battery_set(what, battery, value);
  8032. if (ret < 0)
  8033. return ret;
  8034. ret = tpacpi_battery_get(what, battery, &v);
  8035. if (ret < 0)
  8036. return ret;
  8037. if (v == value)
  8038. return 0;
  8039. msleep(500);
  8040. ret = tpacpi_battery_get(what, battery, &v);
  8041. if (ret < 0)
  8042. return ret;
  8043. if (v == value)
  8044. return 0;
  8045. return -EIO;
  8046. }
  8047. static int tpacpi_battery_probe(int battery)
  8048. {
  8049. int ret = 0;
  8050. memset(&battery_info.batteries[battery], 0,
  8051. sizeof(battery_info.batteries[battery]));
  8052. /*
  8053. * 1) Get the current start threshold
  8054. * 2) Check for support
  8055. * 3) Get the current stop threshold
  8056. * 4) Check for support
  8057. * 5) Get the current force discharge status
  8058. * 6) Check for support
  8059. * 7) Get the current inhibit charge status
  8060. * 8) Check for support
  8061. */
  8062. if (acpi_has_method(hkey_handle, GET_START)) {
  8063. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
  8064. pr_err("Error probing battery %d\n", battery);
  8065. return -ENODEV;
  8066. }
  8067. /* Individual addressing is in bit 9 */
  8068. if (ret & BIT(9))
  8069. battery_info.individual_addressing = true;
  8070. /* Support is marked in bit 8 */
  8071. if (ret & BIT(8))
  8072. battery_info.batteries[battery].start_support = 1;
  8073. else
  8074. return -ENODEV;
  8075. if (tpacpi_battery_get(THRESHOLD_START, battery,
  8076. &battery_info.batteries[battery].charge_start)) {
  8077. pr_err("Error probing battery %d\n", battery);
  8078. return -ENODEV;
  8079. }
  8080. }
  8081. if (acpi_has_method(hkey_handle, GET_STOP)) {
  8082. if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
  8083. pr_err("Error probing battery stop; %d\n", battery);
  8084. return -ENODEV;
  8085. }
  8086. /* Support is marked in bit 8 */
  8087. if (ret & BIT(8))
  8088. battery_info.batteries[battery].stop_support = 1;
  8089. else
  8090. return -ENODEV;
  8091. if (tpacpi_battery_get(THRESHOLD_STOP, battery,
  8092. &battery_info.batteries[battery].charge_stop)) {
  8093. pr_err("Error probing battery stop: %d\n", battery);
  8094. return -ENODEV;
  8095. }
  8096. }
  8097. if (acpi_has_method(hkey_handle, GET_DISCHARGE)) {
  8098. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) {
  8099. pr_err("Error probing battery discharge; %d\n", battery);
  8100. return -ENODEV;
  8101. }
  8102. /* Support is marked in bit 8 */
  8103. if (ret & BIT(8))
  8104. battery_info.batteries[battery].charge_behaviours |=
  8105. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE);
  8106. }
  8107. if (acpi_has_method(hkey_handle, GET_INHIBIT)) {
  8108. if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) {
  8109. pr_err("Error probing battery inhibit charge; %d\n", battery);
  8110. return -ENODEV;
  8111. }
  8112. /* Support is marked in bit 5 */
  8113. if (ret & BIT(5))
  8114. battery_info.batteries[battery].charge_behaviours |=
  8115. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE);
  8116. }
  8117. battery_info.batteries[battery].charge_behaviours |=
  8118. BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO);
  8119. pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n",
  8120. battery,
  8121. battery_info.batteries[battery].charge_start,
  8122. battery_info.batteries[battery].charge_stop,
  8123. battery_info.batteries[battery].charge_behaviours);
  8124. return 0;
  8125. }
  8126. /* General helper functions */
  8127. static int tpacpi_battery_get_id(const char *battery_name)
  8128. {
  8129. if (strcmp(battery_name, "BAT0") == 0 ||
  8130. tp_features.battery_force_primary)
  8131. return BAT_PRIMARY;
  8132. if (strcmp(battery_name, "BAT1") == 0)
  8133. return BAT_SECONDARY;
  8134. /*
  8135. * If for some reason the battery is not BAT0 nor is it
  8136. * BAT1, we will assume it's the default, first battery,
  8137. * AKA primary.
  8138. */
  8139. pr_warn("unknown battery %s, assuming primary", battery_name);
  8140. return BAT_PRIMARY;
  8141. }
  8142. /* sysfs interface */
  8143. static ssize_t tpacpi_battery_store(int what,
  8144. struct device *dev,
  8145. const char *buf, size_t count)
  8146. {
  8147. struct power_supply *supply = to_power_supply(dev);
  8148. unsigned long value;
  8149. int battery, rval;
  8150. /*
  8151. * Some systems have support for more than
  8152. * one battery. If that is the case,
  8153. * tpacpi_battery_probe marked that addressing
  8154. * them individually is supported, so we do that
  8155. * based on the device struct.
  8156. *
  8157. * On systems that are not supported, we assume
  8158. * the primary as most of the ACPI calls fail
  8159. * with "Any Battery" as the parameter.
  8160. */
  8161. if (battery_info.individual_addressing)
  8162. /* BAT_PRIMARY or BAT_SECONDARY */
  8163. battery = tpacpi_battery_get_id(supply->desc->name);
  8164. else
  8165. battery = BAT_PRIMARY;
  8166. rval = kstrtoul(buf, 10, &value);
  8167. if (rval)
  8168. return rval;
  8169. switch (what) {
  8170. case THRESHOLD_START:
  8171. if (!battery_info.batteries[battery].start_support)
  8172. return -ENODEV;
  8173. /* valid values are [0, 99] */
  8174. if (value > 99)
  8175. return -EINVAL;
  8176. if (value > battery_info.batteries[battery].charge_stop)
  8177. return -EINVAL;
  8178. if (tpacpi_battery_set(THRESHOLD_START, battery, value))
  8179. return -ENODEV;
  8180. battery_info.batteries[battery].charge_start = value;
  8181. return count;
  8182. case THRESHOLD_STOP:
  8183. if (!battery_info.batteries[battery].stop_support)
  8184. return -ENODEV;
  8185. /* valid values are [1, 100] */
  8186. if (value < 1 || value > 100)
  8187. return -EINVAL;
  8188. if (value < battery_info.batteries[battery].charge_start)
  8189. return -EINVAL;
  8190. battery_info.batteries[battery].charge_stop = value;
  8191. /*
  8192. * When 100 is passed to stop, we need to flip
  8193. * it to 0 as that the EC understands that as
  8194. * "Default", which will charge to 100%
  8195. */
  8196. if (value == 100)
  8197. value = 0;
  8198. if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
  8199. return -EINVAL;
  8200. return count;
  8201. default:
  8202. pr_crit("Wrong parameter: %d", what);
  8203. return -EINVAL;
  8204. }
  8205. return count;
  8206. }
  8207. static ssize_t tpacpi_battery_show(int what,
  8208. struct device *dev,
  8209. char *buf)
  8210. {
  8211. struct power_supply *supply = to_power_supply(dev);
  8212. int ret, battery;
  8213. /*
  8214. * Some systems have support for more than
  8215. * one battery. If that is the case,
  8216. * tpacpi_battery_probe marked that addressing
  8217. * them individually is supported, so we;
  8218. * based on the device struct.
  8219. *
  8220. * On systems that are not supported, we assume
  8221. * the primary as most of the ACPI calls fail
  8222. * with "Any Battery" as the parameter.
  8223. */
  8224. if (battery_info.individual_addressing)
  8225. /* BAT_PRIMARY or BAT_SECONDARY */
  8226. battery = tpacpi_battery_get_id(supply->desc->name);
  8227. else
  8228. battery = BAT_PRIMARY;
  8229. if (tpacpi_battery_get(what, battery, &ret))
  8230. return -ENODEV;
  8231. return sysfs_emit(buf, "%d\n", ret);
  8232. }
  8233. static ssize_t charge_control_start_threshold_show(struct device *device,
  8234. struct device_attribute *attr,
  8235. char *buf)
  8236. {
  8237. return tpacpi_battery_show(THRESHOLD_START, device, buf);
  8238. }
  8239. static ssize_t charge_control_end_threshold_show(struct device *device,
  8240. struct device_attribute *attr,
  8241. char *buf)
  8242. {
  8243. return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
  8244. }
  8245. static ssize_t charge_behaviour_show(struct device *dev,
  8246. struct device_attribute *attr,
  8247. char *buf)
  8248. {
  8249. enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
  8250. struct power_supply *supply = to_power_supply(dev);
  8251. unsigned int available;
  8252. int ret, battery;
  8253. battery = tpacpi_battery_get_id(supply->desc->name);
  8254. available = battery_info.batteries[battery].charge_behaviours;
  8255. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) {
  8256. if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret))
  8257. return -ENODEV;
  8258. if (ret) {
  8259. active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
  8260. goto out;
  8261. }
  8262. }
  8263. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) {
  8264. if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret))
  8265. return -ENODEV;
  8266. if (ret) {
  8267. active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
  8268. goto out;
  8269. }
  8270. }
  8271. out:
  8272. return power_supply_charge_behaviour_show(dev, available, active, buf);
  8273. }
  8274. static ssize_t charge_control_start_threshold_store(struct device *dev,
  8275. struct device_attribute *attr,
  8276. const char *buf, size_t count)
  8277. {
  8278. return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
  8279. }
  8280. static ssize_t charge_control_end_threshold_store(struct device *dev,
  8281. struct device_attribute *attr,
  8282. const char *buf, size_t count)
  8283. {
  8284. return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
  8285. }
  8286. static ssize_t charge_behaviour_store(struct device *dev,
  8287. struct device_attribute *attr,
  8288. const char *buf, size_t count)
  8289. {
  8290. struct power_supply *supply = to_power_supply(dev);
  8291. int selected, battery, ret = 0;
  8292. unsigned int available;
  8293. battery = tpacpi_battery_get_id(supply->desc->name);
  8294. available = battery_info.batteries[battery].charge_behaviours;
  8295. selected = power_supply_charge_behaviour_parse(available, buf);
  8296. if (selected < 0)
  8297. return selected;
  8298. switch (selected) {
  8299. case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
  8300. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
  8301. ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
  8302. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
  8303. ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0));
  8304. if (ret < 0)
  8305. return ret;
  8306. break;
  8307. case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
  8308. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE))
  8309. ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0);
  8310. ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1));
  8311. if (ret < 0)
  8312. return ret;
  8313. break;
  8314. case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
  8315. if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
  8316. ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0);
  8317. ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1));
  8318. if (ret < 0)
  8319. return ret;
  8320. break;
  8321. default:
  8322. dev_err(dev, "Unexpected charge behaviour: %d\n", selected);
  8323. return -EINVAL;
  8324. }
  8325. return count;
  8326. }
  8327. static DEVICE_ATTR_RW(charge_control_start_threshold);
  8328. static DEVICE_ATTR_RW(charge_control_end_threshold);
  8329. static DEVICE_ATTR_RW(charge_behaviour);
  8330. static struct device_attribute dev_attr_charge_start_threshold = __ATTR(
  8331. charge_start_threshold,
  8332. 0644,
  8333. charge_control_start_threshold_show,
  8334. charge_control_start_threshold_store
  8335. );
  8336. static struct device_attribute dev_attr_charge_stop_threshold = __ATTR(
  8337. charge_stop_threshold,
  8338. 0644,
  8339. charge_control_end_threshold_show,
  8340. charge_control_end_threshold_store
  8341. );
  8342. static struct attribute *tpacpi_battery_attrs[] = {
  8343. &dev_attr_charge_control_start_threshold.attr,
  8344. &dev_attr_charge_control_end_threshold.attr,
  8345. &dev_attr_charge_start_threshold.attr,
  8346. &dev_attr_charge_stop_threshold.attr,
  8347. &dev_attr_charge_behaviour.attr,
  8348. NULL,
  8349. };
  8350. ATTRIBUTE_GROUPS(tpacpi_battery);
  8351. /* ACPI battery hooking */
  8352. static int tpacpi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
  8353. {
  8354. int batteryid = tpacpi_battery_get_id(battery->desc->name);
  8355. if (tpacpi_battery_probe(batteryid))
  8356. return -ENODEV;
  8357. if (device_add_groups(&battery->dev, tpacpi_battery_groups))
  8358. return -ENODEV;
  8359. return 0;
  8360. }
  8361. static int tpacpi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
  8362. {
  8363. device_remove_groups(&battery->dev, tpacpi_battery_groups);
  8364. return 0;
  8365. }
  8366. static struct acpi_battery_hook battery_hook = {
  8367. .add_battery = tpacpi_battery_add,
  8368. .remove_battery = tpacpi_battery_remove,
  8369. .name = "ThinkPad Battery Extension",
  8370. };
  8371. /* Subdriver init/exit */
  8372. static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
  8373. /*
  8374. * Individual addressing is broken on models that expose the
  8375. * primary battery as BAT1.
  8376. */
  8377. TPACPI_Q_LNV('G', '8', true), /* ThinkPad X131e */
  8378. TPACPI_Q_LNV('8', 'F', true), /* Thinkpad X120e */
  8379. TPACPI_Q_LNV('J', '7', true), /* B5400 */
  8380. TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
  8381. TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
  8382. TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
  8383. TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
  8384. TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */
  8385. };
  8386. static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
  8387. {
  8388. memset(&battery_info, 0, sizeof(battery_info));
  8389. tp_features.battery_force_primary = tpacpi_check_quirks(
  8390. battery_quirk_table,
  8391. ARRAY_SIZE(battery_quirk_table));
  8392. battery_hook_register(&battery_hook);
  8393. return 0;
  8394. }
  8395. static void tpacpi_battery_exit(void)
  8396. {
  8397. battery_hook_unregister(&battery_hook);
  8398. }
  8399. static struct ibm_struct battery_driver_data = {
  8400. .name = "battery",
  8401. .exit = tpacpi_battery_exit,
  8402. };
  8403. /*************************************************************************
  8404. * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
  8405. */
  8406. static struct drm_privacy_screen *lcdshadow_dev;
  8407. static acpi_handle lcdshadow_get_handle;
  8408. static acpi_handle lcdshadow_set_handle;
  8409. static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv,
  8410. enum drm_privacy_screen_status state)
  8411. {
  8412. int output;
  8413. if (WARN_ON(!mutex_is_locked(&priv->lock)))
  8414. return -EIO;
  8415. if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state))
  8416. return -EIO;
  8417. priv->hw_state = priv->sw_state = state;
  8418. return 0;
  8419. }
  8420. static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv)
  8421. {
  8422. int output;
  8423. if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
  8424. return;
  8425. priv->hw_state = priv->sw_state = output & 0x1;
  8426. }
  8427. static const struct drm_privacy_screen_ops lcdshadow_ops = {
  8428. .set_sw_state = lcdshadow_set_sw_state,
  8429. .get_hw_state = lcdshadow_get_hw_state,
  8430. };
  8431. static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
  8432. {
  8433. acpi_status status1, status2;
  8434. int output;
  8435. status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle);
  8436. status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle);
  8437. if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2))
  8438. return 0;
  8439. if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0))
  8440. return -EIO;
  8441. if (!(output & 0x10000))
  8442. return 0;
  8443. lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev,
  8444. &lcdshadow_ops, NULL);
  8445. if (IS_ERR(lcdshadow_dev))
  8446. return PTR_ERR(lcdshadow_dev);
  8447. return 0;
  8448. }
  8449. static void lcdshadow_exit(void)
  8450. {
  8451. drm_privacy_screen_unregister(lcdshadow_dev);
  8452. }
  8453. static void lcdshadow_resume(void)
  8454. {
  8455. if (!lcdshadow_dev)
  8456. return;
  8457. mutex_lock(&lcdshadow_dev->lock);
  8458. lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state);
  8459. mutex_unlock(&lcdshadow_dev->lock);
  8460. }
  8461. static int lcdshadow_read(struct seq_file *m)
  8462. {
  8463. if (!lcdshadow_dev) {
  8464. seq_puts(m, "status:\t\tnot supported\n");
  8465. } else {
  8466. seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state);
  8467. seq_puts(m, "commands:\t0, 1\n");
  8468. }
  8469. return 0;
  8470. }
  8471. static int lcdshadow_write(char *buf)
  8472. {
  8473. char *cmd;
  8474. int res, state = -EINVAL;
  8475. if (!lcdshadow_dev)
  8476. return -ENODEV;
  8477. while ((cmd = strsep(&buf, ","))) {
  8478. res = kstrtoint(cmd, 10, &state);
  8479. if (res < 0)
  8480. return res;
  8481. }
  8482. if (state >= 2 || state < 0)
  8483. return -EINVAL;
  8484. mutex_lock(&lcdshadow_dev->lock);
  8485. res = lcdshadow_set_sw_state(lcdshadow_dev, state);
  8486. mutex_unlock(&lcdshadow_dev->lock);
  8487. drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
  8488. return res;
  8489. }
  8490. static struct ibm_struct lcdshadow_driver_data = {
  8491. .name = "lcdshadow",
  8492. .exit = lcdshadow_exit,
  8493. .resume = lcdshadow_resume,
  8494. .read = lcdshadow_read,
  8495. .write = lcdshadow_write,
  8496. };
  8497. /*************************************************************************
  8498. * Thinkpad sensor interfaces
  8499. */
  8500. #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
  8501. #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
  8502. #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
  8503. #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
  8504. #define DYTC_CMD_GET 2 /* To get current IC function and mode */
  8505. #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */
  8506. #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */
  8507. #define PALMSENSOR_ON_BIT 1 /* psensor status */
  8508. static bool has_palmsensor;
  8509. static bool has_lapsensor;
  8510. static bool palm_state;
  8511. static bool lap_state;
  8512. static int dytc_version;
  8513. static int dytc_command(int command, int *output)
  8514. {
  8515. acpi_handle dytc_handle;
  8516. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) {
  8517. /* Platform doesn't support DYTC */
  8518. return -ENODEV;
  8519. }
  8520. if (!acpi_evalf(dytc_handle, output, NULL, "dd", command))
  8521. return -EIO;
  8522. return 0;
  8523. }
  8524. static int lapsensor_get(bool *present, bool *state)
  8525. {
  8526. int output, err;
  8527. *present = false;
  8528. err = dytc_command(DYTC_CMD_GET, &output);
  8529. if (err)
  8530. return err;
  8531. *present = true; /*If we get his far, we have lapmode support*/
  8532. *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false;
  8533. return 0;
  8534. }
  8535. static int palmsensor_get(bool *present, bool *state)
  8536. {
  8537. acpi_handle psensor_handle;
  8538. int output;
  8539. *present = false;
  8540. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle)))
  8541. return -ENODEV;
  8542. if (!acpi_evalf(psensor_handle, &output, NULL, "d"))
  8543. return -EIO;
  8544. *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false;
  8545. *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false;
  8546. return 0;
  8547. }
  8548. static void lapsensor_refresh(void)
  8549. {
  8550. bool state;
  8551. int err;
  8552. if (has_lapsensor) {
  8553. err = lapsensor_get(&has_lapsensor, &state);
  8554. if (err)
  8555. return;
  8556. if (lap_state != state) {
  8557. lap_state = state;
  8558. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode");
  8559. }
  8560. }
  8561. }
  8562. static void palmsensor_refresh(void)
  8563. {
  8564. bool state;
  8565. int err;
  8566. if (has_palmsensor) {
  8567. err = palmsensor_get(&has_palmsensor, &state);
  8568. if (err)
  8569. return;
  8570. if (palm_state != state) {
  8571. palm_state = state;
  8572. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor");
  8573. }
  8574. }
  8575. }
  8576. static ssize_t dytc_lapmode_show(struct device *dev,
  8577. struct device_attribute *attr,
  8578. char *buf)
  8579. {
  8580. if (has_lapsensor)
  8581. return sysfs_emit(buf, "%d\n", lap_state);
  8582. return sysfs_emit(buf, "\n");
  8583. }
  8584. static DEVICE_ATTR_RO(dytc_lapmode);
  8585. static ssize_t palmsensor_show(struct device *dev,
  8586. struct device_attribute *attr,
  8587. char *buf)
  8588. {
  8589. if (has_palmsensor)
  8590. return sysfs_emit(buf, "%d\n", palm_state);
  8591. return sysfs_emit(buf, "\n");
  8592. }
  8593. static DEVICE_ATTR_RO(palmsensor);
  8594. static struct attribute *proxsensor_attributes[] = {
  8595. &dev_attr_dytc_lapmode.attr,
  8596. &dev_attr_palmsensor.attr,
  8597. NULL
  8598. };
  8599. static umode_t proxsensor_attr_is_visible(struct kobject *kobj,
  8600. struct attribute *attr, int n)
  8601. {
  8602. if (attr == &dev_attr_dytc_lapmode.attr) {
  8603. /*
  8604. * Platforms before DYTC version 5 claim to have a lap sensor,
  8605. * but it doesn't work, so we ignore them.
  8606. */
  8607. if (!has_lapsensor || dytc_version < 5)
  8608. return 0;
  8609. } else if (attr == &dev_attr_palmsensor.attr) {
  8610. if (!has_palmsensor)
  8611. return 0;
  8612. }
  8613. return attr->mode;
  8614. }
  8615. static const struct attribute_group proxsensor_attr_group = {
  8616. .is_visible = proxsensor_attr_is_visible,
  8617. .attrs = proxsensor_attributes,
  8618. };
  8619. static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm)
  8620. {
  8621. int palm_err, lap_err;
  8622. palm_err = palmsensor_get(&has_palmsensor, &palm_state);
  8623. lap_err = lapsensor_get(&has_lapsensor, &lap_state);
  8624. /* If support isn't available for both devices return -ENODEV */
  8625. if ((palm_err == -ENODEV) && (lap_err == -ENODEV))
  8626. return -ENODEV;
  8627. /* Otherwise, if there was an error return it */
  8628. if (palm_err && (palm_err != -ENODEV))
  8629. return palm_err;
  8630. if (lap_err && (lap_err != -ENODEV))
  8631. return lap_err;
  8632. return 0;
  8633. }
  8634. static struct ibm_struct proxsensor_driver_data = {
  8635. .name = "proximity-sensor",
  8636. };
  8637. /*************************************************************************
  8638. * DYTC Platform Profile interface
  8639. */
  8640. #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
  8641. #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */
  8642. #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
  8643. #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */
  8644. #define DYTC_FC_MMC 27 /* MMC Mode supported */
  8645. #define DYTC_FC_PSC 29 /* PSC Mode supported */
  8646. #define DYTC_FC_AMT 31 /* AMT mode supported */
  8647. #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
  8648. #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
  8649. #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
  8650. #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
  8651. #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
  8652. #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
  8653. #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
  8654. #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */
  8655. #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */
  8656. #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */
  8657. #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */
  8658. #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */
  8659. #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */
  8660. #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */
  8661. #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */
  8662. #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */
  8663. #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */
  8664. #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */
  8665. #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */
  8666. #define DYTC_MODE_PSCV9_LOWPOWER 1 /* Low power mode */
  8667. #define DYTC_MODE_PSCV9_BALANCE 3 /* Default mode aka balanced */
  8668. #define DYTC_MODE_PSCV9_PERFORM 4 /* High power mode aka performance */
  8669. #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */
  8670. #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */
  8671. #define DYTC_SET_COMMAND(function, mode, on) \
  8672. (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
  8673. (mode) << DYTC_SET_MODE_BIT | \
  8674. (on) << DYTC_SET_VALID_BIT)
  8675. #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0)
  8676. #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1)
  8677. static int dytc_control_amt(bool enable);
  8678. static bool dytc_amt_active;
  8679. static enum platform_profile_option dytc_current_profile;
  8680. static atomic_t dytc_ignore_event = ATOMIC_INIT(0);
  8681. static DEFINE_MUTEX(dytc_mutex);
  8682. static int dytc_capabilities;
  8683. static bool dytc_mmc_get_available;
  8684. static int profile_force;
  8685. static int platform_psc_profile_lowpower = DYTC_MODE_PSC_LOWPOWER;
  8686. static int platform_psc_profile_balanced = DYTC_MODE_PSC_BALANCE;
  8687. static int platform_psc_profile_performance = DYTC_MODE_PSC_PERFORM;
  8688. static int convert_dytc_to_profile(int funcmode, int dytcmode,
  8689. enum platform_profile_option *profile)
  8690. {
  8691. switch (funcmode) {
  8692. case DYTC_FUNCTION_MMC:
  8693. switch (dytcmode) {
  8694. case DYTC_MODE_MMC_LOWPOWER:
  8695. *profile = PLATFORM_PROFILE_LOW_POWER;
  8696. break;
  8697. case DYTC_MODE_MMC_DEFAULT:
  8698. case DYTC_MODE_MMC_BALANCE:
  8699. *profile = PLATFORM_PROFILE_BALANCED;
  8700. break;
  8701. case DYTC_MODE_MMC_PERFORM:
  8702. *profile = PLATFORM_PROFILE_PERFORMANCE;
  8703. break;
  8704. default: /* Unknown mode */
  8705. return -EINVAL;
  8706. }
  8707. return 0;
  8708. case DYTC_FUNCTION_PSC:
  8709. if (dytcmode == platform_psc_profile_lowpower)
  8710. *profile = PLATFORM_PROFILE_LOW_POWER;
  8711. else if (dytcmode == platform_psc_profile_balanced)
  8712. *profile = PLATFORM_PROFILE_BALANCED;
  8713. else if (dytcmode == platform_psc_profile_performance)
  8714. *profile = PLATFORM_PROFILE_PERFORMANCE;
  8715. else
  8716. return -EINVAL;
  8717. return 0;
  8718. case DYTC_FUNCTION_AMT:
  8719. /* For now return balanced. It's the closest we have to 'auto' */
  8720. *profile = PLATFORM_PROFILE_BALANCED;
  8721. return 0;
  8722. default:
  8723. /* Unknown function */
  8724. pr_debug("unknown function 0x%x\n", funcmode);
  8725. return -EOPNOTSUPP;
  8726. }
  8727. return 0;
  8728. }
  8729. static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
  8730. {
  8731. switch (profile) {
  8732. case PLATFORM_PROFILE_LOW_POWER:
  8733. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8734. *perfmode = DYTC_MODE_MMC_LOWPOWER;
  8735. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8736. *perfmode = platform_psc_profile_lowpower;
  8737. break;
  8738. case PLATFORM_PROFILE_BALANCED:
  8739. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8740. *perfmode = DYTC_MODE_MMC_BALANCE;
  8741. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8742. *perfmode = platform_psc_profile_balanced;
  8743. break;
  8744. case PLATFORM_PROFILE_PERFORMANCE:
  8745. if (dytc_capabilities & BIT(DYTC_FC_MMC))
  8746. *perfmode = DYTC_MODE_MMC_PERFORM;
  8747. else if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8748. *perfmode = platform_psc_profile_performance;
  8749. break;
  8750. default: /* Unknown profile */
  8751. return -EOPNOTSUPP;
  8752. }
  8753. return 0;
  8754. }
  8755. /*
  8756. * dytc_profile_get: Function to register with platform_profile
  8757. * handler. Returns current platform profile.
  8758. */
  8759. static int dytc_profile_get(struct platform_profile_handler *pprof,
  8760. enum platform_profile_option *profile)
  8761. {
  8762. *profile = dytc_current_profile;
  8763. return 0;
  8764. }
  8765. static int dytc_control_amt(bool enable)
  8766. {
  8767. int dummy;
  8768. int err;
  8769. int cmd;
  8770. if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) {
  8771. pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n");
  8772. return -ENODEV;
  8773. }
  8774. if (enable)
  8775. cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable);
  8776. else
  8777. cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable);
  8778. pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd);
  8779. err = dytc_command(cmd, &dummy);
  8780. if (err)
  8781. return err;
  8782. dytc_amt_active = enable;
  8783. return 0;
  8784. }
  8785. /*
  8786. * Helper function - check if we are in CQL mode and if we are
  8787. * - disable CQL,
  8788. * - run the command
  8789. * - enable CQL
  8790. * If not in CQL mode, just run the command
  8791. */
  8792. static int dytc_cql_command(int command, int *output)
  8793. {
  8794. int err, cmd_err, dummy;
  8795. int cur_funcmode;
  8796. /* Determine if we are in CQL mode. This alters the commands we do */
  8797. err = dytc_command(DYTC_CMD_GET, output);
  8798. if (err)
  8799. return err;
  8800. cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
  8801. /* Check if we're OK to return immediately */
  8802. if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL))
  8803. return 0;
  8804. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  8805. atomic_inc(&dytc_ignore_event);
  8806. err = dytc_command(DYTC_DISABLE_CQL, &dummy);
  8807. if (err)
  8808. return err;
  8809. }
  8810. cmd_err = dytc_command(command, output);
  8811. /* Check return condition after we've restored CQL state */
  8812. if (cur_funcmode == DYTC_FUNCTION_CQL) {
  8813. err = dytc_command(DYTC_ENABLE_CQL, &dummy);
  8814. if (err)
  8815. return err;
  8816. }
  8817. return cmd_err;
  8818. }
  8819. /*
  8820. * dytc_profile_set: Function to register with platform_profile
  8821. * handler. Sets current platform profile.
  8822. */
  8823. static int dytc_profile_set(struct platform_profile_handler *pprof,
  8824. enum platform_profile_option profile)
  8825. {
  8826. int perfmode;
  8827. int output;
  8828. int err;
  8829. err = mutex_lock_interruptible(&dytc_mutex);
  8830. if (err)
  8831. return err;
  8832. err = convert_profile_to_dytc(profile, &perfmode);
  8833. if (err)
  8834. goto unlock;
  8835. if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
  8836. if (profile == PLATFORM_PROFILE_BALANCED) {
  8837. /*
  8838. * To get back to balanced mode we need to issue a reset command.
  8839. * Note we still need to disable CQL mode before hand and re-enable
  8840. * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays
  8841. * stuck at 0 for aprox. 30 minutes.
  8842. */
  8843. err = dytc_cql_command(DYTC_CMD_RESET, &output);
  8844. if (err)
  8845. goto unlock;
  8846. } else {
  8847. /* Determine if we are in CQL mode. This alters the commands we do */
  8848. err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
  8849. &output);
  8850. if (err)
  8851. goto unlock;
  8852. }
  8853. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
  8854. err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output);
  8855. if (err)
  8856. goto unlock;
  8857. /* system supports AMT, activate it when on balanced */
  8858. if (dytc_capabilities & BIT(DYTC_FC_AMT))
  8859. dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED);
  8860. }
  8861. /* Success - update current profile */
  8862. dytc_current_profile = profile;
  8863. unlock:
  8864. mutex_unlock(&dytc_mutex);
  8865. return err;
  8866. }
  8867. static void dytc_profile_refresh(void)
  8868. {
  8869. enum platform_profile_option profile;
  8870. int output = 0, err = 0;
  8871. int perfmode, funcmode = 0;
  8872. mutex_lock(&dytc_mutex);
  8873. if (dytc_capabilities & BIT(DYTC_FC_MMC)) {
  8874. if (dytc_mmc_get_available)
  8875. err = dytc_command(DYTC_CMD_MMC_GET, &output);
  8876. else
  8877. err = dytc_cql_command(DYTC_CMD_GET, &output);
  8878. funcmode = DYTC_FUNCTION_MMC;
  8879. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) {
  8880. err = dytc_command(DYTC_CMD_GET, &output);
  8881. /* Check if we are PSC mode, or have AMT enabled */
  8882. funcmode = (output >> DYTC_GET_FUNCTION_BIT) & 0xF;
  8883. } else { /* Unknown profile mode */
  8884. err = -ENODEV;
  8885. }
  8886. mutex_unlock(&dytc_mutex);
  8887. if (err)
  8888. return;
  8889. perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
  8890. err = convert_dytc_to_profile(funcmode, perfmode, &profile);
  8891. if (!err && profile != dytc_current_profile) {
  8892. dytc_current_profile = profile;
  8893. platform_profile_notify();
  8894. }
  8895. }
  8896. static struct platform_profile_handler dytc_profile = {
  8897. .profile_get = dytc_profile_get,
  8898. .profile_set = dytc_profile_set,
  8899. };
  8900. static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm)
  8901. {
  8902. int err, output;
  8903. /* Setup supported modes */
  8904. set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices);
  8905. set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices);
  8906. set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices);
  8907. err = dytc_command(DYTC_CMD_QUERY, &output);
  8908. if (err)
  8909. return err;
  8910. if (output & BIT(DYTC_QUERY_ENABLE_BIT))
  8911. dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
  8912. dbg_printk(TPACPI_DBG_INIT, "DYTC version %d\n", dytc_version);
  8913. /* Check DYTC is enabled and supports mode setting */
  8914. if (dytc_version < 5)
  8915. return -ENODEV;
  8916. /* Check what capabilities are supported */
  8917. err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities);
  8918. if (err)
  8919. return err;
  8920. /* Check if user wants to override the profile selection */
  8921. if (profile_force) {
  8922. switch (profile_force) {
  8923. case -1:
  8924. dytc_capabilities = 0;
  8925. break;
  8926. case 1:
  8927. dytc_capabilities = BIT(DYTC_FC_MMC);
  8928. break;
  8929. case 2:
  8930. dytc_capabilities = BIT(DYTC_FC_PSC);
  8931. break;
  8932. }
  8933. pr_debug("Profile selection forced: 0x%x\n", dytc_capabilities);
  8934. }
  8935. if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */
  8936. pr_debug("MMC is supported\n");
  8937. /*
  8938. * Check if MMC_GET functionality available
  8939. * Version > 6 and return success from MMC_GET command
  8940. */
  8941. dytc_mmc_get_available = false;
  8942. if (dytc_version >= 6) {
  8943. err = dytc_command(DYTC_CMD_MMC_GET, &output);
  8944. if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS))
  8945. dytc_mmc_get_available = true;
  8946. }
  8947. } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */
  8948. pr_debug("PSC is supported\n");
  8949. if (dytc_version >= 9) { /* update profiles for DYTC 9 and up */
  8950. platform_psc_profile_lowpower = DYTC_MODE_PSCV9_LOWPOWER;
  8951. platform_psc_profile_balanced = DYTC_MODE_PSCV9_BALANCE;
  8952. platform_psc_profile_performance = DYTC_MODE_PSCV9_PERFORM;
  8953. }
  8954. } else {
  8955. dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n");
  8956. return -ENODEV;
  8957. }
  8958. dbg_printk(TPACPI_DBG_INIT,
  8959. "DYTC version %d: thermal mode available\n", dytc_version);
  8960. /* Create platform_profile structure and register */
  8961. err = platform_profile_register(&dytc_profile);
  8962. /*
  8963. * If for some reason platform_profiles aren't enabled
  8964. * don't quit terminally.
  8965. */
  8966. if (err)
  8967. return -ENODEV;
  8968. /* Ensure initial values are correct */
  8969. dytc_profile_refresh();
  8970. /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */
  8971. if (dytc_capabilities & BIT(DYTC_FC_PSC))
  8972. dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
  8973. return 0;
  8974. }
  8975. static void dytc_profile_exit(void)
  8976. {
  8977. platform_profile_remove();
  8978. }
  8979. static struct ibm_struct dytc_profile_driver_data = {
  8980. .name = "dytc-profile",
  8981. .exit = dytc_profile_exit,
  8982. };
  8983. /*************************************************************************
  8984. * Keyboard language interface
  8985. */
  8986. struct keyboard_lang_data {
  8987. const char *lang_str;
  8988. int lang_code;
  8989. };
  8990. static const struct keyboard_lang_data keyboard_lang_data[] = {
  8991. {"be", 0x080c},
  8992. {"cz", 0x0405},
  8993. {"da", 0x0406},
  8994. {"de", 0x0c07},
  8995. {"en", 0x0000},
  8996. {"es", 0x2c0a},
  8997. {"et", 0x0425},
  8998. {"fr", 0x040c},
  8999. {"fr-ch", 0x100c},
  9000. {"hu", 0x040e},
  9001. {"it", 0x0410},
  9002. {"jp", 0x0411},
  9003. {"nl", 0x0413},
  9004. {"nn", 0x0414},
  9005. {"pl", 0x0415},
  9006. {"pt", 0x0816},
  9007. {"sl", 0x041b},
  9008. {"sv", 0x081d},
  9009. {"tr", 0x041f},
  9010. };
  9011. static int set_keyboard_lang_command(int command)
  9012. {
  9013. acpi_handle sskl_handle;
  9014. int output;
  9015. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) {
  9016. /* Platform doesn't support SSKL */
  9017. return -ENODEV;
  9018. }
  9019. if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command))
  9020. return -EIO;
  9021. return 0;
  9022. }
  9023. static int get_keyboard_lang(int *output)
  9024. {
  9025. acpi_handle gskl_handle;
  9026. int kbd_lang;
  9027. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) {
  9028. /* Platform doesn't support GSKL */
  9029. return -ENODEV;
  9030. }
  9031. if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000))
  9032. return -EIO;
  9033. /*
  9034. * METHOD_ERR gets returned on devices where there are no special (e.g. '=',
  9035. * '(' and ')') keys which use layout dependent key-press emulation.
  9036. */
  9037. if (kbd_lang & METHOD_ERR)
  9038. return -ENODEV;
  9039. *output = kbd_lang;
  9040. return 0;
  9041. }
  9042. /* sysfs keyboard language entry */
  9043. static ssize_t keyboard_lang_show(struct device *dev,
  9044. struct device_attribute *attr,
  9045. char *buf)
  9046. {
  9047. int output, err, i, len = 0;
  9048. err = get_keyboard_lang(&output);
  9049. if (err)
  9050. return err;
  9051. for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
  9052. if (i)
  9053. len += sysfs_emit_at(buf, len, "%s", " ");
  9054. if (output == keyboard_lang_data[i].lang_code) {
  9055. len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str);
  9056. } else {
  9057. len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str);
  9058. }
  9059. }
  9060. len += sysfs_emit_at(buf, len, "\n");
  9061. return len;
  9062. }
  9063. static ssize_t keyboard_lang_store(struct device *dev,
  9064. struct device_attribute *attr,
  9065. const char *buf, size_t count)
  9066. {
  9067. int err, i;
  9068. bool lang_found = false;
  9069. int lang_code = 0;
  9070. for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) {
  9071. if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) {
  9072. lang_code = keyboard_lang_data[i].lang_code;
  9073. lang_found = true;
  9074. break;
  9075. }
  9076. }
  9077. if (lang_found) {
  9078. lang_code = lang_code | 1 << 24;
  9079. /* Set language code */
  9080. err = set_keyboard_lang_command(lang_code);
  9081. if (err)
  9082. return err;
  9083. } else {
  9084. dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n");
  9085. return -EINVAL;
  9086. }
  9087. tpacpi_disclose_usertask(attr->attr.name,
  9088. "keyboard language is set to %s\n", buf);
  9089. sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang");
  9090. return count;
  9091. }
  9092. static DEVICE_ATTR_RW(keyboard_lang);
  9093. static struct attribute *kbdlang_attributes[] = {
  9094. &dev_attr_keyboard_lang.attr,
  9095. NULL
  9096. };
  9097. static umode_t kbdlang_attr_is_visible(struct kobject *kobj,
  9098. struct attribute *attr, int n)
  9099. {
  9100. return tp_features.kbd_lang ? attr->mode : 0;
  9101. }
  9102. static const struct attribute_group kbdlang_attr_group = {
  9103. .is_visible = kbdlang_attr_is_visible,
  9104. .attrs = kbdlang_attributes,
  9105. };
  9106. static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm)
  9107. {
  9108. int err, output;
  9109. err = get_keyboard_lang(&output);
  9110. tp_features.kbd_lang = !err;
  9111. return err;
  9112. }
  9113. static struct ibm_struct kbdlang_driver_data = {
  9114. .name = "kbdlang",
  9115. };
  9116. /*************************************************************************
  9117. * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN
  9118. * and WLAN feature.
  9119. */
  9120. #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000
  9121. #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4)
  9122. #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8)
  9123. static bool has_antennatype;
  9124. static int wwan_antennatype;
  9125. static int dprc_command(int command, int *output)
  9126. {
  9127. acpi_handle dprc_handle;
  9128. if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) {
  9129. /* Platform doesn't support DPRC */
  9130. return -ENODEV;
  9131. }
  9132. if (!acpi_evalf(dprc_handle, output, NULL, "dd", command))
  9133. return -EIO;
  9134. /*
  9135. * METHOD_ERR gets returned on devices where few commands are not supported
  9136. * for example command to get WWAN Antenna type command is not supported on
  9137. * some devices.
  9138. */
  9139. if (*output & METHOD_ERR)
  9140. return -ENODEV;
  9141. return 0;
  9142. }
  9143. static int get_wwan_antenna(int *wwan_antennatype)
  9144. {
  9145. int output, err;
  9146. /* Get current Antenna type */
  9147. err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output);
  9148. if (err)
  9149. return err;
  9150. if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT)
  9151. *wwan_antennatype = 1;
  9152. else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT)
  9153. *wwan_antennatype = 2;
  9154. else
  9155. return -ENODEV;
  9156. return 0;
  9157. }
  9158. /* sysfs wwan antenna type entry */
  9159. static ssize_t wwan_antenna_type_show(struct device *dev,
  9160. struct device_attribute *attr,
  9161. char *buf)
  9162. {
  9163. switch (wwan_antennatype) {
  9164. case 1:
  9165. return sysfs_emit(buf, "type a\n");
  9166. case 2:
  9167. return sysfs_emit(buf, "type b\n");
  9168. default:
  9169. return -ENODATA;
  9170. }
  9171. }
  9172. static DEVICE_ATTR_RO(wwan_antenna_type);
  9173. static struct attribute *dprc_attributes[] = {
  9174. &dev_attr_wwan_antenna_type.attr,
  9175. NULL
  9176. };
  9177. static umode_t dprc_attr_is_visible(struct kobject *kobj,
  9178. struct attribute *attr, int n)
  9179. {
  9180. return has_antennatype ? attr->mode : 0;
  9181. }
  9182. static const struct attribute_group dprc_attr_group = {
  9183. .is_visible = dprc_attr_is_visible,
  9184. .attrs = dprc_attributes,
  9185. };
  9186. static int tpacpi_dprc_init(struct ibm_init_struct *iibm)
  9187. {
  9188. int err;
  9189. err = get_wwan_antenna(&wwan_antennatype);
  9190. if (err)
  9191. return err;
  9192. has_antennatype = true;
  9193. return 0;
  9194. }
  9195. static struct ibm_struct dprc_driver_data = {
  9196. .name = "dprc",
  9197. };
  9198. /*
  9199. * Auxmac
  9200. *
  9201. * This auxiliary mac address is enabled in the bios through the
  9202. * MAC Address Pass-through feature. In most cases, there are three
  9203. * possibilities: Internal Mac, Second Mac, and disabled.
  9204. *
  9205. */
  9206. #define AUXMAC_LEN 12
  9207. #define AUXMAC_START 9
  9208. #define AUXMAC_STRLEN 22
  9209. #define AUXMAC_BEGIN_MARKER 8
  9210. #define AUXMAC_END_MARKER 21
  9211. static char auxmac[AUXMAC_LEN + 1];
  9212. static int auxmac_init(struct ibm_init_struct *iibm)
  9213. {
  9214. acpi_status status;
  9215. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  9216. union acpi_object *obj;
  9217. status = acpi_evaluate_object(NULL, "\\MACA", NULL, &buffer);
  9218. if (ACPI_FAILURE(status))
  9219. return -ENODEV;
  9220. obj = buffer.pointer;
  9221. if (obj->type != ACPI_TYPE_STRING || obj->string.length != AUXMAC_STRLEN) {
  9222. pr_info("Invalid buffer for MAC address pass-through.\n");
  9223. goto auxmacinvalid;
  9224. }
  9225. if (obj->string.pointer[AUXMAC_BEGIN_MARKER] != '#' ||
  9226. obj->string.pointer[AUXMAC_END_MARKER] != '#') {
  9227. pr_info("Invalid header for MAC address pass-through.\n");
  9228. goto auxmacinvalid;
  9229. }
  9230. if (strncmp(obj->string.pointer + AUXMAC_START, "XXXXXXXXXXXX", AUXMAC_LEN) != 0)
  9231. strscpy(auxmac, obj->string.pointer + AUXMAC_START, sizeof(auxmac));
  9232. else
  9233. strscpy(auxmac, "disabled", sizeof(auxmac));
  9234. free:
  9235. kfree(obj);
  9236. return 0;
  9237. auxmacinvalid:
  9238. strscpy(auxmac, "unavailable", sizeof(auxmac));
  9239. goto free;
  9240. }
  9241. static struct ibm_struct auxmac_data = {
  9242. .name = "auxmac",
  9243. };
  9244. static DEVICE_STRING_ATTR_RO(auxmac, 0444, auxmac);
  9245. static umode_t auxmac_attr_is_visible(struct kobject *kobj,
  9246. struct attribute *attr, int n)
  9247. {
  9248. return auxmac[0] == 0 ? 0 : attr->mode;
  9249. }
  9250. static struct attribute *auxmac_attributes[] = {
  9251. &dev_attr_auxmac.attr.attr,
  9252. NULL
  9253. };
  9254. static const struct attribute_group auxmac_attr_group = {
  9255. .is_visible = auxmac_attr_is_visible,
  9256. .attrs = auxmac_attributes,
  9257. };
  9258. /* --------------------------------------------------------------------- */
  9259. static struct attribute *tpacpi_driver_attributes[] = {
  9260. &driver_attr_debug_level.attr,
  9261. &driver_attr_version.attr,
  9262. &driver_attr_interface_version.attr,
  9263. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9264. &driver_attr_wlsw_emulstate.attr,
  9265. &driver_attr_bluetooth_emulstate.attr,
  9266. &driver_attr_wwan_emulstate.attr,
  9267. &driver_attr_uwb_emulstate.attr,
  9268. #endif
  9269. NULL
  9270. };
  9271. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9272. static umode_t tpacpi_attr_is_visible(struct kobject *kobj,
  9273. struct attribute *attr, int n)
  9274. {
  9275. if (attr == &driver_attr_wlsw_emulstate.attr) {
  9276. if (!dbg_wlswemul)
  9277. return 0;
  9278. } else if (attr == &driver_attr_bluetooth_emulstate.attr) {
  9279. if (!dbg_bluetoothemul)
  9280. return 0;
  9281. } else if (attr == &driver_attr_wwan_emulstate.attr) {
  9282. if (!dbg_wwanemul)
  9283. return 0;
  9284. } else if (attr == &driver_attr_uwb_emulstate.attr) {
  9285. if (!dbg_uwbemul)
  9286. return 0;
  9287. }
  9288. return attr->mode;
  9289. }
  9290. #endif
  9291. static const struct attribute_group tpacpi_driver_attr_group = {
  9292. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9293. .is_visible = tpacpi_attr_is_visible,
  9294. #endif
  9295. .attrs = tpacpi_driver_attributes,
  9296. };
  9297. static const struct attribute_group *tpacpi_driver_groups[] = {
  9298. &tpacpi_driver_attr_group,
  9299. NULL,
  9300. };
  9301. static const struct attribute_group *tpacpi_groups[] = {
  9302. &adaptive_kbd_attr_group,
  9303. &hotkey_attr_group,
  9304. &bluetooth_attr_group,
  9305. &wan_attr_group,
  9306. &cmos_attr_group,
  9307. &proxsensor_attr_group,
  9308. &kbdlang_attr_group,
  9309. &dprc_attr_group,
  9310. &auxmac_attr_group,
  9311. NULL,
  9312. };
  9313. static const struct attribute_group *tpacpi_hwmon_groups[] = {
  9314. &thermal_attr_group,
  9315. &temp_label_attr_group,
  9316. &fan_attr_group,
  9317. NULL,
  9318. };
  9319. static const struct attribute_group *tpacpi_hwmon_driver_groups[] = {
  9320. &fan_driver_attr_group,
  9321. NULL,
  9322. };
  9323. /****************************************************************************
  9324. ****************************************************************************
  9325. *
  9326. * Platform drivers
  9327. *
  9328. ****************************************************************************
  9329. ****************************************************************************/
  9330. static struct platform_driver tpacpi_pdriver = {
  9331. .driver = {
  9332. .name = TPACPI_DRVR_NAME,
  9333. .pm = &tpacpi_pm,
  9334. .groups = tpacpi_driver_groups,
  9335. .dev_groups = tpacpi_groups,
  9336. },
  9337. .shutdown = tpacpi_shutdown_handler,
  9338. };
  9339. static struct platform_driver tpacpi_hwmon_pdriver = {
  9340. .driver = {
  9341. .name = TPACPI_HWMON_DRVR_NAME,
  9342. .groups = tpacpi_hwmon_driver_groups,
  9343. },
  9344. };
  9345. /****************************************************************************
  9346. ****************************************************************************
  9347. *
  9348. * Infrastructure
  9349. *
  9350. ****************************************************************************
  9351. ****************************************************************************/
  9352. /*
  9353. * HKEY event callout for other subdrivers go here
  9354. * (yes, it is ugly, but it is quick, safe, and gets the job done
  9355. */
  9356. static bool tpacpi_driver_event(const unsigned int hkey_event)
  9357. {
  9358. switch (hkey_event) {
  9359. case TP_HKEY_EV_BRGHT_UP:
  9360. case TP_HKEY_EV_BRGHT_DOWN:
  9361. if (ibm_backlight_device)
  9362. tpacpi_brightness_notify_change();
  9363. /*
  9364. * Key press events are suppressed by default hotkey_user_mask
  9365. * and should still be reported if explicitly requested.
  9366. */
  9367. return false;
  9368. case TP_HKEY_EV_VOL_UP:
  9369. case TP_HKEY_EV_VOL_DOWN:
  9370. case TP_HKEY_EV_VOL_MUTE:
  9371. if (alsa_card)
  9372. volume_alsa_notify_change();
  9373. /* Key events are suppressed by default hotkey_user_mask */
  9374. return false;
  9375. case TP_HKEY_EV_KBD_LIGHT:
  9376. if (tp_features.kbdlight) {
  9377. enum led_brightness brightness;
  9378. mutex_lock(&kbdlight_mutex);
  9379. /*
  9380. * Check the brightness actually changed, setting the brightness
  9381. * through kbdlight_set_level() also triggers this event.
  9382. */
  9383. brightness = kbdlight_sysfs_get(NULL);
  9384. if (kbdlight_brightness != brightness) {
  9385. kbdlight_brightness = brightness;
  9386. led_classdev_notify_brightness_hw_changed(
  9387. &tpacpi_led_kbdlight.led_classdev, brightness);
  9388. }
  9389. mutex_unlock(&kbdlight_mutex);
  9390. }
  9391. /* Key events are suppressed by default hotkey_user_mask */
  9392. return false;
  9393. case TP_HKEY_EV_DFR_CHANGE_ROW:
  9394. adaptive_keyboard_change_row();
  9395. return true;
  9396. case TP_HKEY_EV_DFR_S_QUICKVIEW_ROW:
  9397. adaptive_keyboard_s_quickview_row();
  9398. return true;
  9399. case TP_HKEY_EV_THM_CSM_COMPLETED:
  9400. lapsensor_refresh();
  9401. /* If we are already accessing DYTC then skip dytc update */
  9402. if (!atomic_add_unless(&dytc_ignore_event, -1, 0))
  9403. dytc_profile_refresh();
  9404. return true;
  9405. case TP_HKEY_EV_PRIVACYGUARD_TOGGLE:
  9406. if (lcdshadow_dev) {
  9407. enum drm_privacy_screen_status old_hw_state;
  9408. bool changed;
  9409. mutex_lock(&lcdshadow_dev->lock);
  9410. old_hw_state = lcdshadow_dev->hw_state;
  9411. lcdshadow_get_hw_state(lcdshadow_dev);
  9412. changed = lcdshadow_dev->hw_state != old_hw_state;
  9413. mutex_unlock(&lcdshadow_dev->lock);
  9414. if (changed)
  9415. drm_privacy_screen_call_notifier_chain(lcdshadow_dev);
  9416. }
  9417. return true;
  9418. case TP_HKEY_EV_AMT_TOGGLE:
  9419. /* If we're enabling AMT we need to force balanced mode */
  9420. if (!dytc_amt_active)
  9421. /* This will also set AMT mode enabled */
  9422. dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED);
  9423. else
  9424. dytc_control_amt(!dytc_amt_active);
  9425. return true;
  9426. case TP_HKEY_EV_DOUBLETAP_TOGGLE:
  9427. tp_features.trackpoint_doubletap = !tp_features.trackpoint_doubletap;
  9428. return true;
  9429. case TP_HKEY_EV_PROFILE_TOGGLE:
  9430. case TP_HKEY_EV_PROFILE_TOGGLE2:
  9431. platform_profile_cycle();
  9432. return true;
  9433. }
  9434. return false;
  9435. }
  9436. /* --------------------------------------------------------------------- */
  9437. /* /proc support */
  9438. static struct proc_dir_entry *proc_dir;
  9439. /*
  9440. * Module and infrastructure proble, init and exit handling
  9441. */
  9442. static bool force_load;
  9443. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  9444. static const char * __init str_supported(int is_supported)
  9445. {
  9446. static char text_unsupported[] __initdata = "not supported";
  9447. return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
  9448. }
  9449. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  9450. static void ibm_exit(struct ibm_struct *ibm)
  9451. {
  9452. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  9453. list_del_init(&ibm->all_drivers);
  9454. if (ibm->flags.acpi_notify_installed) {
  9455. dbg_printk(TPACPI_DBG_EXIT,
  9456. "%s: acpi_remove_notify_handler\n", ibm->name);
  9457. BUG_ON(!ibm->acpi);
  9458. acpi_remove_notify_handler(*ibm->acpi->handle,
  9459. ibm->acpi->type,
  9460. dispatch_acpi_notify);
  9461. ibm->flags.acpi_notify_installed = 0;
  9462. }
  9463. if (ibm->flags.proc_created) {
  9464. dbg_printk(TPACPI_DBG_EXIT,
  9465. "%s: remove_proc_entry\n", ibm->name);
  9466. remove_proc_entry(ibm->name, proc_dir);
  9467. ibm->flags.proc_created = 0;
  9468. }
  9469. if (ibm->flags.acpi_driver_registered) {
  9470. dbg_printk(TPACPI_DBG_EXIT,
  9471. "%s: acpi_bus_unregister_driver\n", ibm->name);
  9472. BUG_ON(!ibm->acpi);
  9473. acpi_bus_unregister_driver(ibm->acpi->driver);
  9474. kfree(ibm->acpi->driver);
  9475. ibm->acpi->driver = NULL;
  9476. ibm->flags.acpi_driver_registered = 0;
  9477. }
  9478. if (ibm->flags.init_called && ibm->exit) {
  9479. ibm->exit();
  9480. ibm->flags.init_called = 0;
  9481. }
  9482. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  9483. }
  9484. static int __init ibm_init(struct ibm_init_struct *iibm)
  9485. {
  9486. int ret;
  9487. struct ibm_struct *ibm = iibm->data;
  9488. struct proc_dir_entry *entry;
  9489. BUG_ON(ibm == NULL);
  9490. INIT_LIST_HEAD(&ibm->all_drivers);
  9491. if (ibm->flags.experimental && !experimental)
  9492. return 0;
  9493. dbg_printk(TPACPI_DBG_INIT,
  9494. "probing for %s\n", ibm->name);
  9495. if (iibm->init) {
  9496. ret = iibm->init(iibm);
  9497. if (ret > 0 || ret == -ENODEV)
  9498. return 0; /* subdriver functionality not available */
  9499. if (ret)
  9500. return ret;
  9501. ibm->flags.init_called = 1;
  9502. }
  9503. if (ibm->acpi) {
  9504. if (ibm->acpi->hid) {
  9505. ret = register_tpacpi_subdriver(ibm);
  9506. if (ret)
  9507. goto err_out;
  9508. }
  9509. if (ibm->acpi->notify) {
  9510. ret = setup_acpi_notify(ibm);
  9511. if (ret == -ENODEV) {
  9512. pr_notice("disabling subdriver %s\n",
  9513. ibm->name);
  9514. ret = 0;
  9515. goto err_out;
  9516. }
  9517. if (ret < 0)
  9518. goto err_out;
  9519. }
  9520. }
  9521. dbg_printk(TPACPI_DBG_INIT,
  9522. "%s installed\n", ibm->name);
  9523. if (ibm->read) {
  9524. umode_t mode = iibm->base_procfs_mode;
  9525. if (!mode)
  9526. mode = S_IRUGO;
  9527. if (ibm->write)
  9528. mode |= S_IWUSR;
  9529. entry = proc_create_data(ibm->name, mode, proc_dir,
  9530. &dispatch_proc_ops, ibm);
  9531. if (!entry) {
  9532. pr_err("unable to create proc entry %s\n", ibm->name);
  9533. ret = -ENODEV;
  9534. goto err_out;
  9535. }
  9536. ibm->flags.proc_created = 1;
  9537. }
  9538. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  9539. return 0;
  9540. err_out:
  9541. dbg_printk(TPACPI_DBG_INIT,
  9542. "%s: at error exit path with result %d\n",
  9543. ibm->name, ret);
  9544. ibm_exit(ibm);
  9545. return (ret < 0) ? ret : 0;
  9546. }
  9547. /* Probing */
  9548. static char __init tpacpi_parse_fw_id(const char * const s,
  9549. u32 *model, u16 *release)
  9550. {
  9551. int i;
  9552. if (!s || strlen(s) < 8)
  9553. goto invalid;
  9554. for (i = 0; i < 8; i++)
  9555. if (!((s[i] >= '0' && s[i] <= '9') ||
  9556. (s[i] >= 'A' && s[i] <= 'Z')))
  9557. goto invalid;
  9558. /*
  9559. * Most models: xxyTkkWW (#.##c)
  9560. * Ancient 570/600 and -SL lacks (#.##c)
  9561. */
  9562. if (s[3] == 'T' || s[3] == 'N') {
  9563. *model = TPID(s[0], s[1]);
  9564. *release = TPVER(s[4], s[5]);
  9565. return s[2];
  9566. /* New models: xxxyTkkW (#.##c); T550 and some others */
  9567. } else if (s[4] == 'T' || s[4] == 'N') {
  9568. *model = TPID3(s[0], s[1], s[2]);
  9569. *release = TPVER(s[5], s[6]);
  9570. return s[3];
  9571. }
  9572. invalid:
  9573. return '\0';
  9574. }
  9575. #define EC_FW_STRING_LEN 18
  9576. static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
  9577. {
  9578. char *ec_fw_string = (char *) private;
  9579. const char *dmi_data = (const char *)dm;
  9580. /*
  9581. * ThinkPad Embedded Controller Program Table on newer models
  9582. *
  9583. * Offset | Name | Width | Description
  9584. * ----------------------------------------------------
  9585. * 0x00 | Type | BYTE | 0x8C
  9586. * 0x01 | Length | BYTE |
  9587. * 0x02 | Handle | WORD | Varies
  9588. * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
  9589. * 0x0A | OEM struct offset | BYTE | 0x0B
  9590. * 0x0B | OEM struct number | BYTE | 0x07, for this structure
  9591. * 0x0C | OEM struct revision | BYTE | 0x01, for this format
  9592. * 0x0D | ECP version ID | STR ID |
  9593. * 0x0E | ECP release date | STR ID |
  9594. */
  9595. /* Return if data structure not match */
  9596. if (dm->type != 140 || dm->length < 0x0F ||
  9597. memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
  9598. dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
  9599. dmi_data[0x0C] != 0x01)
  9600. return;
  9601. /* fwstr is the first 8byte string */
  9602. BUILD_BUG_ON(EC_FW_STRING_LEN <= 8);
  9603. memcpy(ec_fw_string, dmi_data + 0x0F, 8);
  9604. }
  9605. /* returns 0 - probe ok, or < 0 - probe error.
  9606. * Probe ok doesn't mean thinkpad found.
  9607. * On error, kfree() cleanup on tp->* is not performed, caller must do it */
  9608. static int __must_check __init get_thinkpad_model_data(
  9609. struct thinkpad_id_data *tp)
  9610. {
  9611. const struct dmi_device *dev = NULL;
  9612. char ec_fw_string[EC_FW_STRING_LEN] = {0};
  9613. char const *s;
  9614. char t;
  9615. if (!tp)
  9616. return -EINVAL;
  9617. memset(tp, 0, sizeof(*tp));
  9618. if (dmi_name_in_vendors("IBM"))
  9619. tp->vendor = PCI_VENDOR_ID_IBM;
  9620. else if (dmi_name_in_vendors("LENOVO"))
  9621. tp->vendor = PCI_VENDOR_ID_LENOVO;
  9622. else if (dmi_name_in_vendors("NEC"))
  9623. tp->vendor = PCI_VENDOR_ID_LENOVO;
  9624. else
  9625. return 0;
  9626. s = dmi_get_system_info(DMI_BIOS_VERSION);
  9627. tp->bios_version_str = kstrdup(s, GFP_KERNEL);
  9628. if (s && !tp->bios_version_str)
  9629. return -ENOMEM;
  9630. /* Really ancient ThinkPad 240X will fail this, which is fine */
  9631. t = tpacpi_parse_fw_id(tp->bios_version_str,
  9632. &tp->bios_model, &tp->bios_release);
  9633. if (t != 'E' && t != 'C')
  9634. return 0;
  9635. /*
  9636. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  9637. * X32 or newer, all Z series; Some models must have an
  9638. * up-to-date BIOS or they will not be detected.
  9639. *
  9640. * See https://thinkwiki.org/wiki/List_of_DMI_IDs
  9641. */
  9642. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  9643. if (sscanf(dev->name,
  9644. "IBM ThinkPad Embedded Controller -[%17c",
  9645. ec_fw_string) == 1) {
  9646. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  9647. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  9648. break;
  9649. }
  9650. }
  9651. /* Newer ThinkPads have different EC program info table */
  9652. if (!ec_fw_string[0])
  9653. dmi_walk(find_new_ec_fwstr, &ec_fw_string);
  9654. if (ec_fw_string[0]) {
  9655. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  9656. if (!tp->ec_version_str)
  9657. return -ENOMEM;
  9658. t = tpacpi_parse_fw_id(ec_fw_string,
  9659. &tp->ec_model, &tp->ec_release);
  9660. if (t != 'H') {
  9661. pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
  9662. ec_fw_string);
  9663. pr_notice("please report this to %s\n", TPACPI_MAIL);
  9664. }
  9665. }
  9666. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  9667. if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
  9668. tp->model_str = kstrdup(s, GFP_KERNEL);
  9669. if (!tp->model_str)
  9670. return -ENOMEM;
  9671. } else {
  9672. s = dmi_get_system_info(DMI_BIOS_VENDOR);
  9673. if (s && !(strncasecmp(s, "Lenovo", 6))) {
  9674. tp->model_str = kstrdup(s, GFP_KERNEL);
  9675. if (!tp->model_str)
  9676. return -ENOMEM;
  9677. }
  9678. }
  9679. s = dmi_get_system_info(DMI_PRODUCT_NAME);
  9680. tp->nummodel_str = kstrdup(s, GFP_KERNEL);
  9681. if (s && !tp->nummodel_str)
  9682. return -ENOMEM;
  9683. return 0;
  9684. }
  9685. static int __init probe_for_thinkpad(void)
  9686. {
  9687. int is_thinkpad;
  9688. if (acpi_disabled)
  9689. return -ENODEV;
  9690. /* It would be dangerous to run the driver in this case */
  9691. if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
  9692. return -ENODEV;
  9693. /*
  9694. * Non-ancient models have better DMI tagging, but very old models
  9695. * don't. tpacpi_is_fw_known() is a cheat to help in that case.
  9696. */
  9697. is_thinkpad = (thinkpad_id.model_str != NULL) ||
  9698. (thinkpad_id.ec_model != 0) ||
  9699. tpacpi_is_fw_known();
  9700. /* The EC handler is required */
  9701. tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
  9702. if (!ec_handle) {
  9703. if (is_thinkpad)
  9704. pr_err("Not yet supported ThinkPad detected!\n");
  9705. return -ENODEV;
  9706. }
  9707. if (!is_thinkpad && !force_load)
  9708. return -ENODEV;
  9709. return 0;
  9710. }
  9711. static void __init thinkpad_acpi_init_banner(void)
  9712. {
  9713. pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
  9714. pr_info("%s\n", TPACPI_URL);
  9715. pr_info("ThinkPad BIOS %s, EC %s\n",
  9716. (thinkpad_id.bios_version_str) ?
  9717. thinkpad_id.bios_version_str : "unknown",
  9718. (thinkpad_id.ec_version_str) ?
  9719. thinkpad_id.ec_version_str : "unknown");
  9720. BUG_ON(!thinkpad_id.vendor);
  9721. if (thinkpad_id.model_str)
  9722. pr_info("%s %s, model %s\n",
  9723. (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
  9724. "IBM" : ((thinkpad_id.vendor ==
  9725. PCI_VENDOR_ID_LENOVO) ?
  9726. "Lenovo" : "Unknown vendor"),
  9727. thinkpad_id.model_str,
  9728. (thinkpad_id.nummodel_str) ?
  9729. thinkpad_id.nummodel_str : "unknown");
  9730. }
  9731. /* Module init, exit, parameters */
  9732. static struct ibm_init_struct ibms_init[] __initdata = {
  9733. {
  9734. .data = &thinkpad_acpi_driver_data,
  9735. },
  9736. {
  9737. .init = hotkey_init,
  9738. .data = &hotkey_driver_data,
  9739. },
  9740. {
  9741. .init = bluetooth_init,
  9742. .data = &bluetooth_driver_data,
  9743. },
  9744. {
  9745. .init = wan_init,
  9746. .data = &wan_driver_data,
  9747. },
  9748. {
  9749. .init = uwb_init,
  9750. .data = &uwb_driver_data,
  9751. },
  9752. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  9753. {
  9754. .init = video_init,
  9755. .base_procfs_mode = S_IRUSR,
  9756. .data = &video_driver_data,
  9757. },
  9758. #endif
  9759. {
  9760. .init = kbdlight_init,
  9761. .data = &kbdlight_driver_data,
  9762. },
  9763. {
  9764. .init = light_init,
  9765. .data = &light_driver_data,
  9766. },
  9767. {
  9768. .init = cmos_init,
  9769. .data = &cmos_driver_data,
  9770. },
  9771. {
  9772. .init = led_init,
  9773. .data = &led_driver_data,
  9774. },
  9775. {
  9776. .init = beep_init,
  9777. .data = &beep_driver_data,
  9778. },
  9779. {
  9780. .init = thermal_init,
  9781. .data = &thermal_driver_data,
  9782. },
  9783. {
  9784. .init = brightness_init,
  9785. .data = &brightness_driver_data,
  9786. },
  9787. {
  9788. .init = volume_init,
  9789. .data = &volume_driver_data,
  9790. },
  9791. {
  9792. .init = fan_init,
  9793. .data = &fan_driver_data,
  9794. },
  9795. {
  9796. .init = mute_led_init,
  9797. .data = &mute_led_driver_data,
  9798. },
  9799. {
  9800. .init = tpacpi_battery_init,
  9801. .data = &battery_driver_data,
  9802. },
  9803. {
  9804. .init = tpacpi_lcdshadow_init,
  9805. .data = &lcdshadow_driver_data,
  9806. },
  9807. {
  9808. .init = tpacpi_proxsensor_init,
  9809. .data = &proxsensor_driver_data,
  9810. },
  9811. {
  9812. .init = tpacpi_dytc_profile_init,
  9813. .data = &dytc_profile_driver_data,
  9814. },
  9815. {
  9816. .init = tpacpi_kbdlang_init,
  9817. .data = &kbdlang_driver_data,
  9818. },
  9819. {
  9820. .init = tpacpi_dprc_init,
  9821. .data = &dprc_driver_data,
  9822. },
  9823. {
  9824. .init = auxmac_init,
  9825. .data = &auxmac_data,
  9826. },
  9827. };
  9828. static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
  9829. {
  9830. unsigned int i;
  9831. struct ibm_struct *ibm;
  9832. if (!kp || !kp->name || !val)
  9833. return -EINVAL;
  9834. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  9835. ibm = ibms_init[i].data;
  9836. if (!ibm || !ibm->name)
  9837. continue;
  9838. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  9839. if (strlen(val) > sizeof(ibms_init[i].param) - 1)
  9840. return -ENOSPC;
  9841. strcpy(ibms_init[i].param, val);
  9842. return 0;
  9843. }
  9844. }
  9845. return -EINVAL;
  9846. }
  9847. module_param(experimental, int, 0444);
  9848. MODULE_PARM_DESC(experimental,
  9849. "Enables experimental features when non-zero");
  9850. module_param_named(debug, dbg_level, uint, 0);
  9851. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  9852. module_param(force_load, bool, 0444);
  9853. MODULE_PARM_DESC(force_load,
  9854. "Attempts to load the driver even on a mis-identified ThinkPad when true");
  9855. module_param_named(fan_control, fan_control_allowed, bool, 0444);
  9856. MODULE_PARM_DESC(fan_control,
  9857. "Enables setting fan parameters features when true");
  9858. module_param_named(brightness_mode, brightness_mode, uint, 0444);
  9859. MODULE_PARM_DESC(brightness_mode,
  9860. "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
  9861. module_param(brightness_enable, uint, 0444);
  9862. MODULE_PARM_DESC(brightness_enable,
  9863. "Enables backlight control when 1, disables when 0");
  9864. #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
  9865. module_param_named(volume_mode, volume_mode, uint, 0444);
  9866. MODULE_PARM_DESC(volume_mode,
  9867. "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
  9868. module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
  9869. MODULE_PARM_DESC(volume_capabilities,
  9870. "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
  9871. module_param_named(volume_control, volume_control_allowed, bool, 0444);
  9872. MODULE_PARM_DESC(volume_control,
  9873. "Enables software override for the console audio control when true");
  9874. module_param_named(software_mute, software_mute_requested, bool, 0444);
  9875. MODULE_PARM_DESC(software_mute,
  9876. "Request full software mute control");
  9877. /* ALSA module API parameters */
  9878. module_param_named(index, alsa_index, int, 0444);
  9879. MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
  9880. module_param_named(id, alsa_id, charp, 0444);
  9881. MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
  9882. module_param_named(enable, alsa_enable, bool, 0444);
  9883. MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
  9884. #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
  9885. /* The module parameter can't be read back, that's why 0 is used here */
  9886. #define TPACPI_PARAM(feature) \
  9887. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  9888. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
  9889. TPACPI_PARAM(hotkey);
  9890. TPACPI_PARAM(bluetooth);
  9891. TPACPI_PARAM(video);
  9892. TPACPI_PARAM(light);
  9893. TPACPI_PARAM(cmos);
  9894. TPACPI_PARAM(led);
  9895. TPACPI_PARAM(beep);
  9896. TPACPI_PARAM(brightness);
  9897. TPACPI_PARAM(volume);
  9898. TPACPI_PARAM(fan);
  9899. #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
  9900. module_param(dbg_wlswemul, uint, 0444);
  9901. MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
  9902. module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
  9903. MODULE_PARM_DESC(wlsw_state,
  9904. "Initial state of the emulated WLSW switch");
  9905. module_param(dbg_bluetoothemul, uint, 0444);
  9906. MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
  9907. module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
  9908. MODULE_PARM_DESC(bluetooth_state,
  9909. "Initial state of the emulated bluetooth switch");
  9910. module_param(dbg_wwanemul, uint, 0444);
  9911. MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
  9912. module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
  9913. MODULE_PARM_DESC(wwan_state,
  9914. "Initial state of the emulated WWAN switch");
  9915. module_param(dbg_uwbemul, uint, 0444);
  9916. MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
  9917. module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
  9918. MODULE_PARM_DESC(uwb_state,
  9919. "Initial state of the emulated UWB switch");
  9920. #endif
  9921. module_param(profile_force, int, 0444);
  9922. MODULE_PARM_DESC(profile_force, "Force profile mode. -1=off, 1=MMC, 2=PSC");
  9923. static void thinkpad_acpi_module_exit(void)
  9924. {
  9925. struct ibm_struct *ibm, *itmp;
  9926. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  9927. if (tpacpi_hwmon)
  9928. hwmon_device_unregister(tpacpi_hwmon);
  9929. if (tp_features.sensors_pdrv_registered)
  9930. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  9931. if (tp_features.platform_drv_registered)
  9932. platform_driver_unregister(&tpacpi_pdriver);
  9933. list_for_each_entry_safe_reverse(ibm, itmp,
  9934. &tpacpi_all_drivers,
  9935. all_drivers) {
  9936. ibm_exit(ibm);
  9937. }
  9938. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  9939. if (tpacpi_inputdev) {
  9940. if (tp_features.input_device_registered)
  9941. input_unregister_device(tpacpi_inputdev);
  9942. else
  9943. input_free_device(tpacpi_inputdev);
  9944. }
  9945. if (tpacpi_sensors_pdev)
  9946. platform_device_unregister(tpacpi_sensors_pdev);
  9947. if (tpacpi_pdev)
  9948. platform_device_unregister(tpacpi_pdev);
  9949. if (proc_dir)
  9950. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  9951. if (tpacpi_wq)
  9952. destroy_workqueue(tpacpi_wq);
  9953. kfree(thinkpad_id.bios_version_str);
  9954. kfree(thinkpad_id.ec_version_str);
  9955. kfree(thinkpad_id.model_str);
  9956. kfree(thinkpad_id.nummodel_str);
  9957. }
  9958. static int __init thinkpad_acpi_module_init(void)
  9959. {
  9960. const struct dmi_system_id *dmi_id;
  9961. int ret, i;
  9962. acpi_object_type obj_type;
  9963. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  9964. /* Driver-level probe */
  9965. ret = get_thinkpad_model_data(&thinkpad_id);
  9966. if (ret) {
  9967. pr_err("unable to get DMI data: %d\n", ret);
  9968. thinkpad_acpi_module_exit();
  9969. return ret;
  9970. }
  9971. ret = probe_for_thinkpad();
  9972. if (ret) {
  9973. thinkpad_acpi_module_exit();
  9974. return ret;
  9975. }
  9976. /* Driver initialization */
  9977. thinkpad_acpi_init_banner();
  9978. tpacpi_check_outdated_fw();
  9979. TPACPI_ACPIHANDLE_INIT(ecrd);
  9980. TPACPI_ACPIHANDLE_INIT(ecwr);
  9981. /*
  9982. * Quirk: in some models (e.g. X380 Yoga), an object named ECRD
  9983. * exists, but it is a register, not a method.
  9984. */
  9985. if (ecrd_handle) {
  9986. acpi_get_type(ecrd_handle, &obj_type);
  9987. if (obj_type != ACPI_TYPE_METHOD)
  9988. ecrd_handle = NULL;
  9989. }
  9990. if (ecwr_handle) {
  9991. acpi_get_type(ecwr_handle, &obj_type);
  9992. if (obj_type != ACPI_TYPE_METHOD)
  9993. ecwr_handle = NULL;
  9994. }
  9995. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  9996. if (!tpacpi_wq) {
  9997. thinkpad_acpi_module_exit();
  9998. return -ENOMEM;
  9999. }
  10000. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  10001. if (!proc_dir) {
  10002. pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
  10003. thinkpad_acpi_module_exit();
  10004. return -ENODEV;
  10005. }
  10006. dmi_id = dmi_first_match(fwbug_list);
  10007. if (dmi_id)
  10008. tp_features.quirks = dmi_id->driver_data;
  10009. /* Device initialization */
  10010. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE,
  10011. NULL, 0);
  10012. if (IS_ERR(tpacpi_pdev)) {
  10013. ret = PTR_ERR(tpacpi_pdev);
  10014. tpacpi_pdev = NULL;
  10015. pr_err("unable to register platform device\n");
  10016. thinkpad_acpi_module_exit();
  10017. return ret;
  10018. }
  10019. tpacpi_sensors_pdev = platform_device_register_simple(
  10020. TPACPI_HWMON_DRVR_NAME,
  10021. PLATFORM_DEVID_NONE, NULL, 0);
  10022. if (IS_ERR(tpacpi_sensors_pdev)) {
  10023. ret = PTR_ERR(tpacpi_sensors_pdev);
  10024. tpacpi_sensors_pdev = NULL;
  10025. pr_err("unable to register hwmon platform device\n");
  10026. thinkpad_acpi_module_exit();
  10027. return ret;
  10028. }
  10029. mutex_init(&tpacpi_inputdev_send_mutex);
  10030. tpacpi_inputdev = input_allocate_device();
  10031. if (!tpacpi_inputdev) {
  10032. thinkpad_acpi_module_exit();
  10033. return -ENOMEM;
  10034. } else {
  10035. /* Prepare input device, but don't register */
  10036. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  10037. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  10038. tpacpi_inputdev->id.bustype = BUS_HOST;
  10039. tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
  10040. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  10041. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  10042. tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
  10043. }
  10044. /* Init subdriver dependencies */
  10045. tpacpi_detect_brightness_capabilities();
  10046. /* Init subdrivers */
  10047. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  10048. ret = ibm_init(&ibms_init[i]);
  10049. if (ret >= 0 && *ibms_init[i].param)
  10050. ret = ibms_init[i].data->write(ibms_init[i].param);
  10051. if (ret < 0) {
  10052. thinkpad_acpi_module_exit();
  10053. return ret;
  10054. }
  10055. }
  10056. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  10057. ret = platform_driver_register(&tpacpi_pdriver);
  10058. if (ret) {
  10059. pr_err("unable to register main platform driver\n");
  10060. thinkpad_acpi_module_exit();
  10061. return ret;
  10062. }
  10063. tp_features.platform_drv_registered = 1;
  10064. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  10065. if (ret) {
  10066. pr_err("unable to register hwmon platform driver\n");
  10067. thinkpad_acpi_module_exit();
  10068. return ret;
  10069. }
  10070. tp_features.sensors_pdrv_registered = 1;
  10071. tpacpi_hwmon = hwmon_device_register_with_groups(
  10072. &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups);
  10073. if (IS_ERR(tpacpi_hwmon)) {
  10074. ret = PTR_ERR(tpacpi_hwmon);
  10075. tpacpi_hwmon = NULL;
  10076. pr_err("unable to register hwmon device\n");
  10077. thinkpad_acpi_module_exit();
  10078. return ret;
  10079. }
  10080. ret = input_register_device(tpacpi_inputdev);
  10081. if (ret < 0) {
  10082. pr_err("unable to register input device\n");
  10083. thinkpad_acpi_module_exit();
  10084. return ret;
  10085. } else {
  10086. tp_features.input_device_registered = 1;
  10087. }
  10088. return 0;
  10089. }
  10090. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  10091. /*
  10092. * This will autoload the driver in almost every ThinkPad
  10093. * in widespread use.
  10094. *
  10095. * Only _VERY_ old models, like the 240, 240x and 570 lack
  10096. * the HKEY event interface.
  10097. */
  10098. MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
  10099. /*
  10100. * DMI matching for module autoloading
  10101. *
  10102. * See https://thinkwiki.org/wiki/List_of_DMI_IDs
  10103. * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  10104. *
  10105. * Only models listed in thinkwiki will be supported, so add yours
  10106. * if it is not there yet.
  10107. */
  10108. #define IBM_BIOS_MODULE_ALIAS(__type) \
  10109. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
  10110. /* Ancient thinkpad BIOSes have to be identified by
  10111. * BIOS type or model number, and there are far less
  10112. * BIOS types than model numbers... */
  10113. IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
  10114. MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
  10115. MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
  10116. MODULE_DESCRIPTION(TPACPI_DESC);
  10117. MODULE_VERSION(TPACPI_VERSION);
  10118. MODULE_LICENSE("GPL");
  10119. module_init(thinkpad_acpi_module_init);
  10120. module_exit(thinkpad_acpi_module_exit);