thinkpad_acpi.c 160 KB

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