thinkpad_acpi.c 228 KB

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