thinkpad_acpi.c 240 KB

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