thinkpad_acpi.c 183 KB

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