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