thinkpad_acpi.c 183 KB

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