thinkpad_acpi.c 246 KB

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