thinkpad_acpi.c 232 KB

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