thinkpad_acpi.c 265 KB

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