thinkpad_acpi.c 194 KB

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