thinkpad_acpi.c 198 KB

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