thinkpad_acpi.c 233 KB

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