thinkpad_acpi.c 161 KB

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