thinkpad_acpi.c 168 KB

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