thinkpad_acpi.c 250 KB

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