thinkpad_acpi.c 232 KB

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