thinkpad_acpi.c 195 KB

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