thinkpad_acpi.c 227 KB

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