thinkpad_acpi.c 218 KB

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