thinkpad_acpi.c 212 KB

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