thinkpad_acpi.c 194 KB

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