thinkpad_acpi.c 201 KB

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