thinkpad_acpi.c 221 KB

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