thinkpad_acpi.c 196 KB

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