thinkpad_acpi.c 230 KB

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