thinkpad_acpi.c 187 KB

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