thinkpad_acpi.c 230 KB

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