thinkpad_acpi.c 231 KB

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