thinkpad_acpi.c 161 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661
  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. }
  1794. if (tp_features.hotkey_wlsw)
  1795. res = add_to_attr_set(hotkey_dev_attributes,
  1796. &dev_attr_hotkey_radio_sw.attr);
  1797. /* For X41t, X60t, X61t Tablets... */
  1798. if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
  1799. tp_features.hotkey_tablet = 1;
  1800. printk(TPACPI_INFO
  1801. "possible tablet mode switch found; "
  1802. "ThinkPad in %s mode\n",
  1803. (status & TP_HOTKEY_TABLET_MASK)?
  1804. "tablet" : "laptop");
  1805. res = add_to_attr_set(hotkey_dev_attributes,
  1806. &dev_attr_hotkey_tablet_mode.attr);
  1807. }
  1808. if (!res)
  1809. res = register_attr_set_with_sysfs(
  1810. hotkey_dev_attributes,
  1811. &tpacpi_pdev->dev.kobj);
  1812. if (res)
  1813. goto err_exit;
  1814. /* Set up key map */
  1815. hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
  1816. GFP_KERNEL);
  1817. if (!hotkey_keycode_map) {
  1818. printk(TPACPI_ERR
  1819. "failed to allocate memory for key map\n");
  1820. res = -ENOMEM;
  1821. goto err_exit;
  1822. }
  1823. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  1824. dbg_printk(TPACPI_DBG_INIT,
  1825. "using Lenovo default hot key map\n");
  1826. memcpy(hotkey_keycode_map, &lenovo_keycode_map,
  1827. TPACPI_HOTKEY_MAP_SIZE);
  1828. } else {
  1829. dbg_printk(TPACPI_DBG_INIT,
  1830. "using IBM default hot key map\n");
  1831. memcpy(hotkey_keycode_map, &ibm_keycode_map,
  1832. TPACPI_HOTKEY_MAP_SIZE);
  1833. }
  1834. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  1835. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  1836. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  1837. tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
  1838. tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
  1839. tpacpi_inputdev->keycode = hotkey_keycode_map;
  1840. for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
  1841. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  1842. set_bit(hotkey_keycode_map[i],
  1843. tpacpi_inputdev->keybit);
  1844. } else {
  1845. if (i < sizeof(hotkey_reserved_mask)*8)
  1846. hotkey_reserved_mask |= 1 << i;
  1847. }
  1848. }
  1849. if (tp_features.hotkey_wlsw) {
  1850. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1851. set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
  1852. }
  1853. if (tp_features.hotkey_tablet) {
  1854. set_bit(EV_SW, tpacpi_inputdev->evbit);
  1855. set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
  1856. }
  1857. /* Do not issue duplicate brightness change events to
  1858. * userspace */
  1859. if (!tp_features.bright_acpimode)
  1860. /* update bright_acpimode... */
  1861. tpacpi_check_std_acpi_brightness_support();
  1862. if (tp_features.bright_acpimode) {
  1863. printk(TPACPI_INFO
  1864. "This ThinkPad has standard ACPI backlight "
  1865. "brightness control, supported by the ACPI "
  1866. "video driver\n");
  1867. printk(TPACPI_NOTICE
  1868. "Disabling thinkpad-acpi brightness events "
  1869. "by default...\n");
  1870. /* The hotkey_reserved_mask change below is not
  1871. * necessary while the keys are at KEY_RESERVED in the
  1872. * default map, but better safe than sorry, leave it
  1873. * here as a marker of what we have to do, especially
  1874. * when we finally become able to set this at runtime
  1875. * on response to X.org requests */
  1876. hotkey_reserved_mask |=
  1877. (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
  1878. | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
  1879. }
  1880. dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
  1881. res = hotkey_status_set(1);
  1882. if (res) {
  1883. hotkey_exit();
  1884. return res;
  1885. }
  1886. res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
  1887. & ~hotkey_reserved_mask)
  1888. | hotkey_orig_mask);
  1889. if (res < 0 && res != -ENXIO) {
  1890. hotkey_exit();
  1891. return res;
  1892. }
  1893. dbg_printk(TPACPI_DBG_INIT,
  1894. "legacy hot key reporting over procfs %s\n",
  1895. (hotkey_report_mode < 2) ?
  1896. "enabled" : "disabled");
  1897. tpacpi_inputdev->open = &hotkey_inputdev_open;
  1898. tpacpi_inputdev->close = &hotkey_inputdev_close;
  1899. hotkey_poll_setup_safe(1);
  1900. tpacpi_input_send_radiosw();
  1901. tpacpi_input_send_tabletsw();
  1902. return 0;
  1903. err_exit:
  1904. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  1905. hotkey_dev_attributes = NULL;
  1906. return (res < 0)? res : 1;
  1907. }
  1908. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  1909. {
  1910. u32 hkey;
  1911. unsigned int scancode;
  1912. int send_acpi_ev;
  1913. int ignore_acpi_ev;
  1914. int unk_ev;
  1915. if (event != 0x80) {
  1916. printk(TPACPI_ERR
  1917. "unknown HKEY notification event %d\n", event);
  1918. /* forward it to userspace, maybe it knows how to handle it */
  1919. acpi_bus_generate_netlink_event(
  1920. ibm->acpi->device->pnp.device_class,
  1921. ibm->acpi->device->dev.bus_id,
  1922. event, 0);
  1923. return;
  1924. }
  1925. while (1) {
  1926. if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  1927. printk(TPACPI_ERR "failed to retrieve HKEY event\n");
  1928. return;
  1929. }
  1930. if (hkey == 0) {
  1931. /* queue empty */
  1932. return;
  1933. }
  1934. send_acpi_ev = 1;
  1935. ignore_acpi_ev = 0;
  1936. unk_ev = 0;
  1937. switch (hkey >> 12) {
  1938. case 1:
  1939. /* 0x1000-0x1FFF: key presses */
  1940. scancode = hkey & 0xfff;
  1941. if (scancode > 0 && scancode < 0x21) {
  1942. scancode--;
  1943. if (!(hotkey_source_mask & (1 << scancode))) {
  1944. tpacpi_input_send_key(scancode);
  1945. send_acpi_ev = 0;
  1946. } else {
  1947. ignore_acpi_ev = 1;
  1948. }
  1949. } else {
  1950. unk_ev = 1;
  1951. }
  1952. break;
  1953. case 2:
  1954. /* Wakeup reason */
  1955. switch (hkey) {
  1956. case 0x2304: /* suspend, undock */
  1957. case 0x2404: /* hibernation, undock */
  1958. hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
  1959. ignore_acpi_ev = 1;
  1960. break;
  1961. case 0x2305: /* suspend, bay eject */
  1962. case 0x2405: /* hibernation, bay eject */
  1963. hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
  1964. ignore_acpi_ev = 1;
  1965. break;
  1966. default:
  1967. unk_ev = 1;
  1968. }
  1969. if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
  1970. printk(TPACPI_INFO
  1971. "woke up due to a hot-unplug "
  1972. "request...\n");
  1973. hotkey_wakeup_reason_notify_change();
  1974. }
  1975. break;
  1976. case 3:
  1977. /* bay-related wakeups */
  1978. if (hkey == 0x3003) {
  1979. hotkey_autosleep_ack = 1;
  1980. printk(TPACPI_INFO
  1981. "bay ejected\n");
  1982. hotkey_wakeup_hotunplug_complete_notify_change();
  1983. } else {
  1984. unk_ev = 1;
  1985. }
  1986. break;
  1987. case 4:
  1988. /* dock-related wakeups */
  1989. if (hkey == 0x4003) {
  1990. hotkey_autosleep_ack = 1;
  1991. printk(TPACPI_INFO
  1992. "undocked\n");
  1993. hotkey_wakeup_hotunplug_complete_notify_change();
  1994. } else {
  1995. unk_ev = 1;
  1996. }
  1997. break;
  1998. case 5:
  1999. /* 0x5000-0x5FFF: human interface helpers */
  2000. switch (hkey) {
  2001. case 0x5010: /* Lenovo new BIOS: brightness changed */
  2002. case 0x500b: /* X61t: tablet pen inserted into bay */
  2003. case 0x500c: /* X61t: tablet pen removed from bay */
  2004. break;
  2005. case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
  2006. case 0x500a: /* X41t-X61t: swivel down (normal mode) */
  2007. tpacpi_input_send_tabletsw();
  2008. hotkey_tablet_mode_notify_change();
  2009. send_acpi_ev = 0;
  2010. break;
  2011. case 0x5001:
  2012. case 0x5002:
  2013. /* LID switch events. Do not propagate */
  2014. ignore_acpi_ev = 1;
  2015. break;
  2016. default:
  2017. unk_ev = 1;
  2018. }
  2019. break;
  2020. case 7:
  2021. /* 0x7000-0x7FFF: misc */
  2022. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  2023. tpacpi_input_send_radiosw();
  2024. hotkey_radio_sw_notify_change();
  2025. send_acpi_ev = 0;
  2026. break;
  2027. }
  2028. /* fallthrough to default */
  2029. default:
  2030. unk_ev = 1;
  2031. }
  2032. if (unk_ev) {
  2033. printk(TPACPI_NOTICE
  2034. "unhandled HKEY event 0x%04x\n", hkey);
  2035. }
  2036. /* Legacy events */
  2037. if (!ignore_acpi_ev &&
  2038. (send_acpi_ev || hotkey_report_mode < 2)) {
  2039. acpi_bus_generate_proc_event(ibm->acpi->device,
  2040. event, hkey);
  2041. }
  2042. /* netlink events */
  2043. if (!ignore_acpi_ev && send_acpi_ev) {
  2044. acpi_bus_generate_netlink_event(
  2045. ibm->acpi->device->pnp.device_class,
  2046. ibm->acpi->device->dev.bus_id,
  2047. event, hkey);
  2048. }
  2049. }
  2050. }
  2051. static void hotkey_suspend(pm_message_t state)
  2052. {
  2053. /* Do these on suspend, we get the events on early resume! */
  2054. hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
  2055. hotkey_autosleep_ack = 0;
  2056. }
  2057. static void hotkey_resume(void)
  2058. {
  2059. if (hotkey_mask_get())
  2060. printk(TPACPI_ERR
  2061. "error while trying to read hot key mask "
  2062. "from firmware\n");
  2063. tpacpi_input_send_radiosw();
  2064. hotkey_radio_sw_notify_change();
  2065. hotkey_tablet_mode_notify_change();
  2066. hotkey_wakeup_reason_notify_change();
  2067. hotkey_wakeup_hotunplug_complete_notify_change();
  2068. hotkey_poll_setup_safe(0);
  2069. }
  2070. /* procfs -------------------------------------------------------------- */
  2071. static int hotkey_read(char *p)
  2072. {
  2073. int res, status;
  2074. int len = 0;
  2075. if (!tp_features.hotkey) {
  2076. len += sprintf(p + len, "status:\t\tnot supported\n");
  2077. return len;
  2078. }
  2079. if (mutex_lock_interruptible(&hotkey_mutex))
  2080. return -ERESTARTSYS;
  2081. res = hotkey_status_get(&status);
  2082. if (!res)
  2083. res = hotkey_mask_get();
  2084. mutex_unlock(&hotkey_mutex);
  2085. if (res)
  2086. return res;
  2087. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  2088. if (tp_features.hotkey_mask) {
  2089. len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
  2090. len += sprintf(p + len,
  2091. "commands:\tenable, disable, reset, <mask>\n");
  2092. } else {
  2093. len += sprintf(p + len, "mask:\t\tnot supported\n");
  2094. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  2095. }
  2096. return len;
  2097. }
  2098. static int hotkey_write(char *buf)
  2099. {
  2100. int res, status;
  2101. u32 mask;
  2102. char *cmd;
  2103. if (!tp_features.hotkey)
  2104. return -ENODEV;
  2105. if (mutex_lock_interruptible(&hotkey_mutex))
  2106. return -ERESTARTSYS;
  2107. status = -1;
  2108. mask = hotkey_mask;
  2109. res = 0;
  2110. while ((cmd = next_cmd(&buf))) {
  2111. if (strlencmp(cmd, "enable") == 0) {
  2112. status = 1;
  2113. } else if (strlencmp(cmd, "disable") == 0) {
  2114. status = 0;
  2115. } else if (strlencmp(cmd, "reset") == 0) {
  2116. status = hotkey_orig_status;
  2117. mask = hotkey_orig_mask;
  2118. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  2119. /* mask set */
  2120. } else if (sscanf(cmd, "%x", &mask) == 1) {
  2121. /* mask set */
  2122. } else {
  2123. res = -EINVAL;
  2124. goto errexit;
  2125. }
  2126. }
  2127. if (status != -1)
  2128. res = hotkey_status_set(status);
  2129. if (!res && mask != hotkey_mask)
  2130. res = hotkey_mask_set(mask);
  2131. errexit:
  2132. mutex_unlock(&hotkey_mutex);
  2133. return res;
  2134. }
  2135. static const struct acpi_device_id ibm_htk_device_ids[] = {
  2136. {TPACPI_ACPI_HKEY_HID, 0},
  2137. {"", 0},
  2138. };
  2139. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  2140. .hid = ibm_htk_device_ids,
  2141. .notify = hotkey_notify,
  2142. .handle = &hkey_handle,
  2143. .type = ACPI_DEVICE_NOTIFY,
  2144. };
  2145. static struct ibm_struct hotkey_driver_data = {
  2146. .name = "hotkey",
  2147. .read = hotkey_read,
  2148. .write = hotkey_write,
  2149. .exit = hotkey_exit,
  2150. .resume = hotkey_resume,
  2151. .suspend = hotkey_suspend,
  2152. .acpi = &ibm_hotkey_acpidriver,
  2153. };
  2154. /*************************************************************************
  2155. * Bluetooth subdriver
  2156. */
  2157. enum {
  2158. /* ACPI GBDC/SBDC bits */
  2159. TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
  2160. TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
  2161. TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
  2162. };
  2163. static int bluetooth_get_radiosw(void);
  2164. static int bluetooth_set_radiosw(int radio_on);
  2165. /* sysfs bluetooth enable ---------------------------------------------- */
  2166. static ssize_t bluetooth_enable_show(struct device *dev,
  2167. struct device_attribute *attr,
  2168. char *buf)
  2169. {
  2170. int status;
  2171. status = bluetooth_get_radiosw();
  2172. if (status < 0)
  2173. return status;
  2174. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2175. }
  2176. static ssize_t bluetooth_enable_store(struct device *dev,
  2177. struct device_attribute *attr,
  2178. const char *buf, size_t count)
  2179. {
  2180. unsigned long t;
  2181. int res;
  2182. if (parse_strtoul(buf, 1, &t))
  2183. return -EINVAL;
  2184. res = bluetooth_set_radiosw(t);
  2185. return (res) ? res : count;
  2186. }
  2187. static struct device_attribute dev_attr_bluetooth_enable =
  2188. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  2189. bluetooth_enable_show, bluetooth_enable_store);
  2190. /* --------------------------------------------------------------------- */
  2191. static struct attribute *bluetooth_attributes[] = {
  2192. &dev_attr_bluetooth_enable.attr,
  2193. NULL
  2194. };
  2195. static const struct attribute_group bluetooth_attr_group = {
  2196. .attrs = bluetooth_attributes,
  2197. };
  2198. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  2199. {
  2200. int res;
  2201. int status = 0;
  2202. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  2203. TPACPI_ACPIHANDLE_INIT(hkey);
  2204. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  2205. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  2206. tp_features.bluetooth = hkey_handle &&
  2207. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  2208. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  2209. str_supported(tp_features.bluetooth),
  2210. status);
  2211. if (tp_features.bluetooth &&
  2212. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  2213. /* no bluetooth hardware present in system */
  2214. tp_features.bluetooth = 0;
  2215. dbg_printk(TPACPI_DBG_INIT,
  2216. "bluetooth hardware not installed\n");
  2217. }
  2218. if (tp_features.bluetooth) {
  2219. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2220. &bluetooth_attr_group);
  2221. if (res)
  2222. return res;
  2223. }
  2224. return (tp_features.bluetooth)? 0 : 1;
  2225. }
  2226. static void bluetooth_exit(void)
  2227. {
  2228. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2229. &bluetooth_attr_group);
  2230. }
  2231. static int bluetooth_get_radiosw(void)
  2232. {
  2233. int status;
  2234. if (!tp_features.bluetooth)
  2235. return -ENODEV;
  2236. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2237. return -EIO;
  2238. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  2239. }
  2240. static int bluetooth_set_radiosw(int radio_on)
  2241. {
  2242. int status;
  2243. if (!tp_features.bluetooth)
  2244. return -ENODEV;
  2245. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  2246. return -EIO;
  2247. if (radio_on)
  2248. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  2249. else
  2250. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  2251. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  2252. return -EIO;
  2253. return 0;
  2254. }
  2255. /* procfs -------------------------------------------------------------- */
  2256. static int bluetooth_read(char *p)
  2257. {
  2258. int len = 0;
  2259. int status = bluetooth_get_radiosw();
  2260. if (!tp_features.bluetooth)
  2261. len += sprintf(p + len, "status:\t\tnot supported\n");
  2262. else {
  2263. len += sprintf(p + len, "status:\t\t%s\n",
  2264. (status)? "enabled" : "disabled");
  2265. len += sprintf(p + len, "commands:\tenable, disable\n");
  2266. }
  2267. return len;
  2268. }
  2269. static int bluetooth_write(char *buf)
  2270. {
  2271. char *cmd;
  2272. if (!tp_features.bluetooth)
  2273. return -ENODEV;
  2274. while ((cmd = next_cmd(&buf))) {
  2275. if (strlencmp(cmd, "enable") == 0) {
  2276. bluetooth_set_radiosw(1);
  2277. } else if (strlencmp(cmd, "disable") == 0) {
  2278. bluetooth_set_radiosw(0);
  2279. } else
  2280. return -EINVAL;
  2281. }
  2282. return 0;
  2283. }
  2284. static struct ibm_struct bluetooth_driver_data = {
  2285. .name = "bluetooth",
  2286. .read = bluetooth_read,
  2287. .write = bluetooth_write,
  2288. .exit = bluetooth_exit,
  2289. };
  2290. /*************************************************************************
  2291. * Wan subdriver
  2292. */
  2293. enum {
  2294. /* ACPI GWAN/SWAN bits */
  2295. TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
  2296. TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
  2297. TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
  2298. };
  2299. static int wan_get_radiosw(void);
  2300. static int wan_set_radiosw(int radio_on);
  2301. /* sysfs wan enable ---------------------------------------------------- */
  2302. static ssize_t wan_enable_show(struct device *dev,
  2303. struct device_attribute *attr,
  2304. char *buf)
  2305. {
  2306. int status;
  2307. status = wan_get_radiosw();
  2308. if (status < 0)
  2309. return status;
  2310. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  2311. }
  2312. static ssize_t wan_enable_store(struct device *dev,
  2313. struct device_attribute *attr,
  2314. const char *buf, size_t count)
  2315. {
  2316. unsigned long t;
  2317. int res;
  2318. if (parse_strtoul(buf, 1, &t))
  2319. return -EINVAL;
  2320. res = wan_set_radiosw(t);
  2321. return (res) ? res : count;
  2322. }
  2323. static struct device_attribute dev_attr_wan_enable =
  2324. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  2325. wan_enable_show, wan_enable_store);
  2326. /* --------------------------------------------------------------------- */
  2327. static struct attribute *wan_attributes[] = {
  2328. &dev_attr_wan_enable.attr,
  2329. NULL
  2330. };
  2331. static const struct attribute_group wan_attr_group = {
  2332. .attrs = wan_attributes,
  2333. };
  2334. static int __init wan_init(struct ibm_init_struct *iibm)
  2335. {
  2336. int res;
  2337. int status = 0;
  2338. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  2339. TPACPI_ACPIHANDLE_INIT(hkey);
  2340. tp_features.wan = hkey_handle &&
  2341. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  2342. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  2343. str_supported(tp_features.wan),
  2344. status);
  2345. if (tp_features.wan &&
  2346. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  2347. /* no wan hardware present in system */
  2348. tp_features.wan = 0;
  2349. dbg_printk(TPACPI_DBG_INIT,
  2350. "wan hardware not installed\n");
  2351. }
  2352. if (tp_features.wan) {
  2353. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2354. &wan_attr_group);
  2355. if (res)
  2356. return res;
  2357. }
  2358. return (tp_features.wan)? 0 : 1;
  2359. }
  2360. static void wan_exit(void)
  2361. {
  2362. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2363. &wan_attr_group);
  2364. }
  2365. static int wan_get_radiosw(void)
  2366. {
  2367. int status;
  2368. if (!tp_features.wan)
  2369. return -ENODEV;
  2370. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2371. return -EIO;
  2372. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  2373. }
  2374. static int wan_set_radiosw(int radio_on)
  2375. {
  2376. int status;
  2377. if (!tp_features.wan)
  2378. return -ENODEV;
  2379. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  2380. return -EIO;
  2381. if (radio_on)
  2382. status |= TP_ACPI_WANCARD_RADIOSSW;
  2383. else
  2384. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  2385. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  2386. return -EIO;
  2387. return 0;
  2388. }
  2389. /* procfs -------------------------------------------------------------- */
  2390. static int wan_read(char *p)
  2391. {
  2392. int len = 0;
  2393. int status = wan_get_radiosw();
  2394. if (!tp_features.wan)
  2395. len += sprintf(p + len, "status:\t\tnot supported\n");
  2396. else {
  2397. len += sprintf(p + len, "status:\t\t%s\n",
  2398. (status)? "enabled" : "disabled");
  2399. len += sprintf(p + len, "commands:\tenable, disable\n");
  2400. }
  2401. return len;
  2402. }
  2403. static int wan_write(char *buf)
  2404. {
  2405. char *cmd;
  2406. if (!tp_features.wan)
  2407. return -ENODEV;
  2408. while ((cmd = next_cmd(&buf))) {
  2409. if (strlencmp(cmd, "enable") == 0) {
  2410. wan_set_radiosw(1);
  2411. } else if (strlencmp(cmd, "disable") == 0) {
  2412. wan_set_radiosw(0);
  2413. } else
  2414. return -EINVAL;
  2415. }
  2416. return 0;
  2417. }
  2418. static struct ibm_struct wan_driver_data = {
  2419. .name = "wan",
  2420. .read = wan_read,
  2421. .write = wan_write,
  2422. .exit = wan_exit,
  2423. .flags.experimental = 1,
  2424. };
  2425. /*************************************************************************
  2426. * Video subdriver
  2427. */
  2428. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  2429. enum video_access_mode {
  2430. TPACPI_VIDEO_NONE = 0,
  2431. TPACPI_VIDEO_570, /* 570 */
  2432. TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
  2433. TPACPI_VIDEO_NEW, /* all others */
  2434. };
  2435. enum { /* video status flags, based on VIDEO_570 */
  2436. TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
  2437. TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
  2438. TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
  2439. };
  2440. enum { /* TPACPI_VIDEO_570 constants */
  2441. TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
  2442. TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
  2443. * video_status_flags */
  2444. TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
  2445. TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
  2446. };
  2447. static enum video_access_mode video_supported;
  2448. static int video_orig_autosw;
  2449. static int video_autosw_get(void);
  2450. static int video_autosw_set(int enable);
  2451. TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  2452. static int __init video_init(struct ibm_init_struct *iibm)
  2453. {
  2454. int ivga;
  2455. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  2456. TPACPI_ACPIHANDLE_INIT(vid);
  2457. TPACPI_ACPIHANDLE_INIT(vid2);
  2458. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  2459. /* G41, assume IVGA doesn't change */
  2460. vid_handle = vid2_handle;
  2461. if (!vid_handle)
  2462. /* video switching not supported on R30, R31 */
  2463. video_supported = TPACPI_VIDEO_NONE;
  2464. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  2465. /* 570 */
  2466. video_supported = TPACPI_VIDEO_570;
  2467. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  2468. /* 600e/x, 770e, 770x */
  2469. video_supported = TPACPI_VIDEO_770;
  2470. else
  2471. /* all others */
  2472. video_supported = TPACPI_VIDEO_NEW;
  2473. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  2474. str_supported(video_supported != TPACPI_VIDEO_NONE),
  2475. video_supported);
  2476. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  2477. }
  2478. static void video_exit(void)
  2479. {
  2480. dbg_printk(TPACPI_DBG_EXIT,
  2481. "restoring original video autoswitch mode\n");
  2482. if (video_autosw_set(video_orig_autosw))
  2483. printk(TPACPI_ERR "error while trying to restore original "
  2484. "video autoswitch mode\n");
  2485. }
  2486. static int video_outputsw_get(void)
  2487. {
  2488. int status = 0;
  2489. int i;
  2490. switch (video_supported) {
  2491. case TPACPI_VIDEO_570:
  2492. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  2493. TP_ACPI_VIDEO_570_PHSCMD))
  2494. return -EIO;
  2495. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  2496. break;
  2497. case TPACPI_VIDEO_770:
  2498. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2499. return -EIO;
  2500. if (i)
  2501. status |= TP_ACPI_VIDEO_S_LCD;
  2502. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2503. return -EIO;
  2504. if (i)
  2505. status |= TP_ACPI_VIDEO_S_CRT;
  2506. break;
  2507. case TPACPI_VIDEO_NEW:
  2508. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  2509. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  2510. return -EIO;
  2511. if (i)
  2512. status |= TP_ACPI_VIDEO_S_CRT;
  2513. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  2514. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  2515. return -EIO;
  2516. if (i)
  2517. status |= TP_ACPI_VIDEO_S_LCD;
  2518. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  2519. return -EIO;
  2520. if (i)
  2521. status |= TP_ACPI_VIDEO_S_DVI;
  2522. break;
  2523. default:
  2524. return -ENOSYS;
  2525. }
  2526. return status;
  2527. }
  2528. static int video_outputsw_set(int status)
  2529. {
  2530. int autosw;
  2531. int res = 0;
  2532. switch (video_supported) {
  2533. case TPACPI_VIDEO_570:
  2534. res = acpi_evalf(NULL, NULL,
  2535. "\\_SB.PHS2", "vdd",
  2536. TP_ACPI_VIDEO_570_PHS2CMD,
  2537. status | TP_ACPI_VIDEO_570_PHS2SET);
  2538. break;
  2539. case TPACPI_VIDEO_770:
  2540. autosw = video_autosw_get();
  2541. if (autosw < 0)
  2542. return autosw;
  2543. res = video_autosw_set(1);
  2544. if (res)
  2545. return res;
  2546. res = acpi_evalf(vid_handle, NULL,
  2547. "ASWT", "vdd", status * 0x100, 0);
  2548. if (!autosw && video_autosw_set(autosw)) {
  2549. printk(TPACPI_ERR
  2550. "video auto-switch left enabled due to error\n");
  2551. return -EIO;
  2552. }
  2553. break;
  2554. case TPACPI_VIDEO_NEW:
  2555. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  2556. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  2557. break;
  2558. default:
  2559. return -ENOSYS;
  2560. }
  2561. return (res)? 0 : -EIO;
  2562. }
  2563. static int video_autosw_get(void)
  2564. {
  2565. int autosw = 0;
  2566. switch (video_supported) {
  2567. case TPACPI_VIDEO_570:
  2568. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  2569. return -EIO;
  2570. break;
  2571. case TPACPI_VIDEO_770:
  2572. case TPACPI_VIDEO_NEW:
  2573. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  2574. return -EIO;
  2575. break;
  2576. default:
  2577. return -ENOSYS;
  2578. }
  2579. return autosw & 1;
  2580. }
  2581. static int video_autosw_set(int enable)
  2582. {
  2583. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  2584. return -EIO;
  2585. return 0;
  2586. }
  2587. static int video_outputsw_cycle(void)
  2588. {
  2589. int autosw = video_autosw_get();
  2590. int res;
  2591. if (autosw < 0)
  2592. return autosw;
  2593. switch (video_supported) {
  2594. case TPACPI_VIDEO_570:
  2595. res = video_autosw_set(1);
  2596. if (res)
  2597. return res;
  2598. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  2599. break;
  2600. case TPACPI_VIDEO_770:
  2601. case TPACPI_VIDEO_NEW:
  2602. res = video_autosw_set(1);
  2603. if (res)
  2604. return res;
  2605. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  2606. break;
  2607. default:
  2608. return -ENOSYS;
  2609. }
  2610. if (!autosw && video_autosw_set(autosw)) {
  2611. printk(TPACPI_ERR
  2612. "video auto-switch left enabled due to error\n");
  2613. return -EIO;
  2614. }
  2615. return (res)? 0 : -EIO;
  2616. }
  2617. static int video_expand_toggle(void)
  2618. {
  2619. switch (video_supported) {
  2620. case TPACPI_VIDEO_570:
  2621. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  2622. 0 : -EIO;
  2623. case TPACPI_VIDEO_770:
  2624. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  2625. 0 : -EIO;
  2626. case TPACPI_VIDEO_NEW:
  2627. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  2628. 0 : -EIO;
  2629. default:
  2630. return -ENOSYS;
  2631. }
  2632. /* not reached */
  2633. }
  2634. static int video_read(char *p)
  2635. {
  2636. int status, autosw;
  2637. int len = 0;
  2638. if (video_supported == TPACPI_VIDEO_NONE) {
  2639. len += sprintf(p + len, "status:\t\tnot supported\n");
  2640. return len;
  2641. }
  2642. status = video_outputsw_get();
  2643. if (status < 0)
  2644. return status;
  2645. autosw = video_autosw_get();
  2646. if (autosw < 0)
  2647. return autosw;
  2648. len += sprintf(p + len, "status:\t\tsupported\n");
  2649. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  2650. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  2651. if (video_supported == TPACPI_VIDEO_NEW)
  2652. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  2653. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  2654. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  2655. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  2656. if (video_supported == TPACPI_VIDEO_NEW)
  2657. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  2658. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  2659. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  2660. return len;
  2661. }
  2662. static int video_write(char *buf)
  2663. {
  2664. char *cmd;
  2665. int enable, disable, status;
  2666. int res;
  2667. if (video_supported == TPACPI_VIDEO_NONE)
  2668. return -ENODEV;
  2669. enable = 0;
  2670. disable = 0;
  2671. while ((cmd = next_cmd(&buf))) {
  2672. if (strlencmp(cmd, "lcd_enable") == 0) {
  2673. enable |= TP_ACPI_VIDEO_S_LCD;
  2674. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  2675. disable |= TP_ACPI_VIDEO_S_LCD;
  2676. } else if (strlencmp(cmd, "crt_enable") == 0) {
  2677. enable |= TP_ACPI_VIDEO_S_CRT;
  2678. } else if (strlencmp(cmd, "crt_disable") == 0) {
  2679. disable |= TP_ACPI_VIDEO_S_CRT;
  2680. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2681. strlencmp(cmd, "dvi_enable") == 0) {
  2682. enable |= TP_ACPI_VIDEO_S_DVI;
  2683. } else if (video_supported == TPACPI_VIDEO_NEW &&
  2684. strlencmp(cmd, "dvi_disable") == 0) {
  2685. disable |= TP_ACPI_VIDEO_S_DVI;
  2686. } else if (strlencmp(cmd, "auto_enable") == 0) {
  2687. res = video_autosw_set(1);
  2688. if (res)
  2689. return res;
  2690. } else if (strlencmp(cmd, "auto_disable") == 0) {
  2691. res = video_autosw_set(0);
  2692. if (res)
  2693. return res;
  2694. } else if (strlencmp(cmd, "video_switch") == 0) {
  2695. res = video_outputsw_cycle();
  2696. if (res)
  2697. return res;
  2698. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  2699. res = video_expand_toggle();
  2700. if (res)
  2701. return res;
  2702. } else
  2703. return -EINVAL;
  2704. }
  2705. if (enable || disable) {
  2706. status = video_outputsw_get();
  2707. if (status < 0)
  2708. return status;
  2709. res = video_outputsw_set((status & ~disable) | enable);
  2710. if (res)
  2711. return res;
  2712. }
  2713. return 0;
  2714. }
  2715. static struct ibm_struct video_driver_data = {
  2716. .name = "video",
  2717. .read = video_read,
  2718. .write = video_write,
  2719. .exit = video_exit,
  2720. };
  2721. #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
  2722. /*************************************************************************
  2723. * Light (thinklight) subdriver
  2724. */
  2725. TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  2726. TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
  2727. static int light_get_status(void)
  2728. {
  2729. int status = 0;
  2730. if (tp_features.light_status) {
  2731. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  2732. return -EIO;
  2733. return (!!status);
  2734. }
  2735. return -ENXIO;
  2736. }
  2737. static int light_set_status(int status)
  2738. {
  2739. int rc;
  2740. if (tp_features.light) {
  2741. if (cmos_handle) {
  2742. rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
  2743. (status)?
  2744. TP_CMOS_THINKLIGHT_ON :
  2745. TP_CMOS_THINKLIGHT_OFF);
  2746. } else {
  2747. rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
  2748. (status)? 1 : 0);
  2749. }
  2750. return (rc)? 0 : -EIO;
  2751. }
  2752. return -ENXIO;
  2753. }
  2754. static void light_set_status_worker(struct work_struct *work)
  2755. {
  2756. struct tpacpi_led_classdev *data =
  2757. container_of(work, struct tpacpi_led_classdev, work);
  2758. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  2759. light_set_status((data->new_brightness != LED_OFF));
  2760. }
  2761. static void light_sysfs_set(struct led_classdev *led_cdev,
  2762. enum led_brightness brightness)
  2763. {
  2764. struct tpacpi_led_classdev *data =
  2765. container_of(led_cdev,
  2766. struct tpacpi_led_classdev,
  2767. led_classdev);
  2768. data->new_brightness = brightness;
  2769. queue_work(tpacpi_wq, &data->work);
  2770. }
  2771. static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
  2772. {
  2773. return (light_get_status() == 1)? LED_FULL : LED_OFF;
  2774. }
  2775. static struct tpacpi_led_classdev tpacpi_led_thinklight = {
  2776. .led_classdev = {
  2777. .name = "tpacpi::thinklight",
  2778. .brightness_set = &light_sysfs_set,
  2779. .brightness_get = &light_sysfs_get,
  2780. }
  2781. };
  2782. static int __init light_init(struct ibm_init_struct *iibm)
  2783. {
  2784. int rc;
  2785. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  2786. TPACPI_ACPIHANDLE_INIT(ledb);
  2787. TPACPI_ACPIHANDLE_INIT(lght);
  2788. TPACPI_ACPIHANDLE_INIT(cmos);
  2789. INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
  2790. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  2791. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  2792. if (tp_features.light)
  2793. /* light status not supported on
  2794. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  2795. tp_features.light_status =
  2796. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  2797. vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
  2798. str_supported(tp_features.light),
  2799. str_supported(tp_features.light_status));
  2800. if (!tp_features.light)
  2801. return 1;
  2802. rc = led_classdev_register(&tpacpi_pdev->dev,
  2803. &tpacpi_led_thinklight.led_classdev);
  2804. if (rc < 0) {
  2805. tp_features.light = 0;
  2806. tp_features.light_status = 0;
  2807. } else {
  2808. rc = 0;
  2809. }
  2810. return rc;
  2811. }
  2812. static void light_exit(void)
  2813. {
  2814. led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
  2815. if (work_pending(&tpacpi_led_thinklight.work))
  2816. flush_workqueue(tpacpi_wq);
  2817. }
  2818. static int light_read(char *p)
  2819. {
  2820. int len = 0;
  2821. int status;
  2822. if (!tp_features.light) {
  2823. len += sprintf(p + len, "status:\t\tnot supported\n");
  2824. } else if (!tp_features.light_status) {
  2825. len += sprintf(p + len, "status:\t\tunknown\n");
  2826. len += sprintf(p + len, "commands:\ton, off\n");
  2827. } else {
  2828. status = light_get_status();
  2829. if (status < 0)
  2830. return status;
  2831. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  2832. len += sprintf(p + len, "commands:\ton, off\n");
  2833. }
  2834. return len;
  2835. }
  2836. static int light_write(char *buf)
  2837. {
  2838. char *cmd;
  2839. int newstatus = 0;
  2840. if (!tp_features.light)
  2841. return -ENODEV;
  2842. while ((cmd = next_cmd(&buf))) {
  2843. if (strlencmp(cmd, "on") == 0) {
  2844. newstatus = 1;
  2845. } else if (strlencmp(cmd, "off") == 0) {
  2846. newstatus = 0;
  2847. } else
  2848. return -EINVAL;
  2849. }
  2850. return light_set_status(newstatus);
  2851. }
  2852. static struct ibm_struct light_driver_data = {
  2853. .name = "light",
  2854. .read = light_read,
  2855. .write = light_write,
  2856. .exit = light_exit,
  2857. };
  2858. /*************************************************************************
  2859. * Dock subdriver
  2860. */
  2861. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2862. static void dock_notify(struct ibm_struct *ibm, u32 event);
  2863. static int dock_read(char *p);
  2864. static int dock_write(char *buf);
  2865. TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  2866. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  2867. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  2868. "\\_SB.PCI.ISA.SLCE", /* 570 */
  2869. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  2870. /* don't list other alternatives as we install a notify handler on the 570 */
  2871. TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  2872. static const struct acpi_device_id ibm_pci_device_ids[] = {
  2873. {PCI_ROOT_HID_STRING, 0},
  2874. {"", 0},
  2875. };
  2876. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  2877. {
  2878. .notify = dock_notify,
  2879. .handle = &dock_handle,
  2880. .type = ACPI_SYSTEM_NOTIFY,
  2881. },
  2882. {
  2883. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  2884. * We just use it to get notifications of dock hotplug
  2885. * in very old thinkpads */
  2886. .hid = ibm_pci_device_ids,
  2887. .notify = dock_notify,
  2888. .handle = &pci_handle,
  2889. .type = ACPI_SYSTEM_NOTIFY,
  2890. },
  2891. };
  2892. static struct ibm_struct dock_driver_data[2] = {
  2893. {
  2894. .name = "dock",
  2895. .read = dock_read,
  2896. .write = dock_write,
  2897. .acpi = &ibm_dock_acpidriver[0],
  2898. },
  2899. {
  2900. .name = "dock",
  2901. .acpi = &ibm_dock_acpidriver[1],
  2902. },
  2903. };
  2904. #define dock_docked() (_sta(dock_handle) & 1)
  2905. static int __init dock_init(struct ibm_init_struct *iibm)
  2906. {
  2907. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  2908. TPACPI_ACPIHANDLE_INIT(dock);
  2909. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  2910. str_supported(dock_handle != NULL));
  2911. return (dock_handle)? 0 : 1;
  2912. }
  2913. static int __init dock_init2(struct ibm_init_struct *iibm)
  2914. {
  2915. int dock2_needed;
  2916. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  2917. if (dock_driver_data[0].flags.acpi_driver_registered &&
  2918. dock_driver_data[0].flags.acpi_notify_installed) {
  2919. TPACPI_ACPIHANDLE_INIT(pci);
  2920. dock2_needed = (pci_handle != NULL);
  2921. vdbg_printk(TPACPI_DBG_INIT,
  2922. "dock PCI handler for the TP 570 is %s\n",
  2923. str_supported(dock2_needed));
  2924. } else {
  2925. vdbg_printk(TPACPI_DBG_INIT,
  2926. "dock subdriver part 2 not required\n");
  2927. dock2_needed = 0;
  2928. }
  2929. return (dock2_needed)? 0 : 1;
  2930. }
  2931. static void dock_notify(struct ibm_struct *ibm, u32 event)
  2932. {
  2933. int docked = dock_docked();
  2934. int pci = ibm->acpi->hid && ibm->acpi->device &&
  2935. acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
  2936. int data;
  2937. if (event == 1 && !pci) /* 570 */
  2938. data = 1; /* button */
  2939. else if (event == 1 && pci) /* 570 */
  2940. data = 3; /* dock */
  2941. else if (event == 3 && docked)
  2942. data = 1; /* button */
  2943. else if (event == 3 && !docked)
  2944. data = 2; /* undock */
  2945. else if (event == 0 && docked)
  2946. data = 3; /* dock */
  2947. else {
  2948. printk(TPACPI_ERR "unknown dock event %d, status %d\n",
  2949. event, _sta(dock_handle));
  2950. data = 0; /* unknown */
  2951. }
  2952. acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
  2953. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  2954. ibm->acpi->device->dev.bus_id,
  2955. event, data);
  2956. }
  2957. static int dock_read(char *p)
  2958. {
  2959. int len = 0;
  2960. int docked = dock_docked();
  2961. if (!dock_handle)
  2962. len += sprintf(p + len, "status:\t\tnot supported\n");
  2963. else if (!docked)
  2964. len += sprintf(p + len, "status:\t\tundocked\n");
  2965. else {
  2966. len += sprintf(p + len, "status:\t\tdocked\n");
  2967. len += sprintf(p + len, "commands:\tdock, undock\n");
  2968. }
  2969. return len;
  2970. }
  2971. static int dock_write(char *buf)
  2972. {
  2973. char *cmd;
  2974. if (!dock_docked())
  2975. return -ENODEV;
  2976. while ((cmd = next_cmd(&buf))) {
  2977. if (strlencmp(cmd, "undock") == 0) {
  2978. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  2979. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  2980. return -EIO;
  2981. } else if (strlencmp(cmd, "dock") == 0) {
  2982. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  2983. return -EIO;
  2984. } else
  2985. return -EINVAL;
  2986. }
  2987. return 0;
  2988. }
  2989. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  2990. /*************************************************************************
  2991. * Bay subdriver
  2992. */
  2993. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2994. TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  2995. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  2996. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  2997. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  2998. ); /* A21e, R30, R31 */
  2999. TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  3000. "_EJ0", /* all others */
  3001. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  3002. TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  3003. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  3004. ); /* all others */
  3005. TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  3006. "_EJ0", /* 770x */
  3007. ); /* all others */
  3008. static int __init bay_init(struct ibm_init_struct *iibm)
  3009. {
  3010. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  3011. TPACPI_ACPIHANDLE_INIT(bay);
  3012. if (bay_handle)
  3013. TPACPI_ACPIHANDLE_INIT(bay_ej);
  3014. TPACPI_ACPIHANDLE_INIT(bay2);
  3015. if (bay2_handle)
  3016. TPACPI_ACPIHANDLE_INIT(bay2_ej);
  3017. tp_features.bay_status = bay_handle &&
  3018. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  3019. tp_features.bay_status2 = bay2_handle &&
  3020. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  3021. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  3022. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  3023. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  3024. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  3025. vdbg_printk(TPACPI_DBG_INIT,
  3026. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  3027. str_supported(tp_features.bay_status),
  3028. str_supported(tp_features.bay_eject),
  3029. str_supported(tp_features.bay_status2),
  3030. str_supported(tp_features.bay_eject2));
  3031. return (tp_features.bay_status || tp_features.bay_eject ||
  3032. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  3033. }
  3034. static void bay_notify(struct ibm_struct *ibm, u32 event)
  3035. {
  3036. acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
  3037. acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
  3038. ibm->acpi->device->dev.bus_id,
  3039. event, 0);
  3040. }
  3041. #define bay_occupied(b) (_sta(b##_handle) & 1)
  3042. static int bay_read(char *p)
  3043. {
  3044. int len = 0;
  3045. int occupied = bay_occupied(bay);
  3046. int occupied2 = bay_occupied(bay2);
  3047. int eject, eject2;
  3048. len += sprintf(p + len, "status:\t\t%s\n",
  3049. tp_features.bay_status ?
  3050. (occupied ? "occupied" : "unoccupied") :
  3051. "not supported");
  3052. if (tp_features.bay_status2)
  3053. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  3054. "occupied" : "unoccupied");
  3055. eject = tp_features.bay_eject && occupied;
  3056. eject2 = tp_features.bay_eject2 && occupied2;
  3057. if (eject && eject2)
  3058. len += sprintf(p + len, "commands:\teject, eject2\n");
  3059. else if (eject)
  3060. len += sprintf(p + len, "commands:\teject\n");
  3061. else if (eject2)
  3062. len += sprintf(p + len, "commands:\teject2\n");
  3063. return len;
  3064. }
  3065. static int bay_write(char *buf)
  3066. {
  3067. char *cmd;
  3068. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  3069. return -ENODEV;
  3070. while ((cmd = next_cmd(&buf))) {
  3071. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  3072. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  3073. return -EIO;
  3074. } else if (tp_features.bay_eject2 &&
  3075. strlencmp(cmd, "eject2") == 0) {
  3076. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  3077. return -EIO;
  3078. } else
  3079. return -EINVAL;
  3080. }
  3081. return 0;
  3082. }
  3083. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  3084. .notify = bay_notify,
  3085. .handle = &bay_handle,
  3086. .type = ACPI_SYSTEM_NOTIFY,
  3087. };
  3088. static struct ibm_struct bay_driver_data = {
  3089. .name = "bay",
  3090. .read = bay_read,
  3091. .write = bay_write,
  3092. .acpi = &ibm_bay_acpidriver,
  3093. };
  3094. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3095. /*************************************************************************
  3096. * CMOS subdriver
  3097. */
  3098. /* sysfs cmos_command -------------------------------------------------- */
  3099. static ssize_t cmos_command_store(struct device *dev,
  3100. struct device_attribute *attr,
  3101. const char *buf, size_t count)
  3102. {
  3103. unsigned long cmos_cmd;
  3104. int res;
  3105. if (parse_strtoul(buf, 21, &cmos_cmd))
  3106. return -EINVAL;
  3107. res = issue_thinkpad_cmos_command(cmos_cmd);
  3108. return (res)? res : count;
  3109. }
  3110. static struct device_attribute dev_attr_cmos_command =
  3111. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  3112. /* --------------------------------------------------------------------- */
  3113. static int __init cmos_init(struct ibm_init_struct *iibm)
  3114. {
  3115. int res;
  3116. vdbg_printk(TPACPI_DBG_INIT,
  3117. "initializing cmos commands subdriver\n");
  3118. TPACPI_ACPIHANDLE_INIT(cmos);
  3119. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  3120. str_supported(cmos_handle != NULL));
  3121. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  3122. if (res)
  3123. return res;
  3124. return (cmos_handle)? 0 : 1;
  3125. }
  3126. static void cmos_exit(void)
  3127. {
  3128. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  3129. }
  3130. static int cmos_read(char *p)
  3131. {
  3132. int len = 0;
  3133. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  3134. R30, R31, T20-22, X20-21 */
  3135. if (!cmos_handle)
  3136. len += sprintf(p + len, "status:\t\tnot supported\n");
  3137. else {
  3138. len += sprintf(p + len, "status:\t\tsupported\n");
  3139. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  3140. }
  3141. return len;
  3142. }
  3143. static int cmos_write(char *buf)
  3144. {
  3145. char *cmd;
  3146. int cmos_cmd, res;
  3147. while ((cmd = next_cmd(&buf))) {
  3148. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  3149. cmos_cmd >= 0 && cmos_cmd <= 21) {
  3150. /* cmos_cmd set */
  3151. } else
  3152. return -EINVAL;
  3153. res = issue_thinkpad_cmos_command(cmos_cmd);
  3154. if (res)
  3155. return res;
  3156. }
  3157. return 0;
  3158. }
  3159. static struct ibm_struct cmos_driver_data = {
  3160. .name = "cmos",
  3161. .read = cmos_read,
  3162. .write = cmos_write,
  3163. .exit = cmos_exit,
  3164. };
  3165. /*************************************************************************
  3166. * LED subdriver
  3167. */
  3168. enum led_access_mode {
  3169. TPACPI_LED_NONE = 0,
  3170. TPACPI_LED_570, /* 570 */
  3171. TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  3172. TPACPI_LED_NEW, /* all others */
  3173. };
  3174. enum { /* For TPACPI_LED_OLD */
  3175. TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
  3176. TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
  3177. TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
  3178. };
  3179. enum led_status_t {
  3180. TPACPI_LED_OFF = 0,
  3181. TPACPI_LED_ON,
  3182. TPACPI_LED_BLINK,
  3183. };
  3184. static enum led_access_mode led_supported;
  3185. TPACPI_HANDLE(led, ec, "SLED", /* 570 */
  3186. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
  3187. /* T20-22, X20-21 */
  3188. "LED", /* all others */
  3189. ); /* R30, R31 */
  3190. #define TPACPI_LED_NUMLEDS 8
  3191. static struct tpacpi_led_classdev *tpacpi_leds;
  3192. static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
  3193. static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
  3194. /* there's a limit of 19 chars + NULL before 2.6.26 */
  3195. "tpacpi::power",
  3196. "tpacpi:orange:batt",
  3197. "tpacpi:green:batt",
  3198. "tpacpi::dock_active",
  3199. "tpacpi::bay_active",
  3200. "tpacpi::dock_batt",
  3201. "tpacpi::unknown_led",
  3202. "tpacpi::standby",
  3203. };
  3204. static int led_get_status(const unsigned int led)
  3205. {
  3206. int status;
  3207. enum led_status_t led_s;
  3208. switch (led_supported) {
  3209. case TPACPI_LED_570:
  3210. if (!acpi_evalf(ec_handle,
  3211. &status, "GLED", "dd", 1 << led))
  3212. return -EIO;
  3213. led_s = (status == 0)?
  3214. TPACPI_LED_OFF :
  3215. ((status == 1)?
  3216. TPACPI_LED_ON :
  3217. TPACPI_LED_BLINK);
  3218. tpacpi_led_state_cache[led] = led_s;
  3219. return led_s;
  3220. default:
  3221. return -ENXIO;
  3222. }
  3223. /* not reached */
  3224. }
  3225. static int led_set_status(const unsigned int led,
  3226. const enum led_status_t ledstatus)
  3227. {
  3228. /* off, on, blink. Index is led_status_t */
  3229. static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
  3230. static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
  3231. int rc = 0;
  3232. switch (led_supported) {
  3233. case TPACPI_LED_570:
  3234. /* 570 */
  3235. if (led > 7)
  3236. return -EINVAL;
  3237. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3238. (1 << led), led_sled_arg1[ledstatus]))
  3239. rc = -EIO;
  3240. break;
  3241. case TPACPI_LED_OLD:
  3242. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  3243. if (led > 7)
  3244. return -EINVAL;
  3245. rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
  3246. if (rc >= 0)
  3247. rc = ec_write(TPACPI_LED_EC_HLBL,
  3248. (ledstatus == TPACPI_LED_BLINK) << led);
  3249. if (rc >= 0)
  3250. rc = ec_write(TPACPI_LED_EC_HLCL,
  3251. (ledstatus != TPACPI_LED_OFF) << led);
  3252. break;
  3253. case TPACPI_LED_NEW:
  3254. /* all others */
  3255. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  3256. led, led_led_arg1[ledstatus]))
  3257. rc = -EIO;
  3258. break;
  3259. default:
  3260. rc = -ENXIO;
  3261. }
  3262. if (!rc)
  3263. tpacpi_led_state_cache[led] = ledstatus;
  3264. return rc;
  3265. }
  3266. static void led_sysfs_set_status(unsigned int led,
  3267. enum led_brightness brightness)
  3268. {
  3269. led_set_status(led,
  3270. (brightness == LED_OFF) ?
  3271. TPACPI_LED_OFF :
  3272. (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
  3273. TPACPI_LED_BLINK : TPACPI_LED_ON);
  3274. }
  3275. static void led_set_status_worker(struct work_struct *work)
  3276. {
  3277. struct tpacpi_led_classdev *data =
  3278. container_of(work, struct tpacpi_led_classdev, work);
  3279. if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
  3280. led_sysfs_set_status(data->led, data->new_brightness);
  3281. }
  3282. static void led_sysfs_set(struct led_classdev *led_cdev,
  3283. enum led_brightness brightness)
  3284. {
  3285. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3286. struct tpacpi_led_classdev, led_classdev);
  3287. data->new_brightness = brightness;
  3288. queue_work(tpacpi_wq, &data->work);
  3289. }
  3290. static int led_sysfs_blink_set(struct led_classdev *led_cdev,
  3291. unsigned long *delay_on, unsigned long *delay_off)
  3292. {
  3293. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3294. struct tpacpi_led_classdev, led_classdev);
  3295. /* Can we choose the flash rate? */
  3296. if (*delay_on == 0 && *delay_off == 0) {
  3297. /* yes. set them to the hardware blink rate (1 Hz) */
  3298. *delay_on = 500; /* ms */
  3299. *delay_off = 500; /* ms */
  3300. } else if ((*delay_on != 500) || (*delay_off != 500))
  3301. return -EINVAL;
  3302. data->new_brightness = TPACPI_LED_BLINK;
  3303. queue_work(tpacpi_wq, &data->work);
  3304. return 0;
  3305. }
  3306. static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
  3307. {
  3308. int rc;
  3309. struct tpacpi_led_classdev *data = container_of(led_cdev,
  3310. struct tpacpi_led_classdev, led_classdev);
  3311. rc = led_get_status(data->led);
  3312. if (rc == TPACPI_LED_OFF || rc < 0)
  3313. rc = LED_OFF; /* no error handling in led class :( */
  3314. else
  3315. rc = LED_FULL;
  3316. return rc;
  3317. }
  3318. static void led_exit(void)
  3319. {
  3320. unsigned int i;
  3321. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  3322. if (tpacpi_leds[i].led_classdev.name)
  3323. led_classdev_unregister(&tpacpi_leds[i].led_classdev);
  3324. }
  3325. kfree(tpacpi_leds);
  3326. }
  3327. static int __init led_init(struct ibm_init_struct *iibm)
  3328. {
  3329. unsigned int i;
  3330. int rc;
  3331. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  3332. TPACPI_ACPIHANDLE_INIT(led);
  3333. if (!led_handle)
  3334. /* led not supported on R30, R31 */
  3335. led_supported = TPACPI_LED_NONE;
  3336. else if (strlencmp(led_path, "SLED") == 0)
  3337. /* 570 */
  3338. led_supported = TPACPI_LED_570;
  3339. else if (strlencmp(led_path, "SYSL") == 0)
  3340. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  3341. led_supported = TPACPI_LED_OLD;
  3342. else
  3343. /* all others */
  3344. led_supported = TPACPI_LED_NEW;
  3345. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  3346. str_supported(led_supported), led_supported);
  3347. tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
  3348. GFP_KERNEL);
  3349. if (!tpacpi_leds) {
  3350. printk(TPACPI_ERR "Out of memory for LED data\n");
  3351. return -ENOMEM;
  3352. }
  3353. for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
  3354. tpacpi_leds[i].led = i;
  3355. tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
  3356. tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
  3357. if (led_supported == TPACPI_LED_570)
  3358. tpacpi_leds[i].led_classdev.brightness_get =
  3359. &led_sysfs_get;
  3360. tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
  3361. INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
  3362. rc = led_classdev_register(&tpacpi_pdev->dev,
  3363. &tpacpi_leds[i].led_classdev);
  3364. if (rc < 0) {
  3365. tpacpi_leds[i].led_classdev.name = NULL;
  3366. led_exit();
  3367. return rc;
  3368. }
  3369. }
  3370. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  3371. }
  3372. #define str_led_status(s) \
  3373. ((s) == TPACPI_LED_OFF ? "off" : \
  3374. ((s) == TPACPI_LED_ON ? "on" : "blinking"))
  3375. static int led_read(char *p)
  3376. {
  3377. int len = 0;
  3378. if (!led_supported) {
  3379. len += sprintf(p + len, "status:\t\tnot supported\n");
  3380. return len;
  3381. }
  3382. len += sprintf(p + len, "status:\t\tsupported\n");
  3383. if (led_supported == TPACPI_LED_570) {
  3384. /* 570 */
  3385. int i, status;
  3386. for (i = 0; i < 8; i++) {
  3387. status = led_get_status(i);
  3388. if (status < 0)
  3389. return -EIO;
  3390. len += sprintf(p + len, "%d:\t\t%s\n",
  3391. i, str_led_status(status));
  3392. }
  3393. }
  3394. len += sprintf(p + len, "commands:\t"
  3395. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  3396. return len;
  3397. }
  3398. static int led_write(char *buf)
  3399. {
  3400. char *cmd;
  3401. int led, rc;
  3402. enum led_status_t s;
  3403. if (!led_supported)
  3404. return -ENODEV;
  3405. while ((cmd = next_cmd(&buf))) {
  3406. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  3407. return -EINVAL;
  3408. if (strstr(cmd, "off")) {
  3409. s = TPACPI_LED_OFF;
  3410. } else if (strstr(cmd, "on")) {
  3411. s = TPACPI_LED_ON;
  3412. } else if (strstr(cmd, "blink")) {
  3413. s = TPACPI_LED_BLINK;
  3414. } else {
  3415. return -EINVAL;
  3416. }
  3417. rc = led_set_status(led, s);
  3418. if (rc < 0)
  3419. return rc;
  3420. }
  3421. return 0;
  3422. }
  3423. static struct ibm_struct led_driver_data = {
  3424. .name = "led",
  3425. .read = led_read,
  3426. .write = led_write,
  3427. .exit = led_exit,
  3428. };
  3429. /*************************************************************************
  3430. * Beep subdriver
  3431. */
  3432. TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  3433. static int __init beep_init(struct ibm_init_struct *iibm)
  3434. {
  3435. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  3436. TPACPI_ACPIHANDLE_INIT(beep);
  3437. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  3438. str_supported(beep_handle != NULL));
  3439. return (beep_handle)? 0 : 1;
  3440. }
  3441. static int beep_read(char *p)
  3442. {
  3443. int len = 0;
  3444. if (!beep_handle)
  3445. len += sprintf(p + len, "status:\t\tnot supported\n");
  3446. else {
  3447. len += sprintf(p + len, "status:\t\tsupported\n");
  3448. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  3449. }
  3450. return len;
  3451. }
  3452. static int beep_write(char *buf)
  3453. {
  3454. char *cmd;
  3455. int beep_cmd;
  3456. if (!beep_handle)
  3457. return -ENODEV;
  3458. while ((cmd = next_cmd(&buf))) {
  3459. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  3460. beep_cmd >= 0 && beep_cmd <= 17) {
  3461. /* beep_cmd set */
  3462. } else
  3463. return -EINVAL;
  3464. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  3465. return -EIO;
  3466. }
  3467. return 0;
  3468. }
  3469. static struct ibm_struct beep_driver_data = {
  3470. .name = "beep",
  3471. .read = beep_read,
  3472. .write = beep_write,
  3473. };
  3474. /*************************************************************************
  3475. * Thermal subdriver
  3476. */
  3477. enum thermal_access_mode {
  3478. TPACPI_THERMAL_NONE = 0, /* No thermal support */
  3479. TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
  3480. TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
  3481. TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
  3482. TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
  3483. };
  3484. enum { /* TPACPI_THERMAL_TPEC_* */
  3485. TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
  3486. TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
  3487. TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
  3488. };
  3489. #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
  3490. struct ibm_thermal_sensors_struct {
  3491. s32 temp[TPACPI_MAX_THERMAL_SENSORS];
  3492. };
  3493. static enum thermal_access_mode thermal_read_mode;
  3494. /* idx is zero-based */
  3495. static int thermal_get_sensor(int idx, s32 *value)
  3496. {
  3497. int t;
  3498. s8 tmp;
  3499. char tmpi[5];
  3500. t = TP_EC_THERMAL_TMP0;
  3501. switch (thermal_read_mode) {
  3502. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  3503. case TPACPI_THERMAL_TPEC_16:
  3504. if (idx >= 8 && idx <= 15) {
  3505. t = TP_EC_THERMAL_TMP8;
  3506. idx -= 8;
  3507. }
  3508. /* fallthrough */
  3509. #endif
  3510. case TPACPI_THERMAL_TPEC_8:
  3511. if (idx <= 7) {
  3512. if (!acpi_ec_read(t + idx, &tmp))
  3513. return -EIO;
  3514. *value = tmp * 1000;
  3515. return 0;
  3516. }
  3517. break;
  3518. case TPACPI_THERMAL_ACPI_UPDT:
  3519. if (idx <= 7) {
  3520. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3521. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  3522. return -EIO;
  3523. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3524. return -EIO;
  3525. *value = (t - 2732) * 100;
  3526. return 0;
  3527. }
  3528. break;
  3529. case TPACPI_THERMAL_ACPI_TMP07:
  3530. if (idx <= 7) {
  3531. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  3532. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  3533. return -EIO;
  3534. if (t > 127 || t < -127)
  3535. t = TP_EC_THERMAL_TMP_NA;
  3536. *value = t * 1000;
  3537. return 0;
  3538. }
  3539. break;
  3540. case TPACPI_THERMAL_NONE:
  3541. default:
  3542. return -ENOSYS;
  3543. }
  3544. return -EINVAL;
  3545. }
  3546. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  3547. {
  3548. int res, i;
  3549. int n;
  3550. n = 8;
  3551. i = 0;
  3552. if (!s)
  3553. return -EINVAL;
  3554. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  3555. n = 16;
  3556. for (i = 0 ; i < n; i++) {
  3557. res = thermal_get_sensor(i, &s->temp[i]);
  3558. if (res)
  3559. return res;
  3560. }
  3561. return n;
  3562. }
  3563. /* sysfs temp##_input -------------------------------------------------- */
  3564. static ssize_t thermal_temp_input_show(struct device *dev,
  3565. struct device_attribute *attr,
  3566. char *buf)
  3567. {
  3568. struct sensor_device_attribute *sensor_attr =
  3569. to_sensor_dev_attr(attr);
  3570. int idx = sensor_attr->index;
  3571. s32 value;
  3572. int res;
  3573. res = thermal_get_sensor(idx, &value);
  3574. if (res)
  3575. return res;
  3576. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  3577. return -ENXIO;
  3578. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  3579. }
  3580. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  3581. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
  3582. thermal_temp_input_show, NULL, _idxB)
  3583. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  3584. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  3585. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  3586. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  3587. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  3588. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  3589. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  3590. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  3591. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  3592. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  3593. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  3594. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  3595. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  3596. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  3597. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  3598. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  3599. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  3600. };
  3601. #define THERMAL_ATTRS(X) \
  3602. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  3603. static struct attribute *thermal_temp_input_attr[] = {
  3604. THERMAL_ATTRS(8),
  3605. THERMAL_ATTRS(9),
  3606. THERMAL_ATTRS(10),
  3607. THERMAL_ATTRS(11),
  3608. THERMAL_ATTRS(12),
  3609. THERMAL_ATTRS(13),
  3610. THERMAL_ATTRS(14),
  3611. THERMAL_ATTRS(15),
  3612. THERMAL_ATTRS(0),
  3613. THERMAL_ATTRS(1),
  3614. THERMAL_ATTRS(2),
  3615. THERMAL_ATTRS(3),
  3616. THERMAL_ATTRS(4),
  3617. THERMAL_ATTRS(5),
  3618. THERMAL_ATTRS(6),
  3619. THERMAL_ATTRS(7),
  3620. NULL
  3621. };
  3622. static const struct attribute_group thermal_temp_input16_group = {
  3623. .attrs = thermal_temp_input_attr
  3624. };
  3625. static const struct attribute_group thermal_temp_input8_group = {
  3626. .attrs = &thermal_temp_input_attr[8]
  3627. };
  3628. #undef THERMAL_SENSOR_ATTR_TEMP
  3629. #undef THERMAL_ATTRS
  3630. /* --------------------------------------------------------------------- */
  3631. static int __init thermal_init(struct ibm_init_struct *iibm)
  3632. {
  3633. u8 t, ta1, ta2;
  3634. int i;
  3635. int acpi_tmp7;
  3636. int res;
  3637. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  3638. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  3639. if (thinkpad_id.ec_model) {
  3640. /*
  3641. * Direct EC access mode: sensors at registers
  3642. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  3643. * non-implemented, thermal sensors return 0x80 when
  3644. * not available
  3645. */
  3646. ta1 = ta2 = 0;
  3647. for (i = 0; i < 8; i++) {
  3648. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  3649. ta1 |= t;
  3650. } else {
  3651. ta1 = 0;
  3652. break;
  3653. }
  3654. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  3655. ta2 |= t;
  3656. } else {
  3657. ta1 = 0;
  3658. break;
  3659. }
  3660. }
  3661. if (ta1 == 0) {
  3662. /* This is sheer paranoia, but we handle it anyway */
  3663. if (acpi_tmp7) {
  3664. printk(TPACPI_ERR
  3665. "ThinkPad ACPI EC access misbehaving, "
  3666. "falling back to ACPI TMPx access "
  3667. "mode\n");
  3668. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3669. } else {
  3670. printk(TPACPI_ERR
  3671. "ThinkPad ACPI EC access misbehaving, "
  3672. "disabling thermal sensors access\n");
  3673. thermal_read_mode = TPACPI_THERMAL_NONE;
  3674. }
  3675. } else {
  3676. thermal_read_mode =
  3677. (ta2 != 0) ?
  3678. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  3679. }
  3680. } else if (acpi_tmp7) {
  3681. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  3682. /* 600e/x, 770e, 770x */
  3683. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  3684. } else {
  3685. /* Standard ACPI TMPx access, max 8 sensors */
  3686. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  3687. }
  3688. } else {
  3689. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  3690. thermal_read_mode = TPACPI_THERMAL_NONE;
  3691. }
  3692. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  3693. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  3694. thermal_read_mode);
  3695. switch (thermal_read_mode) {
  3696. case TPACPI_THERMAL_TPEC_16:
  3697. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3698. &thermal_temp_input16_group);
  3699. if (res)
  3700. return res;
  3701. break;
  3702. case TPACPI_THERMAL_TPEC_8:
  3703. case TPACPI_THERMAL_ACPI_TMP07:
  3704. case TPACPI_THERMAL_ACPI_UPDT:
  3705. res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  3706. &thermal_temp_input8_group);
  3707. if (res)
  3708. return res;
  3709. break;
  3710. case TPACPI_THERMAL_NONE:
  3711. default:
  3712. return 1;
  3713. }
  3714. return 0;
  3715. }
  3716. static void thermal_exit(void)
  3717. {
  3718. switch (thermal_read_mode) {
  3719. case TPACPI_THERMAL_TPEC_16:
  3720. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3721. &thermal_temp_input16_group);
  3722. break;
  3723. case TPACPI_THERMAL_TPEC_8:
  3724. case TPACPI_THERMAL_ACPI_TMP07:
  3725. case TPACPI_THERMAL_ACPI_UPDT:
  3726. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  3727. &thermal_temp_input16_group);
  3728. break;
  3729. case TPACPI_THERMAL_NONE:
  3730. default:
  3731. break;
  3732. }
  3733. }
  3734. static int thermal_read(char *p)
  3735. {
  3736. int len = 0;
  3737. int n, i;
  3738. struct ibm_thermal_sensors_struct t;
  3739. n = thermal_get_sensors(&t);
  3740. if (unlikely(n < 0))
  3741. return n;
  3742. len += sprintf(p + len, "temperatures:\t");
  3743. if (n > 0) {
  3744. for (i = 0; i < (n - 1); i++)
  3745. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  3746. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  3747. } else
  3748. len += sprintf(p + len, "not supported\n");
  3749. return len;
  3750. }
  3751. static struct ibm_struct thermal_driver_data = {
  3752. .name = "thermal",
  3753. .read = thermal_read,
  3754. .exit = thermal_exit,
  3755. };
  3756. /*************************************************************************
  3757. * EC Dump subdriver
  3758. */
  3759. static u8 ecdump_regs[256];
  3760. static int ecdump_read(char *p)
  3761. {
  3762. int len = 0;
  3763. int i, j;
  3764. u8 v;
  3765. len += sprintf(p + len, "EC "
  3766. " +00 +01 +02 +03 +04 +05 +06 +07"
  3767. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  3768. for (i = 0; i < 256; i += 16) {
  3769. len += sprintf(p + len, "EC 0x%02x:", i);
  3770. for (j = 0; j < 16; j++) {
  3771. if (!acpi_ec_read(i + j, &v))
  3772. break;
  3773. if (v != ecdump_regs[i + j])
  3774. len += sprintf(p + len, " *%02x", v);
  3775. else
  3776. len += sprintf(p + len, " %02x", v);
  3777. ecdump_regs[i + j] = v;
  3778. }
  3779. len += sprintf(p + len, "\n");
  3780. if (j != 16)
  3781. break;
  3782. }
  3783. /* These are way too dangerous to advertise openly... */
  3784. #if 0
  3785. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  3786. " (<offset> is 00-ff, <value> is 00-ff)\n");
  3787. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  3788. " (<offset> is 00-ff, <value> is 0-255)\n");
  3789. #endif
  3790. return len;
  3791. }
  3792. static int ecdump_write(char *buf)
  3793. {
  3794. char *cmd;
  3795. int i, v;
  3796. while ((cmd = next_cmd(&buf))) {
  3797. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  3798. /* i and v set */
  3799. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  3800. /* i and v set */
  3801. } else
  3802. return -EINVAL;
  3803. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  3804. if (!acpi_ec_write(i, v))
  3805. return -EIO;
  3806. } else
  3807. return -EINVAL;
  3808. }
  3809. return 0;
  3810. }
  3811. static struct ibm_struct ecdump_driver_data = {
  3812. .name = "ecdump",
  3813. .read = ecdump_read,
  3814. .write = ecdump_write,
  3815. .flags.experimental = 1,
  3816. };
  3817. /*************************************************************************
  3818. * Backlight/brightness subdriver
  3819. */
  3820. #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
  3821. enum {
  3822. TP_EC_BACKLIGHT = 0x31,
  3823. /* TP_EC_BACKLIGHT bitmasks */
  3824. TP_EC_BACKLIGHT_LVLMSK = 0x1F,
  3825. TP_EC_BACKLIGHT_CMDMSK = 0xE0,
  3826. TP_EC_BACKLIGHT_MAPSW = 0x20,
  3827. };
  3828. static struct backlight_device *ibm_backlight_device;
  3829. static int brightness_mode;
  3830. static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
  3831. static struct mutex brightness_mutex;
  3832. /*
  3833. * ThinkPads can read brightness from two places: EC 0x31, or
  3834. * CMOS NVRAM byte 0x5E, bits 0-3.
  3835. *
  3836. * EC 0x31 has the following layout
  3837. * Bit 7: unknown function
  3838. * Bit 6: unknown function
  3839. * Bit 5: Z: honour scale changes, NZ: ignore scale changes
  3840. * Bit 4: must be set to zero to avoid problems
  3841. * Bit 3-0: backlight brightness level
  3842. *
  3843. * brightness_get_raw returns status data in the EC 0x31 layout
  3844. */
  3845. static int brightness_get_raw(int *status)
  3846. {
  3847. u8 lec = 0, lcmos = 0, level = 0;
  3848. if (brightness_mode & 1) {
  3849. if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
  3850. return -EIO;
  3851. level = lec & TP_EC_BACKLIGHT_LVLMSK;
  3852. };
  3853. if (brightness_mode & 2) {
  3854. lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
  3855. & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
  3856. >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
  3857. lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
  3858. level = lcmos;
  3859. }
  3860. if (brightness_mode == 3) {
  3861. *status = lec; /* Prefer EC, CMOS is just a backing store */
  3862. lec &= TP_EC_BACKLIGHT_LVLMSK;
  3863. if (lec == lcmos)
  3864. tp_warned.bright_cmos_ec_unsync = 0;
  3865. else {
  3866. if (!tp_warned.bright_cmos_ec_unsync) {
  3867. printk(TPACPI_ERR
  3868. "CMOS NVRAM (%u) and EC (%u) do not "
  3869. "agree on display brightness level\n",
  3870. (unsigned int) lcmos,
  3871. (unsigned int) lec);
  3872. tp_warned.bright_cmos_ec_unsync = 1;
  3873. }
  3874. return -EIO;
  3875. }
  3876. } else {
  3877. *status = level;
  3878. }
  3879. return 0;
  3880. }
  3881. /* May return EINTR which can always be mapped to ERESTARTSYS */
  3882. static int brightness_set(int value)
  3883. {
  3884. int cmos_cmd, inc, i, res;
  3885. int current_value;
  3886. int command_bits;
  3887. if (value > ((tp_features.bright_16levels)? 15 : 7) ||
  3888. value < 0)
  3889. return -EINVAL;
  3890. res = mutex_lock_interruptible(&brightness_mutex);
  3891. if (res < 0)
  3892. return res;
  3893. res = brightness_get_raw(&current_value);
  3894. if (res < 0)
  3895. goto errout;
  3896. command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
  3897. current_value &= TP_EC_BACKLIGHT_LVLMSK;
  3898. cmos_cmd = value > current_value ?
  3899. TP_CMOS_BRIGHTNESS_UP :
  3900. TP_CMOS_BRIGHTNESS_DOWN;
  3901. inc = (value > current_value)? 1 : -1;
  3902. res = 0;
  3903. for (i = current_value; i != value; i += inc) {
  3904. if ((brightness_mode & 2) &&
  3905. issue_thinkpad_cmos_command(cmos_cmd)) {
  3906. res = -EIO;
  3907. goto errout;
  3908. }
  3909. if ((brightness_mode & 1) &&
  3910. !acpi_ec_write(TP_EC_BACKLIGHT,
  3911. (i + inc) | command_bits)) {
  3912. res = -EIO;
  3913. goto errout;;
  3914. }
  3915. }
  3916. errout:
  3917. mutex_unlock(&brightness_mutex);
  3918. return res;
  3919. }
  3920. /* sysfs backlight class ----------------------------------------------- */
  3921. static int brightness_update_status(struct backlight_device *bd)
  3922. {
  3923. /* it is the backlight class's job (caller) to handle
  3924. * EINTR and other errors properly */
  3925. return brightness_set(
  3926. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  3927. bd->props.power == FB_BLANK_UNBLANK) ?
  3928. bd->props.brightness : 0);
  3929. }
  3930. static int brightness_get(struct backlight_device *bd)
  3931. {
  3932. int status, res;
  3933. res = brightness_get_raw(&status);
  3934. if (res < 0)
  3935. return 0; /* FIXME: teach backlight about error handling */
  3936. return status & TP_EC_BACKLIGHT_LVLMSK;
  3937. }
  3938. static struct backlight_ops ibm_backlight_data = {
  3939. .get_brightness = brightness_get,
  3940. .update_status = brightness_update_status,
  3941. };
  3942. /* --------------------------------------------------------------------- */
  3943. static int __init brightness_init(struct ibm_init_struct *iibm)
  3944. {
  3945. int b;
  3946. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  3947. mutex_init(&brightness_mutex);
  3948. /*
  3949. * We always attempt to detect acpi support, so as to switch
  3950. * Lenovo Vista BIOS to ACPI brightness mode even if we are not
  3951. * going to publish a backlight interface
  3952. */
  3953. b = tpacpi_check_std_acpi_brightness_support();
  3954. if (b > 0) {
  3955. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
  3956. printk(TPACPI_NOTICE
  3957. "Lenovo BIOS switched to ACPI backlight "
  3958. "control mode\n");
  3959. }
  3960. if (brightness_enable > 1) {
  3961. printk(TPACPI_NOTICE
  3962. "standard ACPI backlight interface "
  3963. "available, not loading native one...\n");
  3964. return 1;
  3965. }
  3966. }
  3967. if (!brightness_enable) {
  3968. dbg_printk(TPACPI_DBG_INIT,
  3969. "brightness support disabled by "
  3970. "module parameter\n");
  3971. return 1;
  3972. }
  3973. if (b > 16) {
  3974. printk(TPACPI_ERR
  3975. "Unsupported brightness interface, "
  3976. "please contact %s\n", TPACPI_MAIL);
  3977. return 1;
  3978. }
  3979. if (b == 16)
  3980. tp_features.bright_16levels = 1;
  3981. if (!brightness_mode) {
  3982. if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
  3983. brightness_mode = 2;
  3984. else
  3985. brightness_mode = 3;
  3986. dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
  3987. brightness_mode);
  3988. }
  3989. if (brightness_mode > 3)
  3990. return -EINVAL;
  3991. if (brightness_get_raw(&b) < 0)
  3992. return 1;
  3993. if (tp_features.bright_16levels)
  3994. printk(TPACPI_INFO
  3995. "detected a 16-level brightness capable ThinkPad\n");
  3996. ibm_backlight_device = backlight_device_register(
  3997. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  3998. &ibm_backlight_data);
  3999. if (IS_ERR(ibm_backlight_device)) {
  4000. printk(TPACPI_ERR "Could not register backlight device\n");
  4001. return PTR_ERR(ibm_backlight_device);
  4002. }
  4003. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  4004. ibm_backlight_device->props.max_brightness =
  4005. (tp_features.bright_16levels)? 15 : 7;
  4006. ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
  4007. backlight_update_status(ibm_backlight_device);
  4008. return 0;
  4009. }
  4010. static void brightness_exit(void)
  4011. {
  4012. if (ibm_backlight_device) {
  4013. vdbg_printk(TPACPI_DBG_EXIT,
  4014. "calling backlight_device_unregister()\n");
  4015. backlight_device_unregister(ibm_backlight_device);
  4016. }
  4017. }
  4018. static int brightness_read(char *p)
  4019. {
  4020. int len = 0;
  4021. int level;
  4022. level = brightness_get(NULL);
  4023. if (level < 0) {
  4024. len += sprintf(p + len, "level:\t\tunreadable\n");
  4025. } else {
  4026. len += sprintf(p + len, "level:\t\t%d\n", level);
  4027. len += sprintf(p + len, "commands:\tup, down\n");
  4028. len += sprintf(p + len, "commands:\tlevel <level>"
  4029. " (<level> is 0-%d)\n",
  4030. (tp_features.bright_16levels) ? 15 : 7);
  4031. }
  4032. return len;
  4033. }
  4034. static int brightness_write(char *buf)
  4035. {
  4036. int level;
  4037. int rc;
  4038. char *cmd;
  4039. int max_level = (tp_features.bright_16levels) ? 15 : 7;
  4040. level = brightness_get(NULL);
  4041. if (level < 0)
  4042. return level;
  4043. while ((cmd = next_cmd(&buf))) {
  4044. if (strlencmp(cmd, "up") == 0) {
  4045. if (level < max_level)
  4046. level++;
  4047. } else if (strlencmp(cmd, "down") == 0) {
  4048. if (level > 0)
  4049. level--;
  4050. } else if (sscanf(cmd, "level %d", &level) == 1 &&
  4051. level >= 0 && level <= max_level) {
  4052. /* new level set */
  4053. } else
  4054. return -EINVAL;
  4055. }
  4056. /*
  4057. * Now we know what the final level should be, so we try to set it.
  4058. * Doing it this way makes the syscall restartable in case of EINTR
  4059. */
  4060. rc = brightness_set(level);
  4061. return (rc == -EINTR)? ERESTARTSYS : rc;
  4062. }
  4063. static struct ibm_struct brightness_driver_data = {
  4064. .name = "brightness",
  4065. .read = brightness_read,
  4066. .write = brightness_write,
  4067. .exit = brightness_exit,
  4068. };
  4069. /*************************************************************************
  4070. * Volume subdriver
  4071. */
  4072. static int volume_offset = 0x30;
  4073. static int volume_read(char *p)
  4074. {
  4075. int len = 0;
  4076. u8 level;
  4077. if (!acpi_ec_read(volume_offset, &level)) {
  4078. len += sprintf(p + len, "level:\t\tunreadable\n");
  4079. } else {
  4080. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  4081. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  4082. len += sprintf(p + len, "commands:\tup, down, mute\n");
  4083. len += sprintf(p + len, "commands:\tlevel <level>"
  4084. " (<level> is 0-15)\n");
  4085. }
  4086. return len;
  4087. }
  4088. static int volume_write(char *buf)
  4089. {
  4090. int cmos_cmd, inc, i;
  4091. u8 level, mute;
  4092. int new_level, new_mute;
  4093. char *cmd;
  4094. while ((cmd = next_cmd(&buf))) {
  4095. if (!acpi_ec_read(volume_offset, &level))
  4096. return -EIO;
  4097. new_mute = mute = level & 0x40;
  4098. new_level = level = level & 0xf;
  4099. if (strlencmp(cmd, "up") == 0) {
  4100. if (mute)
  4101. new_mute = 0;
  4102. else
  4103. new_level = level == 15 ? 15 : level + 1;
  4104. } else if (strlencmp(cmd, "down") == 0) {
  4105. if (mute)
  4106. new_mute = 0;
  4107. else
  4108. new_level = level == 0 ? 0 : level - 1;
  4109. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  4110. new_level >= 0 && new_level <= 15) {
  4111. /* new_level set */
  4112. } else if (strlencmp(cmd, "mute") == 0) {
  4113. new_mute = 0x40;
  4114. } else
  4115. return -EINVAL;
  4116. if (new_level != level) {
  4117. /* mute doesn't change */
  4118. cmos_cmd = (new_level > level) ?
  4119. TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  4120. inc = new_level > level ? 1 : -1;
  4121. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  4122. !acpi_ec_write(volume_offset, level)))
  4123. return -EIO;
  4124. for (i = level; i != new_level; i += inc)
  4125. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  4126. !acpi_ec_write(volume_offset, i + inc))
  4127. return -EIO;
  4128. if (mute &&
  4129. (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  4130. !acpi_ec_write(volume_offset, new_level + mute))) {
  4131. return -EIO;
  4132. }
  4133. }
  4134. if (new_mute != mute) {
  4135. /* level doesn't change */
  4136. cmos_cmd = (new_mute) ?
  4137. TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  4138. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  4139. !acpi_ec_write(volume_offset, level + new_mute))
  4140. return -EIO;
  4141. }
  4142. }
  4143. return 0;
  4144. }
  4145. static struct ibm_struct volume_driver_data = {
  4146. .name = "volume",
  4147. .read = volume_read,
  4148. .write = volume_write,
  4149. };
  4150. /*************************************************************************
  4151. * Fan subdriver
  4152. */
  4153. /*
  4154. * FAN ACCESS MODES
  4155. *
  4156. * TPACPI_FAN_RD_ACPI_GFAN:
  4157. * ACPI GFAN method: returns fan level
  4158. *
  4159. * see TPACPI_FAN_WR_ACPI_SFAN
  4160. * EC 0x2f (HFSP) not available if GFAN exists
  4161. *
  4162. * TPACPI_FAN_WR_ACPI_SFAN:
  4163. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  4164. *
  4165. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  4166. * it for writing.
  4167. *
  4168. * TPACPI_FAN_WR_TPEC:
  4169. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  4170. * Supported on almost all ThinkPads
  4171. *
  4172. * Fan speed changes of any sort (including those caused by the
  4173. * disengaged mode) are usually done slowly by the firmware as the
  4174. * maximum ammount of fan duty cycle change per second seems to be
  4175. * limited.
  4176. *
  4177. * Reading is not available if GFAN exists.
  4178. * Writing is not available if SFAN exists.
  4179. *
  4180. * Bits
  4181. * 7 automatic mode engaged;
  4182. * (default operation mode of the ThinkPad)
  4183. * fan level is ignored in this mode.
  4184. * 6 full speed mode (takes precedence over bit 7);
  4185. * not available on all thinkpads. May disable
  4186. * the tachometer while the fan controller ramps up
  4187. * the speed (which can take up to a few *minutes*).
  4188. * Speeds up fan to 100% duty-cycle, which is far above
  4189. * the standard RPM levels. It is not impossible that
  4190. * it could cause hardware damage.
  4191. * 5-3 unused in some models. Extra bits for fan level
  4192. * in others, but still useless as all values above
  4193. * 7 map to the same speed as level 7 in these models.
  4194. * 2-0 fan level (0..7 usually)
  4195. * 0x00 = stop
  4196. * 0x07 = max (set when temperatures critical)
  4197. * Some ThinkPads may have other levels, see
  4198. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  4199. *
  4200. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  4201. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  4202. * does so, its initial value is meaningless (0x07).
  4203. *
  4204. * For firmware bugs, refer to:
  4205. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  4206. *
  4207. * ----
  4208. *
  4209. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  4210. * Main fan tachometer reading (in RPM)
  4211. *
  4212. * This register is present on all ThinkPads with a new-style EC, and
  4213. * it is known not to be present on the A21m/e, and T22, as there is
  4214. * something else in offset 0x84 according to the ACPI DSDT. Other
  4215. * ThinkPads from this same time period (and earlier) probably lack the
  4216. * tachometer as well.
  4217. *
  4218. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  4219. * was never fixed by IBM to report the EC firmware version string
  4220. * probably support the tachometer (like the early X models), so
  4221. * detecting it is quite hard. We need more data to know for sure.
  4222. *
  4223. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  4224. * might result.
  4225. *
  4226. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  4227. * mode.
  4228. *
  4229. * For firmware bugs, refer to:
  4230. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  4231. *
  4232. * TPACPI_FAN_WR_ACPI_FANS:
  4233. * ThinkPad X31, X40, X41. Not available in the X60.
  4234. *
  4235. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  4236. * high speed. ACPI DSDT seems to map these three speeds to levels
  4237. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  4238. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  4239. *
  4240. * The speeds are stored on handles
  4241. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  4242. *
  4243. * There are three default speed sets, acessible as handles:
  4244. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  4245. *
  4246. * ACPI DSDT switches which set is in use depending on various
  4247. * factors.
  4248. *
  4249. * TPACPI_FAN_WR_TPEC is also available and should be used to
  4250. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  4251. * but the ACPI tables just mention level 7.
  4252. */
  4253. enum { /* Fan control constants */
  4254. fan_status_offset = 0x2f, /* EC register 0x2f */
  4255. fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
  4256. * 0x84 must be read before 0x85 */
  4257. TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
  4258. TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
  4259. TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
  4260. };
  4261. enum fan_status_access_mode {
  4262. TPACPI_FAN_NONE = 0, /* No fan status or control */
  4263. TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
  4264. TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
  4265. };
  4266. enum fan_control_access_mode {
  4267. TPACPI_FAN_WR_NONE = 0, /* No fan control */
  4268. TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
  4269. TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
  4270. TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
  4271. };
  4272. enum fan_control_commands {
  4273. TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
  4274. TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
  4275. TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
  4276. * and also watchdog cmd */
  4277. };
  4278. static int fan_control_allowed;
  4279. static enum fan_status_access_mode fan_status_access_mode;
  4280. static enum fan_control_access_mode fan_control_access_mode;
  4281. static enum fan_control_commands fan_control_commands;
  4282. static u8 fan_control_initial_status;
  4283. static u8 fan_control_desired_level;
  4284. static int fan_watchdog_maxinterval;
  4285. static struct mutex fan_mutex;
  4286. static void fan_watchdog_fire(struct work_struct *ignored);
  4287. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  4288. TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  4289. TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
  4290. "\\FSPD", /* 600e/x, 770e, 770x */
  4291. ); /* all others */
  4292. TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
  4293. "JFNS", /* 770x-JL */
  4294. ); /* all others */
  4295. /*
  4296. * Call with fan_mutex held
  4297. */
  4298. static void fan_update_desired_level(u8 status)
  4299. {
  4300. if ((status &
  4301. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4302. if (status > 7)
  4303. fan_control_desired_level = 7;
  4304. else
  4305. fan_control_desired_level = status;
  4306. }
  4307. }
  4308. static int fan_get_status(u8 *status)
  4309. {
  4310. u8 s;
  4311. /* TODO:
  4312. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  4313. switch (fan_status_access_mode) {
  4314. case TPACPI_FAN_RD_ACPI_GFAN:
  4315. /* 570, 600e/x, 770e, 770x */
  4316. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  4317. return -EIO;
  4318. if (likely(status))
  4319. *status = s & 0x07;
  4320. break;
  4321. case TPACPI_FAN_RD_TPEC:
  4322. /* all except 570, 600e/x, 770e, 770x */
  4323. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  4324. return -EIO;
  4325. if (likely(status))
  4326. *status = s;
  4327. break;
  4328. default:
  4329. return -ENXIO;
  4330. }
  4331. return 0;
  4332. }
  4333. static int fan_get_status_safe(u8 *status)
  4334. {
  4335. int rc;
  4336. u8 s;
  4337. if (mutex_lock_interruptible(&fan_mutex))
  4338. return -ERESTARTSYS;
  4339. rc = fan_get_status(&s);
  4340. if (!rc)
  4341. fan_update_desired_level(s);
  4342. mutex_unlock(&fan_mutex);
  4343. if (status)
  4344. *status = s;
  4345. return rc;
  4346. }
  4347. static int fan_get_speed(unsigned int *speed)
  4348. {
  4349. u8 hi, lo;
  4350. switch (fan_status_access_mode) {
  4351. case TPACPI_FAN_RD_TPEC:
  4352. /* all except 570, 600e/x, 770e, 770x */
  4353. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  4354. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  4355. return -EIO;
  4356. if (likely(speed))
  4357. *speed = (hi << 8) | lo;
  4358. break;
  4359. default:
  4360. return -ENXIO;
  4361. }
  4362. return 0;
  4363. }
  4364. static int fan_set_level(int level)
  4365. {
  4366. if (!fan_control_allowed)
  4367. return -EPERM;
  4368. switch (fan_control_access_mode) {
  4369. case TPACPI_FAN_WR_ACPI_SFAN:
  4370. if (level >= 0 && level <= 7) {
  4371. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  4372. return -EIO;
  4373. } else
  4374. return -EINVAL;
  4375. break;
  4376. case TPACPI_FAN_WR_ACPI_FANS:
  4377. case TPACPI_FAN_WR_TPEC:
  4378. if ((level != TP_EC_FAN_AUTO) &&
  4379. (level != TP_EC_FAN_FULLSPEED) &&
  4380. ((level < 0) || (level > 7)))
  4381. return -EINVAL;
  4382. /* safety net should the EC not support AUTO
  4383. * or FULLSPEED mode bits and just ignore them */
  4384. if (level & TP_EC_FAN_FULLSPEED)
  4385. level |= 7; /* safety min speed 7 */
  4386. else if (level & TP_EC_FAN_AUTO)
  4387. level |= 4; /* safety min speed 4 */
  4388. if (!acpi_ec_write(fan_status_offset, level))
  4389. return -EIO;
  4390. else
  4391. tp_features.fan_ctrl_status_undef = 0;
  4392. break;
  4393. default:
  4394. return -ENXIO;
  4395. }
  4396. return 0;
  4397. }
  4398. static int fan_set_level_safe(int level)
  4399. {
  4400. int rc;
  4401. if (!fan_control_allowed)
  4402. return -EPERM;
  4403. if (mutex_lock_interruptible(&fan_mutex))
  4404. return -ERESTARTSYS;
  4405. if (level == TPACPI_FAN_LAST_LEVEL)
  4406. level = fan_control_desired_level;
  4407. rc = fan_set_level(level);
  4408. if (!rc)
  4409. fan_update_desired_level(level);
  4410. mutex_unlock(&fan_mutex);
  4411. return rc;
  4412. }
  4413. static int fan_set_enable(void)
  4414. {
  4415. u8 s;
  4416. int rc;
  4417. if (!fan_control_allowed)
  4418. return -EPERM;
  4419. if (mutex_lock_interruptible(&fan_mutex))
  4420. return -ERESTARTSYS;
  4421. switch (fan_control_access_mode) {
  4422. case TPACPI_FAN_WR_ACPI_FANS:
  4423. case TPACPI_FAN_WR_TPEC:
  4424. rc = fan_get_status(&s);
  4425. if (rc < 0)
  4426. break;
  4427. /* Don't go out of emergency fan mode */
  4428. if (s != 7) {
  4429. s &= 0x07;
  4430. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  4431. }
  4432. if (!acpi_ec_write(fan_status_offset, s))
  4433. rc = -EIO;
  4434. else {
  4435. tp_features.fan_ctrl_status_undef = 0;
  4436. rc = 0;
  4437. }
  4438. break;
  4439. case TPACPI_FAN_WR_ACPI_SFAN:
  4440. rc = fan_get_status(&s);
  4441. if (rc < 0)
  4442. break;
  4443. s &= 0x07;
  4444. /* Set fan to at least level 4 */
  4445. s |= 4;
  4446. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  4447. rc = -EIO;
  4448. else
  4449. rc = 0;
  4450. break;
  4451. default:
  4452. rc = -ENXIO;
  4453. }
  4454. mutex_unlock(&fan_mutex);
  4455. return rc;
  4456. }
  4457. static int fan_set_disable(void)
  4458. {
  4459. int rc;
  4460. if (!fan_control_allowed)
  4461. return -EPERM;
  4462. if (mutex_lock_interruptible(&fan_mutex))
  4463. return -ERESTARTSYS;
  4464. rc = 0;
  4465. switch (fan_control_access_mode) {
  4466. case TPACPI_FAN_WR_ACPI_FANS:
  4467. case TPACPI_FAN_WR_TPEC:
  4468. if (!acpi_ec_write(fan_status_offset, 0x00))
  4469. rc = -EIO;
  4470. else {
  4471. fan_control_desired_level = 0;
  4472. tp_features.fan_ctrl_status_undef = 0;
  4473. }
  4474. break;
  4475. case TPACPI_FAN_WR_ACPI_SFAN:
  4476. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  4477. rc = -EIO;
  4478. else
  4479. fan_control_desired_level = 0;
  4480. break;
  4481. default:
  4482. rc = -ENXIO;
  4483. }
  4484. mutex_unlock(&fan_mutex);
  4485. return rc;
  4486. }
  4487. static int fan_set_speed(int speed)
  4488. {
  4489. int rc;
  4490. if (!fan_control_allowed)
  4491. return -EPERM;
  4492. if (mutex_lock_interruptible(&fan_mutex))
  4493. return -ERESTARTSYS;
  4494. rc = 0;
  4495. switch (fan_control_access_mode) {
  4496. case TPACPI_FAN_WR_ACPI_FANS:
  4497. if (speed >= 0 && speed <= 65535) {
  4498. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  4499. speed, speed, speed))
  4500. rc = -EIO;
  4501. } else
  4502. rc = -EINVAL;
  4503. break;
  4504. default:
  4505. rc = -ENXIO;
  4506. }
  4507. mutex_unlock(&fan_mutex);
  4508. return rc;
  4509. }
  4510. static void fan_watchdog_reset(void)
  4511. {
  4512. static int fan_watchdog_active;
  4513. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  4514. return;
  4515. if (fan_watchdog_active)
  4516. cancel_delayed_work(&fan_watchdog_task);
  4517. if (fan_watchdog_maxinterval > 0 &&
  4518. tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
  4519. fan_watchdog_active = 1;
  4520. if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
  4521. msecs_to_jiffies(fan_watchdog_maxinterval
  4522. * 1000))) {
  4523. printk(TPACPI_ERR
  4524. "failed to queue the fan watchdog, "
  4525. "watchdog will not trigger\n");
  4526. }
  4527. } else
  4528. fan_watchdog_active = 0;
  4529. }
  4530. static void fan_watchdog_fire(struct work_struct *ignored)
  4531. {
  4532. int rc;
  4533. if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
  4534. return;
  4535. printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
  4536. rc = fan_set_enable();
  4537. if (rc < 0) {
  4538. printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
  4539. "will try again later...\n", -rc);
  4540. /* reschedule for later */
  4541. fan_watchdog_reset();
  4542. }
  4543. }
  4544. /*
  4545. * SYSFS fan layout: hwmon compatible (device)
  4546. *
  4547. * pwm*_enable:
  4548. * 0: "disengaged" mode
  4549. * 1: manual mode
  4550. * 2: native EC "auto" mode (recommended, hardware default)
  4551. *
  4552. * pwm*: set speed in manual mode, ignored otherwise.
  4553. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  4554. * interpolation.
  4555. *
  4556. * fan*_input: tachometer reading, RPM
  4557. *
  4558. *
  4559. * SYSFS fan layout: extensions
  4560. *
  4561. * fan_watchdog (driver):
  4562. * fan watchdog interval in seconds, 0 disables (default), max 120
  4563. */
  4564. /* sysfs fan pwm1_enable ----------------------------------------------- */
  4565. static ssize_t fan_pwm1_enable_show(struct device *dev,
  4566. struct device_attribute *attr,
  4567. char *buf)
  4568. {
  4569. int res, mode;
  4570. u8 status;
  4571. res = fan_get_status_safe(&status);
  4572. if (res)
  4573. return res;
  4574. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4575. if (status != fan_control_initial_status) {
  4576. tp_features.fan_ctrl_status_undef = 0;
  4577. } else {
  4578. /* Return most likely status. In fact, it
  4579. * might be the only possible status */
  4580. status = TP_EC_FAN_AUTO;
  4581. }
  4582. }
  4583. if (status & TP_EC_FAN_FULLSPEED) {
  4584. mode = 0;
  4585. } else if (status & TP_EC_FAN_AUTO) {
  4586. mode = 2;
  4587. } else
  4588. mode = 1;
  4589. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  4590. }
  4591. static ssize_t fan_pwm1_enable_store(struct device *dev,
  4592. struct device_attribute *attr,
  4593. const char *buf, size_t count)
  4594. {
  4595. unsigned long t;
  4596. int res, level;
  4597. if (parse_strtoul(buf, 2, &t))
  4598. return -EINVAL;
  4599. switch (t) {
  4600. case 0:
  4601. level = TP_EC_FAN_FULLSPEED;
  4602. break;
  4603. case 1:
  4604. level = TPACPI_FAN_LAST_LEVEL;
  4605. break;
  4606. case 2:
  4607. level = TP_EC_FAN_AUTO;
  4608. break;
  4609. case 3:
  4610. /* reserved for software-controlled auto mode */
  4611. return -ENOSYS;
  4612. default:
  4613. return -EINVAL;
  4614. }
  4615. res = fan_set_level_safe(level);
  4616. if (res == -ENXIO)
  4617. return -EINVAL;
  4618. else if (res < 0)
  4619. return res;
  4620. fan_watchdog_reset();
  4621. return count;
  4622. }
  4623. static struct device_attribute dev_attr_fan_pwm1_enable =
  4624. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  4625. fan_pwm1_enable_show, fan_pwm1_enable_store);
  4626. /* sysfs fan pwm1 ------------------------------------------------------ */
  4627. static ssize_t fan_pwm1_show(struct device *dev,
  4628. struct device_attribute *attr,
  4629. char *buf)
  4630. {
  4631. int res;
  4632. u8 status;
  4633. res = fan_get_status_safe(&status);
  4634. if (res)
  4635. return res;
  4636. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4637. if (status != fan_control_initial_status) {
  4638. tp_features.fan_ctrl_status_undef = 0;
  4639. } else {
  4640. status = TP_EC_FAN_AUTO;
  4641. }
  4642. }
  4643. if ((status &
  4644. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  4645. status = fan_control_desired_level;
  4646. if (status > 7)
  4647. status = 7;
  4648. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  4649. }
  4650. static ssize_t fan_pwm1_store(struct device *dev,
  4651. struct device_attribute *attr,
  4652. const char *buf, size_t count)
  4653. {
  4654. unsigned long s;
  4655. int rc;
  4656. u8 status, newlevel;
  4657. if (parse_strtoul(buf, 255, &s))
  4658. return -EINVAL;
  4659. /* scale down from 0-255 to 0-7 */
  4660. newlevel = (s >> 5) & 0x07;
  4661. if (mutex_lock_interruptible(&fan_mutex))
  4662. return -ERESTARTSYS;
  4663. rc = fan_get_status(&status);
  4664. if (!rc && (status &
  4665. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  4666. rc = fan_set_level(newlevel);
  4667. if (rc == -ENXIO)
  4668. rc = -EINVAL;
  4669. else if (!rc) {
  4670. fan_update_desired_level(newlevel);
  4671. fan_watchdog_reset();
  4672. }
  4673. }
  4674. mutex_unlock(&fan_mutex);
  4675. return (rc)? rc : count;
  4676. }
  4677. static struct device_attribute dev_attr_fan_pwm1 =
  4678. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  4679. fan_pwm1_show, fan_pwm1_store);
  4680. /* sysfs fan fan1_input ------------------------------------------------ */
  4681. static ssize_t fan_fan1_input_show(struct device *dev,
  4682. struct device_attribute *attr,
  4683. char *buf)
  4684. {
  4685. int res;
  4686. unsigned int speed;
  4687. res = fan_get_speed(&speed);
  4688. if (res < 0)
  4689. return res;
  4690. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  4691. }
  4692. static struct device_attribute dev_attr_fan_fan1_input =
  4693. __ATTR(fan1_input, S_IRUGO,
  4694. fan_fan1_input_show, NULL);
  4695. /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
  4696. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  4697. char *buf)
  4698. {
  4699. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  4700. }
  4701. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  4702. const char *buf, size_t count)
  4703. {
  4704. unsigned long t;
  4705. if (parse_strtoul(buf, 120, &t))
  4706. return -EINVAL;
  4707. if (!fan_control_allowed)
  4708. return -EPERM;
  4709. fan_watchdog_maxinterval = t;
  4710. fan_watchdog_reset();
  4711. return count;
  4712. }
  4713. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  4714. fan_fan_watchdog_show, fan_fan_watchdog_store);
  4715. /* --------------------------------------------------------------------- */
  4716. static struct attribute *fan_attributes[] = {
  4717. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  4718. &dev_attr_fan_fan1_input.attr,
  4719. NULL
  4720. };
  4721. static const struct attribute_group fan_attr_group = {
  4722. .attrs = fan_attributes,
  4723. };
  4724. static int __init fan_init(struct ibm_init_struct *iibm)
  4725. {
  4726. int rc;
  4727. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  4728. mutex_init(&fan_mutex);
  4729. fan_status_access_mode = TPACPI_FAN_NONE;
  4730. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4731. fan_control_commands = 0;
  4732. fan_watchdog_maxinterval = 0;
  4733. tp_features.fan_ctrl_status_undef = 0;
  4734. fan_control_desired_level = 7;
  4735. TPACPI_ACPIHANDLE_INIT(fans);
  4736. TPACPI_ACPIHANDLE_INIT(gfan);
  4737. TPACPI_ACPIHANDLE_INIT(sfan);
  4738. if (gfan_handle) {
  4739. /* 570, 600e/x, 770e, 770x */
  4740. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  4741. } else {
  4742. /* all other ThinkPads: note that even old-style
  4743. * ThinkPad ECs supports the fan control register */
  4744. if (likely(acpi_ec_read(fan_status_offset,
  4745. &fan_control_initial_status))) {
  4746. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  4747. /* In some ThinkPads, neither the EC nor the ACPI
  4748. * DSDT initialize the fan status, and it ends up
  4749. * being set to 0x07 when it *could* be either
  4750. * 0x07 or 0x80.
  4751. *
  4752. * Enable for TP-1Y (T43), TP-78 (R51e),
  4753. * TP-76 (R52), TP-70 (T43, R52), which are known
  4754. * to be buggy. */
  4755. if (fan_control_initial_status == 0x07) {
  4756. switch (thinkpad_id.ec_model) {
  4757. case 0x5931: /* TP-1Y */
  4758. case 0x3837: /* TP-78 */
  4759. case 0x3637: /* TP-76 */
  4760. case 0x3037: /* TP-70 */
  4761. printk(TPACPI_NOTICE
  4762. "fan_init: initial fan status "
  4763. "is unknown, assuming it is "
  4764. "in auto mode\n");
  4765. tp_features.fan_ctrl_status_undef = 1;
  4766. ;;
  4767. }
  4768. }
  4769. } else {
  4770. printk(TPACPI_ERR
  4771. "ThinkPad ACPI EC access misbehaving, "
  4772. "fan status and control unavailable\n");
  4773. return 1;
  4774. }
  4775. }
  4776. if (sfan_handle) {
  4777. /* 570, 770x-JL */
  4778. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  4779. fan_control_commands |=
  4780. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  4781. } else {
  4782. if (!gfan_handle) {
  4783. /* gfan without sfan means no fan control */
  4784. /* all other models implement TP EC 0x2f control */
  4785. if (fans_handle) {
  4786. /* X31, X40, X41 */
  4787. fan_control_access_mode =
  4788. TPACPI_FAN_WR_ACPI_FANS;
  4789. fan_control_commands |=
  4790. TPACPI_FAN_CMD_SPEED |
  4791. TPACPI_FAN_CMD_LEVEL |
  4792. TPACPI_FAN_CMD_ENABLE;
  4793. } else {
  4794. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  4795. fan_control_commands |=
  4796. TPACPI_FAN_CMD_LEVEL |
  4797. TPACPI_FAN_CMD_ENABLE;
  4798. }
  4799. }
  4800. }
  4801. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  4802. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  4803. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  4804. fan_status_access_mode, fan_control_access_mode);
  4805. /* fan control master switch */
  4806. if (!fan_control_allowed) {
  4807. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  4808. fan_control_commands = 0;
  4809. dbg_printk(TPACPI_DBG_INIT,
  4810. "fan control features disabled by parameter\n");
  4811. }
  4812. /* update fan_control_desired_level */
  4813. if (fan_status_access_mode != TPACPI_FAN_NONE)
  4814. fan_get_status_safe(NULL);
  4815. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  4816. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  4817. rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
  4818. &fan_attr_group);
  4819. if (rc < 0)
  4820. return rc;
  4821. rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
  4822. &driver_attr_fan_watchdog);
  4823. if (rc < 0) {
  4824. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
  4825. &fan_attr_group);
  4826. return rc;
  4827. }
  4828. return 0;
  4829. } else
  4830. return 1;
  4831. }
  4832. static void fan_exit(void)
  4833. {
  4834. vdbg_printk(TPACPI_DBG_EXIT,
  4835. "cancelling any pending fan watchdog tasks\n");
  4836. /* FIXME: can we really do this unconditionally? */
  4837. sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
  4838. driver_remove_file(&tpacpi_hwmon_pdriver.driver,
  4839. &driver_attr_fan_watchdog);
  4840. cancel_delayed_work(&fan_watchdog_task);
  4841. flush_workqueue(tpacpi_wq);
  4842. }
  4843. static int fan_read(char *p)
  4844. {
  4845. int len = 0;
  4846. int rc;
  4847. u8 status;
  4848. unsigned int speed = 0;
  4849. switch (fan_status_access_mode) {
  4850. case TPACPI_FAN_RD_ACPI_GFAN:
  4851. /* 570, 600e/x, 770e, 770x */
  4852. rc = fan_get_status_safe(&status);
  4853. if (rc < 0)
  4854. return rc;
  4855. len += sprintf(p + len, "status:\t\t%s\n"
  4856. "level:\t\t%d\n",
  4857. (status != 0) ? "enabled" : "disabled", status);
  4858. break;
  4859. case TPACPI_FAN_RD_TPEC:
  4860. /* all except 570, 600e/x, 770e, 770x */
  4861. rc = fan_get_status_safe(&status);
  4862. if (rc < 0)
  4863. return rc;
  4864. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  4865. if (status != fan_control_initial_status)
  4866. tp_features.fan_ctrl_status_undef = 0;
  4867. else
  4868. /* Return most likely status. In fact, it
  4869. * might be the only possible status */
  4870. status = TP_EC_FAN_AUTO;
  4871. }
  4872. len += sprintf(p + len, "status:\t\t%s\n",
  4873. (status != 0) ? "enabled" : "disabled");
  4874. rc = fan_get_speed(&speed);
  4875. if (rc < 0)
  4876. return rc;
  4877. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  4878. if (status & TP_EC_FAN_FULLSPEED)
  4879. /* Disengaged mode takes precedence */
  4880. len += sprintf(p + len, "level:\t\tdisengaged\n");
  4881. else if (status & TP_EC_FAN_AUTO)
  4882. len += sprintf(p + len, "level:\t\tauto\n");
  4883. else
  4884. len += sprintf(p + len, "level:\t\t%d\n", status);
  4885. break;
  4886. case TPACPI_FAN_NONE:
  4887. default:
  4888. len += sprintf(p + len, "status:\t\tnot supported\n");
  4889. }
  4890. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  4891. len += sprintf(p + len, "commands:\tlevel <level>");
  4892. switch (fan_control_access_mode) {
  4893. case TPACPI_FAN_WR_ACPI_SFAN:
  4894. len += sprintf(p + len, " (<level> is 0-7)\n");
  4895. break;
  4896. default:
  4897. len += sprintf(p + len, " (<level> is 0-7, "
  4898. "auto, disengaged, full-speed)\n");
  4899. break;
  4900. }
  4901. }
  4902. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  4903. len += sprintf(p + len, "commands:\tenable, disable\n"
  4904. "commands:\twatchdog <timeout> (<timeout> "
  4905. "is 0 (off), 1-120 (seconds))\n");
  4906. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  4907. len += sprintf(p + len, "commands:\tspeed <speed>"
  4908. " (<speed> is 0-65535)\n");
  4909. return len;
  4910. }
  4911. static int fan_write_cmd_level(const char *cmd, int *rc)
  4912. {
  4913. int level;
  4914. if (strlencmp(cmd, "level auto") == 0)
  4915. level = TP_EC_FAN_AUTO;
  4916. else if ((strlencmp(cmd, "level disengaged") == 0) |
  4917. (strlencmp(cmd, "level full-speed") == 0))
  4918. level = TP_EC_FAN_FULLSPEED;
  4919. else if (sscanf(cmd, "level %d", &level) != 1)
  4920. return 0;
  4921. *rc = fan_set_level_safe(level);
  4922. if (*rc == -ENXIO)
  4923. printk(TPACPI_ERR "level command accepted for unsupported "
  4924. "access mode %d", fan_control_access_mode);
  4925. return 1;
  4926. }
  4927. static int fan_write_cmd_enable(const char *cmd, int *rc)
  4928. {
  4929. if (strlencmp(cmd, "enable") != 0)
  4930. return 0;
  4931. *rc = fan_set_enable();
  4932. if (*rc == -ENXIO)
  4933. printk(TPACPI_ERR "enable command accepted for unsupported "
  4934. "access mode %d", fan_control_access_mode);
  4935. return 1;
  4936. }
  4937. static int fan_write_cmd_disable(const char *cmd, int *rc)
  4938. {
  4939. if (strlencmp(cmd, "disable") != 0)
  4940. return 0;
  4941. *rc = fan_set_disable();
  4942. if (*rc == -ENXIO)
  4943. printk(TPACPI_ERR "disable command accepted for unsupported "
  4944. "access mode %d", fan_control_access_mode);
  4945. return 1;
  4946. }
  4947. static int fan_write_cmd_speed(const char *cmd, int *rc)
  4948. {
  4949. int speed;
  4950. /* TODO:
  4951. * Support speed <low> <medium> <high> ? */
  4952. if (sscanf(cmd, "speed %d", &speed) != 1)
  4953. return 0;
  4954. *rc = fan_set_speed(speed);
  4955. if (*rc == -ENXIO)
  4956. printk(TPACPI_ERR "speed command accepted for unsupported "
  4957. "access mode %d", fan_control_access_mode);
  4958. return 1;
  4959. }
  4960. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  4961. {
  4962. int interval;
  4963. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  4964. return 0;
  4965. if (interval < 0 || interval > 120)
  4966. *rc = -EINVAL;
  4967. else
  4968. fan_watchdog_maxinterval = interval;
  4969. return 1;
  4970. }
  4971. static int fan_write(char *buf)
  4972. {
  4973. char *cmd;
  4974. int rc = 0;
  4975. while (!rc && (cmd = next_cmd(&buf))) {
  4976. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  4977. fan_write_cmd_level(cmd, &rc)) &&
  4978. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  4979. (fan_write_cmd_enable(cmd, &rc) ||
  4980. fan_write_cmd_disable(cmd, &rc) ||
  4981. fan_write_cmd_watchdog(cmd, &rc))) &&
  4982. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  4983. fan_write_cmd_speed(cmd, &rc))
  4984. )
  4985. rc = -EINVAL;
  4986. else if (!rc)
  4987. fan_watchdog_reset();
  4988. }
  4989. return rc;
  4990. }
  4991. static struct ibm_struct fan_driver_data = {
  4992. .name = "fan",
  4993. .read = fan_read,
  4994. .write = fan_write,
  4995. .exit = fan_exit,
  4996. };
  4997. /****************************************************************************
  4998. ****************************************************************************
  4999. *
  5000. * Infrastructure
  5001. *
  5002. ****************************************************************************
  5003. ****************************************************************************/
  5004. /* sysfs name ---------------------------------------------------------- */
  5005. static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
  5006. struct device_attribute *attr,
  5007. char *buf)
  5008. {
  5009. return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
  5010. }
  5011. static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
  5012. __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
  5013. /* --------------------------------------------------------------------- */
  5014. /* /proc support */
  5015. static struct proc_dir_entry *proc_dir;
  5016. /*
  5017. * Module and infrastructure proble, init and exit handling
  5018. */
  5019. static int force_load;
  5020. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  5021. static const char * __init str_supported(int is_supported)
  5022. {
  5023. static char text_unsupported[] __initdata = "not supported";
  5024. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  5025. }
  5026. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  5027. static void ibm_exit(struct ibm_struct *ibm)
  5028. {
  5029. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  5030. list_del_init(&ibm->all_drivers);
  5031. if (ibm->flags.acpi_notify_installed) {
  5032. dbg_printk(TPACPI_DBG_EXIT,
  5033. "%s: acpi_remove_notify_handler\n", ibm->name);
  5034. BUG_ON(!ibm->acpi);
  5035. acpi_remove_notify_handler(*ibm->acpi->handle,
  5036. ibm->acpi->type,
  5037. dispatch_acpi_notify);
  5038. ibm->flags.acpi_notify_installed = 0;
  5039. ibm->flags.acpi_notify_installed = 0;
  5040. }
  5041. if (ibm->flags.proc_created) {
  5042. dbg_printk(TPACPI_DBG_EXIT,
  5043. "%s: remove_proc_entry\n", ibm->name);
  5044. remove_proc_entry(ibm->name, proc_dir);
  5045. ibm->flags.proc_created = 0;
  5046. }
  5047. if (ibm->flags.acpi_driver_registered) {
  5048. dbg_printk(TPACPI_DBG_EXIT,
  5049. "%s: acpi_bus_unregister_driver\n", ibm->name);
  5050. BUG_ON(!ibm->acpi);
  5051. acpi_bus_unregister_driver(ibm->acpi->driver);
  5052. kfree(ibm->acpi->driver);
  5053. ibm->acpi->driver = NULL;
  5054. ibm->flags.acpi_driver_registered = 0;
  5055. }
  5056. if (ibm->flags.init_called && ibm->exit) {
  5057. ibm->exit();
  5058. ibm->flags.init_called = 0;
  5059. }
  5060. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  5061. }
  5062. static int __init ibm_init(struct ibm_init_struct *iibm)
  5063. {
  5064. int ret;
  5065. struct ibm_struct *ibm = iibm->data;
  5066. struct proc_dir_entry *entry;
  5067. BUG_ON(ibm == NULL);
  5068. INIT_LIST_HEAD(&ibm->all_drivers);
  5069. if (ibm->flags.experimental && !experimental)
  5070. return 0;
  5071. dbg_printk(TPACPI_DBG_INIT,
  5072. "probing for %s\n", ibm->name);
  5073. if (iibm->init) {
  5074. ret = iibm->init(iibm);
  5075. if (ret > 0)
  5076. return 0; /* probe failed */
  5077. if (ret)
  5078. return ret;
  5079. ibm->flags.init_called = 1;
  5080. }
  5081. if (ibm->acpi) {
  5082. if (ibm->acpi->hid) {
  5083. ret = register_tpacpi_subdriver(ibm);
  5084. if (ret)
  5085. goto err_out;
  5086. }
  5087. if (ibm->acpi->notify) {
  5088. ret = setup_acpi_notify(ibm);
  5089. if (ret == -ENODEV) {
  5090. printk(TPACPI_NOTICE "disabling subdriver %s\n",
  5091. ibm->name);
  5092. ret = 0;
  5093. goto err_out;
  5094. }
  5095. if (ret < 0)
  5096. goto err_out;
  5097. }
  5098. }
  5099. dbg_printk(TPACPI_DBG_INIT,
  5100. "%s installed\n", ibm->name);
  5101. if (ibm->read) {
  5102. entry = create_proc_entry(ibm->name,
  5103. S_IFREG | S_IRUGO | S_IWUSR,
  5104. proc_dir);
  5105. if (!entry) {
  5106. printk(TPACPI_ERR "unable to create proc entry %s\n",
  5107. ibm->name);
  5108. ret = -ENODEV;
  5109. goto err_out;
  5110. }
  5111. entry->owner = THIS_MODULE;
  5112. entry->data = ibm;
  5113. entry->read_proc = &dispatch_procfs_read;
  5114. if (ibm->write)
  5115. entry->write_proc = &dispatch_procfs_write;
  5116. ibm->flags.proc_created = 1;
  5117. }
  5118. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  5119. return 0;
  5120. err_out:
  5121. dbg_printk(TPACPI_DBG_INIT,
  5122. "%s: at error exit path with result %d\n",
  5123. ibm->name, ret);
  5124. ibm_exit(ibm);
  5125. return (ret < 0)? ret : 0;
  5126. }
  5127. /* Probing */
  5128. static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
  5129. {
  5130. const struct dmi_device *dev = NULL;
  5131. char ec_fw_string[18];
  5132. if (!tp)
  5133. return;
  5134. memset(tp, 0, sizeof(*tp));
  5135. if (dmi_name_in_vendors("IBM"))
  5136. tp->vendor = PCI_VENDOR_ID_IBM;
  5137. else if (dmi_name_in_vendors("LENOVO"))
  5138. tp->vendor = PCI_VENDOR_ID_LENOVO;
  5139. else
  5140. return;
  5141. tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
  5142. GFP_KERNEL);
  5143. if (!tp->bios_version_str)
  5144. return;
  5145. tp->bios_model = tp->bios_version_str[0]
  5146. | (tp->bios_version_str[1] << 8);
  5147. /*
  5148. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  5149. * X32 or newer, all Z series; Some models must have an
  5150. * up-to-date BIOS or they will not be detected.
  5151. *
  5152. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5153. */
  5154. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  5155. if (sscanf(dev->name,
  5156. "IBM ThinkPad Embedded Controller -[%17c",
  5157. ec_fw_string) == 1) {
  5158. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  5159. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  5160. tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
  5161. tp->ec_model = ec_fw_string[0]
  5162. | (ec_fw_string[1] << 8);
  5163. break;
  5164. }
  5165. }
  5166. tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
  5167. GFP_KERNEL);
  5168. if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
  5169. kfree(tp->model_str);
  5170. tp->model_str = NULL;
  5171. }
  5172. tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
  5173. GFP_KERNEL);
  5174. }
  5175. static int __init probe_for_thinkpad(void)
  5176. {
  5177. int is_thinkpad;
  5178. if (acpi_disabled)
  5179. return -ENODEV;
  5180. /*
  5181. * Non-ancient models have better DMI tagging, but very old models
  5182. * don't.
  5183. */
  5184. is_thinkpad = (thinkpad_id.model_str != NULL);
  5185. /* ec is required because many other handles are relative to it */
  5186. TPACPI_ACPIHANDLE_INIT(ec);
  5187. if (!ec_handle) {
  5188. if (is_thinkpad)
  5189. printk(TPACPI_ERR
  5190. "Not yet supported ThinkPad detected!\n");
  5191. return -ENODEV;
  5192. }
  5193. /*
  5194. * Risks a regression on very old machines, but reduces potential
  5195. * false positives a damn great deal
  5196. */
  5197. if (!is_thinkpad)
  5198. is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
  5199. if (!is_thinkpad && !force_load)
  5200. return -ENODEV;
  5201. return 0;
  5202. }
  5203. /* Module init, exit, parameters */
  5204. static struct ibm_init_struct ibms_init[] __initdata = {
  5205. {
  5206. .init = thinkpad_acpi_driver_init,
  5207. .data = &thinkpad_acpi_driver_data,
  5208. },
  5209. {
  5210. .init = hotkey_init,
  5211. .data = &hotkey_driver_data,
  5212. },
  5213. {
  5214. .init = bluetooth_init,
  5215. .data = &bluetooth_driver_data,
  5216. },
  5217. {
  5218. .init = wan_init,
  5219. .data = &wan_driver_data,
  5220. },
  5221. #ifdef CONFIG_THINKPAD_ACPI_VIDEO
  5222. {
  5223. .init = video_init,
  5224. .data = &video_driver_data,
  5225. },
  5226. #endif
  5227. {
  5228. .init = light_init,
  5229. .data = &light_driver_data,
  5230. },
  5231. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  5232. {
  5233. .init = dock_init,
  5234. .data = &dock_driver_data[0],
  5235. },
  5236. {
  5237. .init = dock_init2,
  5238. .data = &dock_driver_data[1],
  5239. },
  5240. #endif
  5241. #ifdef CONFIG_THINKPAD_ACPI_BAY
  5242. {
  5243. .init = bay_init,
  5244. .data = &bay_driver_data,
  5245. },
  5246. #endif
  5247. {
  5248. .init = cmos_init,
  5249. .data = &cmos_driver_data,
  5250. },
  5251. {
  5252. .init = led_init,
  5253. .data = &led_driver_data,
  5254. },
  5255. {
  5256. .init = beep_init,
  5257. .data = &beep_driver_data,
  5258. },
  5259. {
  5260. .init = thermal_init,
  5261. .data = &thermal_driver_data,
  5262. },
  5263. {
  5264. .data = &ecdump_driver_data,
  5265. },
  5266. {
  5267. .init = brightness_init,
  5268. .data = &brightness_driver_data,
  5269. },
  5270. {
  5271. .data = &volume_driver_data,
  5272. },
  5273. {
  5274. .init = fan_init,
  5275. .data = &fan_driver_data,
  5276. },
  5277. };
  5278. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  5279. {
  5280. unsigned int i;
  5281. struct ibm_struct *ibm;
  5282. if (!kp || !kp->name || !val)
  5283. return -EINVAL;
  5284. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5285. ibm = ibms_init[i].data;
  5286. WARN_ON(ibm == NULL);
  5287. if (!ibm || !ibm->name)
  5288. continue;
  5289. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  5290. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  5291. return -ENOSPC;
  5292. strcpy(ibms_init[i].param, val);
  5293. strcat(ibms_init[i].param, ",");
  5294. return 0;
  5295. }
  5296. }
  5297. return -EINVAL;
  5298. }
  5299. module_param(experimental, int, 0);
  5300. MODULE_PARM_DESC(experimental,
  5301. "Enables experimental features when non-zero");
  5302. module_param_named(debug, dbg_level, uint, 0);
  5303. MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
  5304. module_param(force_load, bool, 0);
  5305. MODULE_PARM_DESC(force_load,
  5306. "Attempts to load the driver even on a "
  5307. "mis-identified ThinkPad when true");
  5308. module_param_named(fan_control, fan_control_allowed, bool, 0);
  5309. MODULE_PARM_DESC(fan_control,
  5310. "Enables setting fan parameters features when true");
  5311. module_param_named(brightness_mode, brightness_mode, int, 0);
  5312. MODULE_PARM_DESC(brightness_mode,
  5313. "Selects brightness control strategy: "
  5314. "0=auto, 1=EC, 2=CMOS, 3=both");
  5315. module_param(brightness_enable, uint, 0);
  5316. MODULE_PARM_DESC(brightness_enable,
  5317. "Enables backlight control when 1, disables when 0");
  5318. module_param(hotkey_report_mode, uint, 0);
  5319. MODULE_PARM_DESC(hotkey_report_mode,
  5320. "used for backwards compatibility with userspace, "
  5321. "see documentation");
  5322. #define TPACPI_PARAM(feature) \
  5323. module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
  5324. MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
  5325. "at module load, see documentation")
  5326. TPACPI_PARAM(hotkey);
  5327. TPACPI_PARAM(bluetooth);
  5328. TPACPI_PARAM(video);
  5329. TPACPI_PARAM(light);
  5330. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  5331. TPACPI_PARAM(dock);
  5332. #endif
  5333. #ifdef CONFIG_THINKPAD_ACPI_BAY
  5334. TPACPI_PARAM(bay);
  5335. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  5336. TPACPI_PARAM(cmos);
  5337. TPACPI_PARAM(led);
  5338. TPACPI_PARAM(beep);
  5339. TPACPI_PARAM(ecdump);
  5340. TPACPI_PARAM(brightness);
  5341. TPACPI_PARAM(volume);
  5342. TPACPI_PARAM(fan);
  5343. static void thinkpad_acpi_module_exit(void)
  5344. {
  5345. struct ibm_struct *ibm, *itmp;
  5346. tpacpi_lifecycle = TPACPI_LIFE_EXITING;
  5347. list_for_each_entry_safe_reverse(ibm, itmp,
  5348. &tpacpi_all_drivers,
  5349. all_drivers) {
  5350. ibm_exit(ibm);
  5351. }
  5352. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  5353. if (tpacpi_inputdev) {
  5354. if (tp_features.input_device_registered)
  5355. input_unregister_device(tpacpi_inputdev);
  5356. else
  5357. input_free_device(tpacpi_inputdev);
  5358. }
  5359. if (tpacpi_hwmon)
  5360. hwmon_device_unregister(tpacpi_hwmon);
  5361. if (tp_features.sensors_pdev_attrs_registered)
  5362. device_remove_file(&tpacpi_sensors_pdev->dev,
  5363. &dev_attr_thinkpad_acpi_pdev_name);
  5364. if (tpacpi_sensors_pdev)
  5365. platform_device_unregister(tpacpi_sensors_pdev);
  5366. if (tpacpi_pdev)
  5367. platform_device_unregister(tpacpi_pdev);
  5368. if (tp_features.sensors_pdrv_attrs_registered)
  5369. tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
  5370. if (tp_features.platform_drv_attrs_registered)
  5371. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  5372. if (tp_features.sensors_pdrv_registered)
  5373. platform_driver_unregister(&tpacpi_hwmon_pdriver);
  5374. if (tp_features.platform_drv_registered)
  5375. platform_driver_unregister(&tpacpi_pdriver);
  5376. if (proc_dir)
  5377. remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
  5378. if (tpacpi_wq)
  5379. destroy_workqueue(tpacpi_wq);
  5380. kfree(thinkpad_id.bios_version_str);
  5381. kfree(thinkpad_id.ec_version_str);
  5382. kfree(thinkpad_id.model_str);
  5383. }
  5384. static int __init thinkpad_acpi_module_init(void)
  5385. {
  5386. int ret, i;
  5387. tpacpi_lifecycle = TPACPI_LIFE_INIT;
  5388. /* Parameter checking */
  5389. if (hotkey_report_mode > 2)
  5390. return -EINVAL;
  5391. /* Driver-level probe */
  5392. get_thinkpad_model_data(&thinkpad_id);
  5393. ret = probe_for_thinkpad();
  5394. if (ret) {
  5395. thinkpad_acpi_module_exit();
  5396. return ret;
  5397. }
  5398. /* Driver initialization */
  5399. TPACPI_ACPIHANDLE_INIT(ecrd);
  5400. TPACPI_ACPIHANDLE_INIT(ecwr);
  5401. tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
  5402. if (!tpacpi_wq) {
  5403. thinkpad_acpi_module_exit();
  5404. return -ENOMEM;
  5405. }
  5406. proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
  5407. if (!proc_dir) {
  5408. printk(TPACPI_ERR
  5409. "unable to create proc dir " TPACPI_PROC_DIR);
  5410. thinkpad_acpi_module_exit();
  5411. return -ENODEV;
  5412. }
  5413. proc_dir->owner = THIS_MODULE;
  5414. ret = platform_driver_register(&tpacpi_pdriver);
  5415. if (ret) {
  5416. printk(TPACPI_ERR
  5417. "unable to register main platform driver\n");
  5418. thinkpad_acpi_module_exit();
  5419. return ret;
  5420. }
  5421. tp_features.platform_drv_registered = 1;
  5422. ret = platform_driver_register(&tpacpi_hwmon_pdriver);
  5423. if (ret) {
  5424. printk(TPACPI_ERR
  5425. "unable to register hwmon platform driver\n");
  5426. thinkpad_acpi_module_exit();
  5427. return ret;
  5428. }
  5429. tp_features.sensors_pdrv_registered = 1;
  5430. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  5431. if (!ret) {
  5432. tp_features.platform_drv_attrs_registered = 1;
  5433. ret = tpacpi_create_driver_attributes(
  5434. &tpacpi_hwmon_pdriver.driver);
  5435. }
  5436. if (ret) {
  5437. printk(TPACPI_ERR
  5438. "unable to create sysfs driver attributes\n");
  5439. thinkpad_acpi_module_exit();
  5440. return ret;
  5441. }
  5442. tp_features.sensors_pdrv_attrs_registered = 1;
  5443. /* Device initialization */
  5444. tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
  5445. NULL, 0);
  5446. if (IS_ERR(tpacpi_pdev)) {
  5447. ret = PTR_ERR(tpacpi_pdev);
  5448. tpacpi_pdev = NULL;
  5449. printk(TPACPI_ERR "unable to register platform device\n");
  5450. thinkpad_acpi_module_exit();
  5451. return ret;
  5452. }
  5453. tpacpi_sensors_pdev = platform_device_register_simple(
  5454. TPACPI_HWMON_DRVR_NAME,
  5455. -1, NULL, 0);
  5456. if (IS_ERR(tpacpi_sensors_pdev)) {
  5457. ret = PTR_ERR(tpacpi_sensors_pdev);
  5458. tpacpi_sensors_pdev = NULL;
  5459. printk(TPACPI_ERR
  5460. "unable to register hwmon platform device\n");
  5461. thinkpad_acpi_module_exit();
  5462. return ret;
  5463. }
  5464. ret = device_create_file(&tpacpi_sensors_pdev->dev,
  5465. &dev_attr_thinkpad_acpi_pdev_name);
  5466. if (ret) {
  5467. printk(TPACPI_ERR
  5468. "unable to create sysfs hwmon device attributes\n");
  5469. thinkpad_acpi_module_exit();
  5470. return ret;
  5471. }
  5472. tp_features.sensors_pdev_attrs_registered = 1;
  5473. tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
  5474. if (IS_ERR(tpacpi_hwmon)) {
  5475. ret = PTR_ERR(tpacpi_hwmon);
  5476. tpacpi_hwmon = NULL;
  5477. printk(TPACPI_ERR "unable to register hwmon device\n");
  5478. thinkpad_acpi_module_exit();
  5479. return ret;
  5480. }
  5481. mutex_init(&tpacpi_inputdev_send_mutex);
  5482. tpacpi_inputdev = input_allocate_device();
  5483. if (!tpacpi_inputdev) {
  5484. printk(TPACPI_ERR "unable to allocate input device\n");
  5485. thinkpad_acpi_module_exit();
  5486. return -ENOMEM;
  5487. } else {
  5488. /* Prepare input device, but don't register */
  5489. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  5490. tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
  5491. tpacpi_inputdev->id.bustype = BUS_HOST;
  5492. tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
  5493. thinkpad_id.vendor :
  5494. PCI_VENDOR_ID_IBM;
  5495. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  5496. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  5497. }
  5498. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  5499. ret = ibm_init(&ibms_init[i]);
  5500. if (ret >= 0 && *ibms_init[i].param)
  5501. ret = ibms_init[i].data->write(ibms_init[i].param);
  5502. if (ret < 0) {
  5503. thinkpad_acpi_module_exit();
  5504. return ret;
  5505. }
  5506. }
  5507. ret = input_register_device(tpacpi_inputdev);
  5508. if (ret < 0) {
  5509. printk(TPACPI_ERR "unable to register input device\n");
  5510. thinkpad_acpi_module_exit();
  5511. return ret;
  5512. } else {
  5513. tp_features.input_device_registered = 1;
  5514. }
  5515. tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
  5516. return 0;
  5517. }
  5518. /* Please remove this in year 2009 */
  5519. MODULE_ALIAS("ibm_acpi");
  5520. MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
  5521. /*
  5522. * DMI matching for module autoloading
  5523. *
  5524. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  5525. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  5526. *
  5527. * Only models listed in thinkwiki will be supported, so add yours
  5528. * if it is not there yet.
  5529. */
  5530. #define IBM_BIOS_MODULE_ALIAS(__type) \
  5531. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  5532. /* Non-ancient thinkpads */
  5533. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  5534. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  5535. /* Ancient thinkpad BIOSes have to be identified by
  5536. * BIOS type or model number, and there are far less
  5537. * BIOS types than model numbers... */
  5538. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  5539. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  5540. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  5541. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  5542. MODULE_DESCRIPTION(TPACPI_DESC);
  5543. MODULE_VERSION(TPACPI_VERSION);
  5544. MODULE_LICENSE("GPL");
  5545. module_init(thinkpad_acpi_module_init);
  5546. module_exit(thinkpad_acpi_module_exit);